add noop broker and noop store (#30)

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
Василий Толстов 2020-09-03 15:11:05 +03:00 committed by GitHub
parent 0252addf05
commit c062aab1a9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 126 additions and 16 deletions

View File

@ -2,7 +2,7 @@
package broker
var (
DefaultBroker Broker
DefaultBroker Broker = newBroker()
)
// Broker is an interface used for asynchronous messaging.

74
broker/noop.go Normal file
View File

@ -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}
}

View File

@ -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}
}

View File

@ -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

View File

@ -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

View File

@ -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 {

View File

@ -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)

View File

@ -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