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:
2020-10-16 09:38:57 +03:00
parent a59aae760f
commit 14c97d59c1
39 changed files with 1384 additions and 432 deletions

View File

@@ -1,53 +1,59 @@
package registry
import "fmt"
import (
"context"
"fmt"
)
type noopRegistry struct {
type NoopRegistry struct {
opts Options
}
func (n *noopRegistry) Init(opts ...Option) error {
func (n *NoopRegistry) Init(opts ...Option) error {
for _, o := range opts {
o(&n.opts)
}
return nil
}
func (n *noopRegistry) Options() Options {
func (n *NoopRegistry) Options() Options {
return n.opts
}
func (n *noopRegistry) Register(*Service, ...RegisterOption) error {
func (n *NoopRegistry) Connect(ctx context.Context) error {
return nil
}
func (n *noopRegistry) Deregister(*Service, ...DeregisterOption) error {
func (n *NoopRegistry) Disconnect(ctx context.Context) error {
return nil
}
func (n *noopRegistry) GetService(string, ...GetOption) ([]*Service, error) {
func (n *NoopRegistry) Register(*Service, ...RegisterOption) error {
return nil
}
func (n *NoopRegistry) Deregister(*Service, ...DeregisterOption) error {
return nil
}
func (n *NoopRegistry) GetService(string, ...GetOption) ([]*Service, error) {
return []*Service{}, nil
}
func (n *noopRegistry) ListServices(...ListOption) ([]*Service, error) {
func (n *NoopRegistry) ListServices(...ListOption) ([]*Service, error) {
return []*Service{}, nil
}
func (n *noopRegistry) Watch(...WatchOption) (Watcher, error) {
func (n *NoopRegistry) Watch(...WatchOption) (Watcher, error) {
return nil, fmt.Errorf("not implemented")
}
func (n *noopRegistry) String() string {
func (n *NoopRegistry) String() string {
return "noop"
}
// newRegistry returns a new noop registry
func newRegistry(opts ...Option) Registry {
options := NewOptions()
for _, o := range opts {
o(&options)
}
return &noopRegistry{opts: options}
// NewRegistry returns a new noop registry
func NewRegistry(opts ...Option) Registry {
options := NewOptions(opts...)
return &NoopRegistry{opts: options}
}

View File

@@ -37,6 +37,8 @@ type RegisterOptions struct {
Context context.Context
// Domain to register the service in
Domain string
// Attempts specify attempts for register
Attempts int
}
type WatchOptions struct {
@@ -54,6 +56,8 @@ type DeregisterOptions struct {
Context context.Context
// Domain the service was registered in
Domain string
// Atempts specify max attempts for deregister
Attempts int
}
type GetOptions struct {
@@ -95,6 +99,13 @@ func Logger(l logger.Logger) Option {
}
}
// Context sets the context
func Context(ctx context.Context) Option {
return func(o *Options) {
o.Context = ctx
}
}
// Specify TLS Config
func TLSConfig(t *tls.Config) Option {
return func(o *Options) {
@@ -102,6 +113,12 @@ func TLSConfig(t *tls.Config) Option {
}
}
func RegisterAttempts(t int) RegisterOption {
return func(o *RegisterOptions) {
o.Attempts = t
}
}
func RegisterTTL(t time.Duration) RegisterOption {
return func(o *RegisterOptions) {
o.TTL = t
@@ -139,6 +156,12 @@ func WatchDomain(d string) WatchOption {
}
}
func DeregisterTimeout(t int) DeregisterOption {
return func(o *DeregisterOptions) {
o.Attempts = t
}
}
func DeregisterContext(ctx context.Context) DeregisterOption {
return func(o *DeregisterOptions) {
o.Context = ctx

View File

@@ -2,6 +2,7 @@
package registry
import (
"context"
"errors"
)
@@ -13,7 +14,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
@@ -26,6 +27,8 @@ var (
type Registry interface {
Init(...Option) error
Options() Options
Connect(context.Context) error
Disconnect(context.Context) error
Register(*Service, ...RegisterOption) error
Deregister(*Service, ...DeregisterOption) error
GetService(string, ...GetOption) ([]*Service, error)