many improvements with options and noop stuff
* add many options helpers * fix noop client to allow publish messages to topic in broker * fix noop server to allow registering in registry * fix noop server to allow subscribe to topic in broker * fix new service initialization Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
@@ -1,8 +1,10 @@
|
||||
// Package broker is an interface used for asynchronous messaging
|
||||
package broker
|
||||
|
||||
import "context"
|
||||
|
||||
var (
|
||||
DefaultBroker Broker = newBroker()
|
||||
DefaultBroker Broker = &NoopBroker{opts: NewOptions()}
|
||||
)
|
||||
|
||||
// Broker is an interface used for asynchronous messaging.
|
||||
@@ -10,10 +12,10 @@ type Broker interface {
|
||||
Init(...Option) error
|
||||
Options() Options
|
||||
Address() string
|
||||
Connect() error
|
||||
Disconnect() error
|
||||
Publish(topic string, m *Message, opts ...PublishOption) error
|
||||
Subscribe(topic string, h Handler, opts ...SubscribeOption) (Subscriber, error)
|
||||
Connect(context.Context) error
|
||||
Disconnect(context.Context) error
|
||||
Publish(context.Context, string, *Message, ...PublishOption) error
|
||||
Subscribe(context.Context, string, Handler, ...SubscribeOption) (Subscriber, error)
|
||||
String() string
|
||||
}
|
||||
|
||||
@@ -39,5 +41,5 @@ type Message struct {
|
||||
type Subscriber interface {
|
||||
Options() SubscribeOptions
|
||||
Topic() string
|
||||
Unsubscribe() error
|
||||
Unsubscribe(context.Context) error
|
||||
}
|
||||
|
@@ -1,6 +1,8 @@
|
||||
package broker
|
||||
|
||||
type noopBroker struct {
|
||||
import "context"
|
||||
|
||||
type NoopBroker struct {
|
||||
opts Options
|
||||
}
|
||||
|
||||
@@ -9,7 +11,7 @@ type noopSubscriber struct {
|
||||
opts SubscribeOptions
|
||||
}
|
||||
|
||||
func (n *noopBroker) Init(opts ...Option) error {
|
||||
func (n *NoopBroker) Init(opts ...Option) error {
|
||||
for _, o := range opts {
|
||||
o(&n.opts)
|
||||
}
|
||||
@@ -17,27 +19,27 @@ func (n *noopBroker) Init(opts ...Option) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (n *noopBroker) Options() Options {
|
||||
func (n *NoopBroker) Options() Options {
|
||||
return n.opts
|
||||
}
|
||||
|
||||
func (n *noopBroker) Address() string {
|
||||
func (n *NoopBroker) Address() string {
|
||||
return ""
|
||||
}
|
||||
|
||||
func (n *noopBroker) Connect() error {
|
||||
func (n *NoopBroker) Connect(ctx context.Context) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (n *noopBroker) Disconnect() error {
|
||||
func (n *NoopBroker) Disconnect(ctx context.Context) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (n *noopBroker) Publish(topic string, m *Message, opts ...PublishOption) error {
|
||||
func (n *NoopBroker) Publish(ctx context.Context, topic string, m *Message, opts ...PublishOption) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (n *noopBroker) Subscribe(topic string, h Handler, opts ...SubscribeOption) (Subscriber, error) {
|
||||
func (n *NoopBroker) Subscribe(ctx context.Context, topic string, h Handler, opts ...SubscribeOption) (Subscriber, error) {
|
||||
options := NewSubscribeOptions()
|
||||
|
||||
for _, o := range opts {
|
||||
@@ -47,7 +49,7 @@ func (n *noopBroker) Subscribe(topic string, h Handler, opts ...SubscribeOption)
|
||||
return &noopSubscriber{topic: topic, opts: options}, nil
|
||||
}
|
||||
|
||||
func (n *noopBroker) String() string {
|
||||
func (n *NoopBroker) String() string {
|
||||
return "noop"
|
||||
}
|
||||
|
||||
@@ -59,16 +61,6 @@ func (n *noopSubscriber) Topic() string {
|
||||
return n.topic
|
||||
}
|
||||
|
||||
func (n *noopSubscriber) Unsubscribe() error {
|
||||
func (n *noopSubscriber) Unsubscribe(ctx context.Context) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// newBroker returns a new noop broker
|
||||
func newBroker(opts ...Option) Broker {
|
||||
options := NewOptions()
|
||||
|
||||
for _, o := range opts {
|
||||
o(&options)
|
||||
}
|
||||
return &noopBroker{opts: options}
|
||||
}
|
||||
|
@@ -39,6 +39,12 @@ func NewOptions(opts ...Option) Options {
|
||||
return options
|
||||
}
|
||||
|
||||
func Context(ctx context.Context) Option {
|
||||
return func(o *Options) {
|
||||
o.Context = ctx
|
||||
}
|
||||
}
|
||||
|
||||
type PublishOptions struct {
|
||||
// Other options for implementations of the interface
|
||||
// can be stored in a context
|
||||
|
Reference in New Issue
Block a user