From c062aab1a9005fb554bb469771e4ba3937ee0806 Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Thu, 3 Sep 2020 15:11:05 +0300 Subject: [PATCH] add noop broker and noop store (#30) Signed-off-by: Vasiliy Tolstov --- broker/broker.go | 2 +- broker/noop.go | 74 ++++++++++++++++++++++++++++++++++++++++++++ registry/noop.go | 29 +++++++++++------ registry/options.go | 7 +++++ registry/registry.go | 2 +- store/noop.go | 17 ++++++++-- store/options.go | 7 +++++ store/store.go | 4 +-- 8 files changed, 126 insertions(+), 16 deletions(-) create mode 100644 broker/noop.go diff --git a/broker/broker.go b/broker/broker.go index fda70e40..d0c185f3 100644 --- a/broker/broker.go +++ b/broker/broker.go @@ -2,7 +2,7 @@ package broker var ( - DefaultBroker Broker + DefaultBroker Broker = newBroker() ) // Broker is an interface used for asynchronous messaging. diff --git a/broker/noop.go b/broker/noop.go new file mode 100644 index 00000000..29dad2c5 --- /dev/null +++ b/broker/noop.go @@ -0,0 +1,74 @@ +package broker + +type noopBroker struct { + opts Options +} + +type noopSubscriber struct { + topic string + opts SubscribeOptions +} + +func (n *noopBroker) Init(opts ...Option) error { + for _, o := range opts { + o(&n.opts) + } + + return nil +} + +func (n *noopBroker) Options() Options { + return n.opts +} + +func (n *noopBroker) Address() string { + return "" +} + +func (n *noopBroker) Connect() error { + return nil +} + +func (n *noopBroker) Disconnect() error { + return nil +} + +func (n *noopBroker) Publish(topic string, m *Message, opts ...PublishOption) error { + return nil +} + +func (n *noopBroker) Subscribe(topic string, h Handler, opts ...SubscribeOption) (Subscriber, error) { + options := NewSubscribeOptions() + + for _, o := range opts { + o(&options) + } + + return &noopSubscriber{topic: topic, opts: options}, nil +} + +func (n *noopBroker) String() string { + return "noop" +} + +func (n *noopSubscriber) Options() SubscribeOptions { + return n.opts +} + +func (n *noopSubscriber) Topic() string { + return n.topic +} + +func (n *noopSubscriber) Unsubscribe() 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} +} diff --git a/registry/noop.go b/registry/noop.go index 4ea2fe7e..2370a642 100644 --- a/registry/noop.go +++ b/registry/noop.go @@ -1,17 +1,20 @@ package registry -import ( - "errors" -) +import "fmt" -type noopRegistry struct{} +type noopRegistry struct { + opts Options +} -func (n *noopRegistry) Init(...Option) error { +func (n *noopRegistry) Init(opts ...Option) error { + for _, o := range opts { + o(&n.opts) + } return nil } func (n *noopRegistry) Options() Options { - return Options{} + return n.opts } func (n *noopRegistry) Register(*Service, ...RegisterOption) error { @@ -31,14 +34,20 @@ func (n *noopRegistry) ListServices(...ListOption) ([]*Service, error) { } func (n *noopRegistry) Watch(...WatchOption) (Watcher, error) { - return nil, errors.New("not implemented") + return nil, fmt.Errorf("not implemented") } func (n *noopRegistry) String() string { return "noop" } -// NewRegistry returns a new noop registry -func NewRegistry(...Option) Registry { - return &noopRegistry{} +// newRegistry returns a new noop registry +func newRegistry(opts ...Option) Registry { + options := NewOptions() + + for _, o := range opts { + o(&options) + } + + return &noopRegistry{opts: options} } diff --git a/registry/options.go b/registry/options.go index b8f3bb42..598ecc9d 100644 --- a/registry/options.go +++ b/registry/options.go @@ -19,6 +19,13 @@ type Options struct { Context context.Context } +func NewOptions() Options { + return Options{ + Logger: logger.DefaultLogger, + Context: context.Background(), + } +} + type RegisterOptions struct { TTL time.Duration // Other options for implementations of the interface diff --git a/registry/registry.go b/registry/registry.go index 11e9f926..4c042dac 100644 --- a/registry/registry.go +++ b/registry/registry.go @@ -13,7 +13,7 @@ const ( ) var ( - DefaultRegistry Registry = NewRegistry() + DefaultRegistry Registry = newRegistry() // ErrNotFound returned when GetService is called and no services found ErrNotFound = errors.New("service not found") // ErrWatcherStopped returned when when watcher is stopped diff --git a/store/noop.go b/store/noop.go index 25d4850a..57ad55de 100644 --- a/store/noop.go +++ b/store/noop.go @@ -1,13 +1,26 @@ package store -type noopStore struct{} +type noopStore struct { + opts Options +} + +func newStore(opts ...Option) Store { + options := NewOptions() + for _, o := range opts { + o(&options) + } + return &noopStore{opts: options} +} func (n *noopStore) Init(opts ...Option) error { + for _, o := range opts { + o(&n.opts) + } return nil } func (n *noopStore) Options() Options { - return Options{} + return n.opts } func (n *noopStore) String() string { diff --git a/store/options.go b/store/options.go index e6972ff5..2a5685b4 100644 --- a/store/options.go +++ b/store/options.go @@ -23,6 +23,13 @@ type Options struct { Context context.Context } +func NewOptions() Options { + return Options{ + Logger: logger.DefaultLogger, + Context: context.Background(), + } +} + // Option sets values in Options type Option func(o *Options) diff --git a/store/store.go b/store/store.go index b48f9999..1299dfcd 100644 --- a/store/store.go +++ b/store/store.go @@ -9,8 +9,8 @@ import ( var ( // ErrNotFound is returned when a key doesn't exist - ErrNotFound = errors.New("not found") - DefaultStore Store + ErrNotFound = errors.New("not found") + DefaultStore Store = newStore() ) // Store is a data storage interface