From 40b0870cf813030590eeed220cbf51aabd6cb154 Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Tue, 3 Nov 2020 01:08:23 +0300 Subject: [PATCH] fix linting (#4) Signed-off-by: Vasiliy Tolstov --- api/resolver/grpc/grpc.go | 3 ++ api/resolver/options.go | 9 +++-- api/resolver/resolver.go | 6 ++-- api/server/options.go | 1 + broker/broker.go | 2 +- broker/noop.go | 41 ++++++++++++++--------- broker/options.go | 27 +++++++++++---- build/options.go | 4 ++- client/backoff.go | 1 + client/client.go | 2 +- client/noop.go | 41 +++++++++-------------- function_test.go | 3 +- metadata/metadata.go | 4 +++ network/options.go | 1 + network/transport/noop.go | 16 +++++---- network/transport/transport.go | 3 +- network/tunnel/options.go | 25 +++++++++----- network/tunnel/transport/transport.go | 2 +- options.go | 2 ++ registry/extractor.go | 3 ++ registry/noop.go | 35 ++++++++++++-------- registry/registry.go | 1 + router/router.go | 1 + runtime/runtime.go | 1 + server/context.go | 2 ++ server/noop.go | 31 ++++++++++-------- server/options.go | 26 +++++++++------ server/registry.go | 1 + server/server.go | 2 +- server/subscriber.go | 3 +- store/noop.go | 33 +++++++++++++------ store/store.go | 6 ++-- tracer/options.go | 5 ++- util/http/options.go | 3 ++ util/signal/signal.go | 13 -------- util/test/test.go | 47 --------------------------- 36 files changed, 218 insertions(+), 188 deletions(-) delete mode 100644 util/signal/signal.go delete mode 100644 util/test/test.go diff --git a/api/resolver/grpc/grpc.go b/api/resolver/grpc/grpc.go index f06be716..1bcb5a02 100644 --- a/api/resolver/grpc/grpc.go +++ b/api/resolver/grpc/grpc.go @@ -9,10 +9,12 @@ import ( "github.com/unistack-org/micro/v3/api/resolver" ) +// Resolver struct type Resolver struct { opts resolver.Options } +// Resolve func to resolve enndpoint func (r *Resolver) Resolve(req *http.Request, opts ...resolver.ResolveOption) (*resolver.Endpoint, error) { // parse options options := resolver.NewResolveOptions(opts...) @@ -39,6 +41,7 @@ func (r *Resolver) String() string { return "grpc" } +// NewResolver is used to create new Resolver func NewResolver(opts ...resolver.Option) resolver.Resolver { return &Resolver{opts: resolver.NewOptions(opts...)} } diff --git a/api/resolver/options.go b/api/resolver/options.go index 115c1f22..87e99e29 100644 --- a/api/resolver/options.go +++ b/api/resolver/options.go @@ -4,11 +4,13 @@ import ( "github.com/unistack-org/micro/v3/registry" ) +// Options struct type Options struct { Handler string ServicePrefix string } +// Option func type Option func(o *Options) // WithHandler sets the handler being used @@ -27,7 +29,7 @@ func WithServicePrefix(p string) Option { // NewOptions returns new initialised options func NewOptions(opts ...Option) Options { - var options Options + options := Options{} for _, o := range opts { o(&options) } @@ -51,13 +53,10 @@ func Domain(n string) ResolveOption { // NewResolveOptions returns new initialised resolve options func NewResolveOptions(opts ...ResolveOption) ResolveOptions { - var options ResolveOptions + options := ResolveOptions{Domain: registry.DefaultDomain} for _, o := range opts { o(&options) } - if len(options.Domain) == 0 { - options.Domain = registry.DefaultDomain - } return options } diff --git a/api/resolver/resolver.go b/api/resolver/resolver.go index 67da74f1..67143e2f 100644 --- a/api/resolver/resolver.go +++ b/api/resolver/resolver.go @@ -7,7 +7,9 @@ import ( ) var ( - ErrNotFound = errors.New("not found") + // ErrNotFound returned when endpoint is not found + ErrNotFound = errors.New("not found") + // ErrInvalidPath returned on invalid path ErrInvalidPath = errors.New("invalid path") ) @@ -19,7 +21,7 @@ type Resolver interface { // Endpoint is the endpoint for a http request type Endpoint struct { - // e.g greeter + // Endpoint name e.g greeter Name string // HTTP Host e.g example.com Host string diff --git a/api/server/options.go b/api/server/options.go index 1f974941..f58d2b08 100644 --- a/api/server/options.go +++ b/api/server/options.go @@ -23,6 +23,7 @@ type Options struct { Wrappers []Wrapper } +// NewOptions returns new Options func NewOptions(opts ...Option) Options { options := Options{} for _, o := range opts { diff --git a/broker/broker.go b/broker/broker.go index 883afb0e..62e7cfd7 100644 --- a/broker/broker.go +++ b/broker/broker.go @@ -4,7 +4,7 @@ package broker import "context" var ( - DefaultBroker Broker = &NoopBroker{opts: NewOptions()} + DefaultBroker Broker = NewBroker() ) // Broker is an interface used for asynchronous messaging. diff --git a/broker/noop.go b/broker/noop.go index 9dfcac87..932da27c 100644 --- a/broker/noop.go +++ b/broker/noop.go @@ -2,7 +2,7 @@ package broker import "context" -type NoopBroker struct { +type noopBroker struct { opts Options } @@ -11,7 +11,13 @@ type noopSubscriber struct { opts SubscribeOptions } -func (n *NoopBroker) Init(opts ...Option) error { +// NewBroker returns new noop broker +func NewBroker(opts ...Option) Broker { + return &noopBroker{opts: NewOptions(opts...)} +} + +// Init initialize broker +func (n *noopBroker) Init(opts ...Option) error { for _, o := range opts { o(&n.opts) } @@ -19,48 +25,53 @@ func (n *NoopBroker) Init(opts ...Option) error { return nil } -func (n *NoopBroker) Options() Options { +// Options returns broker Options +func (n *noopBroker) Options() Options { return n.opts } -func (n *NoopBroker) Address() string { +// Address returns broker address +func (n *noopBroker) Address() string { return "" } -func (n *NoopBroker) Connect(ctx context.Context) error { +// Connect connects to broker +func (n *noopBroker) Connect(ctx context.Context) error { return nil } -func (n *NoopBroker) Disconnect(ctx context.Context) error { +// Disconnect disconnects from broker +func (n *noopBroker) Disconnect(ctx context.Context) error { return nil } -func (n *NoopBroker) Publish(ctx context.Context, topic string, m *Message, opts ...PublishOption) error { +// Publish publishes message to broker +func (n *noopBroker) Publish(ctx context.Context, topic string, m *Message, opts ...PublishOption) error { return nil } -func (n *NoopBroker) Subscribe(ctx context.Context, topic string, h Handler, opts ...SubscribeOption) (Subscriber, error) { - options := NewSubscribeOptions() - - for _, o := range opts { - o(&options) - } - +// Subscribe subscribes to broker topic +func (n *noopBroker) Subscribe(ctx context.Context, topic string, h Handler, opts ...SubscribeOption) (Subscriber, error) { + options := NewSubscribeOptions(opts...) return &noopSubscriber{topic: topic, opts: options}, nil } -func (n *NoopBroker) String() string { +// String return broker string representation +func (n *noopBroker) String() string { return "noop" } +// Options returns subscriber options func (n *noopSubscriber) Options() SubscribeOptions { return n.opts } +// TOpic returns subscriber topic func (n *noopSubscriber) Topic() string { return n.topic } +// Unsubscribe unsbscribes from broker topic func (n *noopSubscriber) Unsubscribe(ctx context.Context) error { return nil } diff --git a/broker/options.go b/broker/options.go index b48cf5d4..cef47c23 100644 --- a/broker/options.go +++ b/broker/options.go @@ -9,6 +9,7 @@ import ( "github.com/unistack-org/micro/v3/registry" ) +// Options struct type Options struct { Addrs []string Secure bool @@ -27,6 +28,7 @@ type Options struct { Context context.Context } +// NewOptions create new Options func NewOptions(opts ...Option) Options { options := Options{ Registry: registry.DefaultRegistry, @@ -39,30 +41,34 @@ func NewOptions(opts ...Option) Options { return options } +// Context sets the context option func Context(ctx context.Context) Option { return func(o *Options) { o.Context = ctx } } +// PublishOptions struct type PublishOptions struct { // Other options for implementations of the interface // can be stored in a context Context context.Context } +// NewPublishOptions creates PublishOptions struct func NewPublishOptions(opts ...PublishOption) PublishOptions { - opt := PublishOptions{ + options := PublishOptions{ Context: context.Background(), } for _, o := range opts { - o(&opt) + o(&options) } - return opt + return options } +// SubscribeOptions struct type SubscribeOptions struct { // AutoAck ack messages if handler returns nil err AutoAck bool @@ -80,30 +86,34 @@ type SubscribeOptions struct { Context context.Context } +// Option func type Option func(*Options) +// PublishOption func type PublishOption func(*PublishOptions) -// PublishContext set context +// PublishContext sets the context func PublishContext(ctx context.Context) PublishOption { return func(o *PublishOptions) { o.Context = ctx } } +// SubscribeOption func type SubscribeOption func(*SubscribeOptions) +// NewSubscribeOptions creates new SubscribeOptions func NewSubscribeOptions(opts ...SubscribeOption) SubscribeOptions { - opt := SubscribeOptions{ + options := SubscribeOptions{ AutoAck: true, Context: context.Background(), } for _, o := range opts { - o(&opt) + o(&options) } - return opt + return options } // Addrs sets the host addresses to be used by the broker @@ -121,6 +131,7 @@ func Codec(c codec.Marshaler) Option { } } +// DisableAutoAck disables auto ack func DisableAutoAck() SubscribeOption { return func(o *SubscribeOptions) { o.AutoAck = false @@ -151,6 +162,7 @@ func SubscribeErrorHandler(h Handler) SubscribeOption { } } +// Queue sets the subscribers sueue func Queue(name string) SubscribeOption { return func(o *SubscribeOptions) { o.Group = name @@ -164,6 +176,7 @@ func SubscribeGroup(name string) SubscribeOption { } } +// Registry sets registry option func Registry(r registry.Registry) Option { return func(o *Options) { o.Registry = r diff --git a/build/options.go b/build/options.go index 65e7dd68..93a494b0 100644 --- a/build/options.go +++ b/build/options.go @@ -1,13 +1,15 @@ package build +// Options struct type Options struct { // local path to download source Path string } +// Option func type Option func(o *Options) -// Local path for repository +// Path is the Local path for repository func Path(p string) Option { return func(o *Options) { o.Path = p diff --git a/client/backoff.go b/client/backoff.go index 5a401453..69087d4e 100644 --- a/client/backoff.go +++ b/client/backoff.go @@ -7,6 +7,7 @@ import ( "github.com/unistack-org/micro/v3/util/backoff" ) +// BackoffFunc is the backoff call func type BackoffFunc func(ctx context.Context, req Request, attempts int) (time.Duration, error) func exponentialBackoff(ctx context.Context, req Request, attempts int) (time.Duration, error) { diff --git a/client/client.go b/client/client.go index 4cb10297..1615cec8 100644 --- a/client/client.go +++ b/client/client.go @@ -9,7 +9,7 @@ import ( ) var ( - DefaultClient Client = &NoopClient{opts: NewOptions()} + DefaultClient Client = NewClient() ) // Client is the interface used to make requests to services. diff --git a/client/noop.go b/client/noop.go index 5a06ee0a..351094c6 100644 --- a/client/noop.go +++ b/client/noop.go @@ -11,7 +11,7 @@ import ( "github.com/unistack-org/micro/v3/metadata" ) -type NoopClient struct { +type noopClient struct { opts Options } @@ -31,6 +31,11 @@ type noopRequest struct { stream bool } +// NewClient returns new noop client +func NewClient(opts ...Option) Client { + return &noopClient{opts: NewOptions(opts...)} +} + func (n *noopRequest) Service() string { return n.service } @@ -118,49 +123,41 @@ func (n *noopMessage) ContentType() string { return n.opts.ContentType } -func (n *NoopClient) Init(opts ...Option) error { +func (n *noopClient) Init(opts ...Option) error { for _, o := range opts { o(&n.opts) } return nil } -func (n *NoopClient) Options() Options { +func (n *noopClient) Options() Options { return n.opts } -func (n *NoopClient) String() string { +func (n *noopClient) String() string { return "noop" } -func (n *NoopClient) Call(ctx context.Context, req Request, rsp interface{}, opts ...CallOption) error { +func (n *noopClient) Call(ctx context.Context, req Request, rsp interface{}, opts ...CallOption) error { return nil } -func (n *NoopClient) NewRequest(service, endpoint string, req interface{}, opts ...RequestOption) Request { +func (n *noopClient) NewRequest(service, endpoint string, req interface{}, opts ...RequestOption) Request { return &noopRequest{} } -func (n *NoopClient) NewMessage(topic string, msg interface{}, opts ...MessageOption) Message { - options := MessageOptions{} - for _, o := range opts { - o(&options) - } - +func (n *noopClient) NewMessage(topic string, msg interface{}, opts ...MessageOption) Message { + options := NewMessageOptions(opts...) return &noopMessage{topic: topic, payload: msg, opts: options} } -func (n *NoopClient) Stream(ctx context.Context, req Request, opts ...CallOption) (Stream, error) { +func (n *noopClient) Stream(ctx context.Context, req Request, opts ...CallOption) (Stream, error) { return &noopStream{}, nil } -func (n *NoopClient) Publish(ctx context.Context, p Message, opts ...PublishOption) error { +func (n *noopClient) Publish(ctx context.Context, p Message, opts ...PublishOption) error { var body []byte - if err := n.opts.Broker.Connect(ctx); err != nil { - return errors.InternalServerError("go.micro.client", err.Error()) - } - options := NewPublishOptions(opts...) md, ok := metadata.FromContext(ctx) @@ -208,11 +205,3 @@ func (n *NoopClient) Publish(ctx context.Context, p Message, opts ...PublishOpti return nil } - -func newClient(opts ...Option) Client { - options := NewOptions() - for _, o := range opts { - o(&options) - } - return &NoopClient{opts: options} -} diff --git a/function_test.go b/function_test.go index 0c068408..bd1c881e 100644 --- a/function_test.go +++ b/function_test.go @@ -8,14 +8,13 @@ import ( "testing" rmemory "github.com/unistack-org/micro-registry-memory" - "github.com/unistack-org/micro/v3/util/test" ) func TestFunction(t *testing.T) { var wg sync.WaitGroup wg.Add(1) - r := rmemory.NewRegistry(rmemory.Services(test.Data)) + r := rmemory.NewRegistry() // create service fn := NewFunction( diff --git a/metadata/metadata.go b/metadata/metadata.go index ac8d17ce..1165bc95 100644 --- a/metadata/metadata.go +++ b/metadata/metadata.go @@ -14,9 +14,11 @@ type metadataKey struct{} type Metadata map[string]string var ( + // DefaultMetadataSize used when need to init new Metadata DefaultMetadataSize = 6 ) +// Get returns value from metadata by key func (md Metadata) Get(key string) (string, bool) { // fast path val, ok := md[key] @@ -27,10 +29,12 @@ func (md Metadata) Get(key string) (string, bool) { return val, ok } +// Set is used to store value in metadata func (md Metadata) Set(key, val string) { md[textproto.CanonicalMIMEHeaderKey(key)] = val } +// Del is used to remove value from metadata func (md Metadata) Del(key string) { // fast path if _, ok := md[key]; ok { diff --git a/network/options.go b/network/options.go index 90101277..38d8284f 100644 --- a/network/options.go +++ b/network/options.go @@ -8,6 +8,7 @@ import ( "github.com/unistack-org/micro/v3/router" ) +// Option func type Option func(*Options) // Options configure network diff --git a/network/transport/noop.go b/network/transport/noop.go index 95a039a0..9b5efdf0 100644 --- a/network/transport/noop.go +++ b/network/transport/noop.go @@ -1,31 +1,35 @@ package transport -type NoopTransport struct { +type noopTransport struct { opts Options } -func (t *NoopTransport) Init(opts ...Option) error { +func NewTransport(opts ...Option) Transport { + return &noopTransport{opts: NewOptions(opts...)} +} + +func (t *noopTransport) Init(opts ...Option) error { for _, o := range opts { o(&t.opts) } return nil } -func (t *NoopTransport) Options() Options { +func (t *noopTransport) Options() Options { return t.opts } -func (t *NoopTransport) Dial(addr string, opts ...DialOption) (Client, error) { +func (t *noopTransport) Dial(addr string, opts ...DialOption) (Client, error) { options := NewDialOptions(opts...) return &noopClient{opts: options}, nil } -func (t *NoopTransport) Listen(addr string, opts ...ListenOption) (Listener, error) { +func (t *noopTransport) Listen(addr string, opts ...ListenOption) (Listener, error) { options := NewListenOptions(opts...) return &noopListener{opts: options}, nil } -func (t *NoopTransport) String() string { +func (t *noopTransport) String() string { return "noop" } diff --git a/network/transport/transport.go b/network/transport/transport.go index e3b0a957..f9314516 100644 --- a/network/transport/transport.go +++ b/network/transport/transport.go @@ -6,7 +6,8 @@ import ( ) var ( - DefaultTransport Transport = &NoopTransport{opts: NewOptions()} + // DefaultTransport is the global default transport + DefaultTransport Transport = NewTransport() ) // Transport is an interface which is used for communication between diff --git a/network/tunnel/options.go b/network/tunnel/options.go index 9f29d9fe..4a2fa6c9 100644 --- a/network/tunnel/options.go +++ b/network/tunnel/options.go @@ -15,6 +15,7 @@ var ( DefaultToken = "go.micro.tunnel" ) +// Option func type Option func(*Options) // Options provides network configuration options @@ -33,8 +34,10 @@ type Options struct { Logger logger.Logger } +// DialOption func type DialOption func(*DialOptions) +// DialOptions provides dial options type DialOptions struct { // Link specifies the link to use Link string @@ -46,8 +49,10 @@ type DialOptions struct { Timeout time.Duration } +// ListenOption func type ListenOption func(*ListenOptions) +// ListenOptions provides listen options type ListenOptions struct { // specify mode of the session Mode Mode @@ -55,7 +60,7 @@ type ListenOptions struct { Timeout time.Duration } -// The tunnel id +// Id sets the tunnel id func Id(id string) Option { return func(o *Options) { o.Id = id @@ -69,7 +74,7 @@ func Logger(l logger.Logger) Option { } } -// The tunnel address +// Address sets the tunnel address func Address(a string) Option { return func(o *Options) { o.Address = a @@ -97,23 +102,21 @@ func Transport(t transport.Transport) Option { } } -// Listen options +// ListenMode option func ListenMode(m Mode) ListenOption { return func(o *ListenOptions) { o.Mode = m } } -// Timeout for reads and writes on the listener session +// ListenTimeout for reads and writes on the listener session func ListenTimeout(t time.Duration) ListenOption { return func(o *ListenOptions) { o.Timeout = t } } -// Dial options - -// Dial multicast sets the multicast option to send only to those mapped +// DialMode multicast sets the multicast option to send only to those mapped func DialMode(m Mode) DialOption { return func(o *DialOptions) { o.Mode = m @@ -144,10 +147,14 @@ func DialWait(b bool) DialOption { } // DefaultOptions returns router default options -func DefaultOptions() Options { - return Options{ +func NewOptions(opts ...Option) Options { + options := Options{ Id: uuid.New().String(), Address: DefaultAddress, Token: DefaultToken, } + for _, o := range opts { + o(&options) + } + return options } diff --git a/network/tunnel/transport/transport.go b/network/tunnel/transport/transport.go index 018c2a5b..cb6ea8a6 100644 --- a/network/tunnel/transport/transport.go +++ b/network/tunnel/transport/transport.go @@ -93,7 +93,7 @@ func NewTransport(opts ...transport.Option) transport.Transport { return t } -// WithTransport sets the internal tunnel +// WithTunnel sets the internal tunnel func WithTunnel(t tunnel.Tunnel) transport.Option { return func(o *transport.Options) { if o.Context == nil { diff --git a/options.go b/options.go index 363c3706..7d56e9db 100644 --- a/options.go +++ b/options.go @@ -49,6 +49,7 @@ type Options struct { Context context.Context } +// NewOptions returns new Options filled with defaults and overrided by provided opts func NewOptions(opts ...Option) Options { options := Options{ Context: context.Background(), @@ -73,6 +74,7 @@ func NewOptions(opts ...Option) Options { return options } +// Option func type Option func(*Options) // Broker to be used for service diff --git a/registry/extractor.go b/registry/extractor.go index cdb381f1..64ca9458 100644 --- a/registry/extractor.go +++ b/registry/extractor.go @@ -6,6 +6,7 @@ import ( "strings" ) +// Extract *Value from reflect.Type func ExtractValue(v reflect.Type, d int) *Value { if d == 3 { return nil @@ -59,6 +60,7 @@ func ExtractValue(v reflect.Type, d int) *Value { return arg } +// ExtractEndpoint extract *Endpoint from reflect.Method func ExtractEndpoint(method reflect.Method) *Endpoint { if method.PkgPath != "" { return nil @@ -104,6 +106,7 @@ func ExtractEndpoint(method reflect.Method) *Endpoint { return ep } +// ExtractSubValue exctact *Value from reflect.Type func ExtractSubValue(typ reflect.Type) *Value { var reqType reflect.Type switch typ.NumIn() { diff --git a/registry/noop.go b/registry/noop.go index 4b995790..0a637b95 100644 --- a/registry/noop.go +++ b/registry/noop.go @@ -5,55 +5,64 @@ import ( "fmt" ) -type NoopRegistry struct { +type noopRegistry struct { opts Options } -func (n *NoopRegistry) Init(opts ...Option) error { +// Init initialize registry +func (n *noopRegistry) Init(opts ...Option) error { for _, o := range opts { o(&n.opts) } return nil } -func (n *NoopRegistry) Options() Options { +// Options returns options struct +func (n *noopRegistry) Options() Options { return n.opts } -func (n *NoopRegistry) Connect(ctx context.Context) error { +// Connect opens connection to registry +func (n *noopRegistry) Connect(ctx context.Context) error { return nil } -func (n *NoopRegistry) Disconnect(ctx context.Context) error { +// Disconnect close connection to registry +func (n *noopRegistry) Disconnect(ctx context.Context) error { return nil } -func (n *NoopRegistry) Register(*Service, ...RegisterOption) error { +// Register registers service +func (n *noopRegistry) Register(*Service, ...RegisterOption) error { return nil } -func (n *NoopRegistry) Deregister(*Service, ...DeregisterOption) error { +// Deregister deregisters service +func (n *noopRegistry) Deregister(*Service, ...DeregisterOption) error { return nil } -func (n *NoopRegistry) GetService(string, ...GetOption) ([]*Service, error) { +// GetService returns servive info +func (n *noopRegistry) GetService(string, ...GetOption) ([]*Service, error) { return []*Service{}, nil } -func (n *NoopRegistry) ListServices(...ListOption) ([]*Service, error) { +// ListServices listing services +func (n *noopRegistry) ListServices(...ListOption) ([]*Service, error) { return []*Service{}, nil } -func (n *NoopRegistry) Watch(...WatchOption) (Watcher, error) { +// Watch is used to watch for service changes +func (n *noopRegistry) Watch(...WatchOption) (Watcher, error) { return nil, fmt.Errorf("not implemented") } -func (n *NoopRegistry) String() string { +// String returns registry string representation +func (n *noopRegistry) String() string { return "noop" } // NewRegistry returns a new noop registry func NewRegistry(opts ...Option) Registry { - options := NewOptions(opts...) - return &NoopRegistry{opts: options} + return &noopRegistry{opts: NewOptions(opts...)} } diff --git a/registry/registry.go b/registry/registry.go index c76f7982..742416b4 100644 --- a/registry/registry.go +++ b/registry/registry.go @@ -14,6 +14,7 @@ const ( ) var ( + // DefaultRegistry is the global default registry DefaultRegistry Registry = NewRegistry() // ErrNotFound returned when GetService is called and no services found ErrNotFound = errors.New("service not found") diff --git a/router/router.go b/router/router.go index 1ce85fdc..be07c687 100644 --- a/router/router.go +++ b/router/router.go @@ -6,6 +6,7 @@ import ( ) var ( + // DefaultRouter is the global default router DefaultRouter Router // DefaultNetwork is default micro network DefaultNetwork = "micro" diff --git a/runtime/runtime.go b/runtime/runtime.go index ec252d02..f46a9aab 100644 --- a/runtime/runtime.go +++ b/runtime/runtime.go @@ -7,6 +7,7 @@ import ( ) var ( + // ErrAlreadyExists error ErrAlreadyExists = errors.New("already exists") ) diff --git a/server/context.go b/server/context.go index daa70554..cbce8c45 100644 --- a/server/context.go +++ b/server/context.go @@ -6,11 +6,13 @@ import ( type serverKey struct{} +// FromContext returns Server from context func FromContext(ctx context.Context) (Server, bool) { c, ok := ctx.Value(serverKey{}).(Server) return c, ok } +// NewContext stores Server to context func NewContext(ctx context.Context, s Server) context.Context { return context.WithValue(ctx, serverKey{}, s) } diff --git a/server/noop.go b/server/noop.go index 98f65c6f..7393eec0 100644 --- a/server/noop.go +++ b/server/noop.go @@ -32,7 +32,7 @@ const ( defaultContentType = "application/json" ) -type NoopServer struct { +type noopServer struct { h Handler opts Options rsvc *registry.Service @@ -45,7 +45,12 @@ type NoopServer struct { sync.RWMutex } -func (n *NoopServer) newCodec(contentType string) (codec.NewCodec, error) { +// NewServer returns new noop server +func NewServer(opts ...Option) Server { + return &noopServer{opts: NewOptions(opts...)} +} + +func (n *noopServer) newCodec(contentType string) (codec.NewCodec, error) { if cf, ok := n.opts.Codecs[contentType]; ok { return cf, nil } @@ -55,12 +60,12 @@ func (n *NoopServer) newCodec(contentType string) (codec.NewCodec, error) { return nil, fmt.Errorf("Unsupported Content-Type: %s", contentType) } -func (n *NoopServer) Handle(handler Handler) error { +func (n *noopServer) Handle(handler Handler) error { n.h = handler return nil } -func (n *NoopServer) Subscribe(sb Subscriber) error { +func (n *noopServer) Subscribe(sb Subscriber) error { sub, ok := sb.(*subscriber) if !ok { return fmt.Errorf("invalid subscriber: expected *subscriber") @@ -84,15 +89,15 @@ func (n *NoopServer) Subscribe(sb Subscriber) error { return nil } -func (n *NoopServer) NewHandler(h interface{}, opts ...HandlerOption) Handler { +func (n *noopServer) NewHandler(h interface{}, opts ...HandlerOption) Handler { return newRpcHandler(h, opts...) } -func (n *NoopServer) NewSubscriber(topic string, sb interface{}, opts ...SubscriberOption) Subscriber { +func (n *noopServer) NewSubscriber(topic string, sb interface{}, opts ...SubscriberOption) Subscriber { return newSubscriber(topic, sb, opts...) } -func (n *NoopServer) Init(opts ...Option) error { +func (n *noopServer) Init(opts ...Option) error { for _, o := range opts { o(&n.opts) } @@ -110,15 +115,15 @@ func (n *NoopServer) Init(opts ...Option) error { return nil } -func (n *NoopServer) Options() Options { +func (n *noopServer) Options() Options { return n.opts } -func (n *NoopServer) String() string { +func (n *noopServer) String() string { return "noop" } -func (n *NoopServer) Register() error { +func (n *noopServer) Register() error { n.RLock() rsvc := n.rsvc config := n.opts @@ -233,7 +238,7 @@ func (n *NoopServer) Register() error { return nil } -func (n *NoopServer) Deregister() error { +func (n *noopServer) Deregister() error { var err error n.RLock() @@ -293,7 +298,7 @@ func (n *NoopServer) Deregister() error { return nil } -func (n *NoopServer) Start() error { +func (n *noopServer) Start() error { n.RLock() if n.started { n.RUnlock() @@ -433,7 +438,7 @@ func (n *NoopServer) Start() error { return nil } -func (n *NoopServer) Stop() error { +func (n *noopServer) Stop() error { n.RLock() if !n.started { n.RUnlock() diff --git a/server/options.go b/server/options.go index f0504df5..9df56c39 100644 --- a/server/options.go +++ b/server/options.go @@ -85,7 +85,7 @@ func NewOptions(opts ...Option) Options { return options } -// Server name +// Name sets the server name option func Name(n string) Option { return func(o *Options) { o.Name = n @@ -99,7 +99,7 @@ func Namespace(n string) Option { } } -// Logger +// Logger sets the logger option func Logger(l logger.Logger) Option { return func(o *Options) { o.Logger = l @@ -127,7 +127,7 @@ func Address(a string) Option { } } -// The address to advertise for discovery - host:port +// Advertise the address to advertise for discovery - host:port func Advertise(a string) Option { return func(o *Options) { o.Advertise = a @@ -199,14 +199,14 @@ func RegisterCheck(fn func(context.Context) error) Option { } } -// Register the service with a TTL +// RegisterTTL registers service with a TTL func RegisterTTL(t time.Duration) Option { return func(o *Options) { o.RegisterTTL = t } } -// Register the service with at interval +// RegisterInterval registers service with at interval func RegisterInterval(t time.Duration) Option { return func(o *Options) { o.RegisterInterval = t @@ -250,28 +250,31 @@ func Wait(wg *sync.WaitGroup) Option { } } -// Adds a handler Wrapper to a list of options passed into the server +// WrapHandler adds a handler Wrapper to a list of options passed into the server func WrapHandler(w HandlerWrapper) Option { return func(o *Options) { o.HdlrWrappers = append(o.HdlrWrappers, w) } } -// Adds a subscriber Wrapper to a list of options passed into the server +// WrapSubscriber adds a subscriber Wrapper to a list of options passed into the server func WrapSubscriber(w SubscriberWrapper) Option { return func(o *Options) { o.SubWrappers = append(o.SubWrappers, w) } } +// HandlerOption func type HandlerOption func(*HandlerOptions) +// HandlerOptions struct type HandlerOptions struct { Internal bool Metadata map[string]map[string]string Context context.Context } +// NewHandlerOptions creates new HandlerOptions func NewHandlerOptions(opts ...HandlerOption) HandlerOptions { options := HandlerOptions{ Context: context.Background(), @@ -284,8 +287,10 @@ func NewHandlerOptions(opts ...HandlerOption) HandlerOptions { return options } +// SubscriberOption func type SubscriberOption func(*SubscriberOptions) +// SubscriberOptions struct type SubscriberOptions struct { // AutoAck defaults to true. When a handler returns // with a nil error the message is acked. @@ -295,6 +300,7 @@ type SubscriberOptions struct { Context context.Context } +// NewSubscriberOptions create new SubscriberOptions func NewSubscriberOptions(opts ...SubscriberOption) SubscriberOptions { options := SubscriberOptions{ AutoAck: true, @@ -316,7 +322,7 @@ func EndpointMetadata(name string, md map[string]string) HandlerOption { } } -// Internal Handler options specifies that a handler is not advertised +// InternalHandler options specifies that a handler is not advertised // to the discovery system. In the future this may also limit request // to the internal network or authorised user. func InternalHandler(b bool) HandlerOption { @@ -325,7 +331,7 @@ func InternalHandler(b bool) HandlerOption { } } -// Internal Subscriber options specifies that a subscriber is not advertised +// InternalSubscriber options specifies that a subscriber is not advertised // to the discovery system. func InternalSubscriber(b bool) SubscriberOption { return func(o *SubscriberOptions) { @@ -341,7 +347,7 @@ func DisableAutoAck() SubscriberOption { } } -// Shared queue name distributed messages across subscribers +// SubscriberQueue sets the shared queue name distributed messages across subscribers func SubscriberQueue(n string) SubscriberOption { return func(o *SubscriberOptions) { o.Queue = n diff --git a/server/registry.go b/server/registry.go index 191b4e3b..1ed54341 100644 --- a/server/registry.go +++ b/server/registry.go @@ -52,6 +52,7 @@ var ( } ) +// NewRegistryService returns *registry.Service from Server func NewRegistryService(s Server) (*registry.Service, error) { opts := s.Options() diff --git a/server/server.go b/server/server.go index d8901833..70559c5e 100644 --- a/server/server.go +++ b/server/server.go @@ -11,7 +11,7 @@ import ( ) var ( - DefaultServer Server = &NoopServer{opts: NewOptions()} + DefaultServer Server = NewServer() ) // Server is a simple micro server abstraction diff --git a/server/subscriber.go b/server/subscriber.go index cfeca8ba..fa12664f 100644 --- a/server/subscriber.go +++ b/server/subscriber.go @@ -59,6 +59,7 @@ func isExportedOrBuiltinType(t reflect.Type) bool { return isExported(t.Name()) || t.PkgPath() == "" } +// ValidateSubscriber func func ValidateSubscriber(sub Subscriber) error { typ := reflect.TypeOf(sub.Subscriber()) var argType reflect.Type @@ -184,7 +185,7 @@ func newSubscriber(topic string, sub interface{}, opts ...SubscriberOption) Subs } } -func (n *NoopServer) createSubHandler(sb *subscriber, opts Options) broker.Handler { +func (n *noopServer) createSubHandler(sb *subscriber, opts Options) broker.Handler { return func(p broker.Event) (err error) { defer func() { if r := recover(); r != nil { diff --git a/store/noop.go b/store/noop.go index d71927eb..0c89e599 100644 --- a/store/noop.go +++ b/store/noop.go @@ -2,45 +2,58 @@ package store import "context" -type NoopStore struct { +type noopStore struct { opts Options } -func (n *NoopStore) Init(opts ...Option) error { +func NewStore(opts ...Option) Store { + return &noopStore{opts: NewOptions(opts...)} +} + +// Init initialize store +func (n *noopStore) Init(opts ...Option) error { for _, o := range opts { o(&n.opts) } return nil } -func (n *NoopStore) Options() Options { +// Options returns Options struct +func (n *noopStore) Options() Options { return n.opts } -func (n *NoopStore) String() string { +// String returns string representation +func (n *noopStore) String() string { return "noop" } -func (n *NoopStore) Read(ctx context.Context, key string, opts ...ReadOption) ([]*Record, error) { +// Read reads store value by key +func (n *noopStore) Read(ctx context.Context, key string, opts ...ReadOption) ([]*Record, error) { return []*Record{}, nil } -func (n *NoopStore) Write(ctx context.Context, r *Record, opts ...WriteOption) error { +// Write writes store record +func (n *noopStore) Write(ctx context.Context, r *Record, opts ...WriteOption) error { return nil } -func (n *NoopStore) Delete(ctx context.Context, key string, opts ...DeleteOption) error { +// Delete removes store value by key +func (n *noopStore) Delete(ctx context.Context, key string, opts ...DeleteOption) error { return nil } -func (n *NoopStore) List(ctx context.Context, opts ...ListOption) ([]string, error) { +// List lists store +func (n *noopStore) List(ctx context.Context, opts ...ListOption) ([]string, error) { return []string{}, nil } -func (n *NoopStore) Connect(ctx context.Context) error { +// Connect connects to store +func (n *noopStore) Connect(ctx context.Context) error { return nil } -func (n *NoopStore) Disconnect(ctx context.Context) error { +// Disconnect disconnects from store +func (n *noopStore) Disconnect(ctx context.Context) error { return nil } diff --git a/store/store.go b/store/store.go index 6f17e973..1560e34f 100644 --- a/store/store.go +++ b/store/store.go @@ -10,14 +10,16 @@ import ( var ( // ErrNotFound is returned when a key doesn't exist - ErrNotFound = errors.New("not found") - DefaultStore Store = &NoopStore{opts: NewOptions()} + ErrNotFound = errors.New("not found") + // DefaultStore is the global default store + DefaultStore Store = NewStore() ) // Store is a data storage interface type Store interface { // Init initialises the store. It must perform any required setup on the backing storage implementation and check that it is ready for use, returning any errors. Init(opts ...Option) error + // Connect is used when store needs to be connected Connect(ctx context.Context) error // Options allows you to view the current options. Options() Options diff --git a/tracer/options.go b/tracer/options.go index 11113d6b..c1f20d82 100644 --- a/tracer/options.go +++ b/tracer/options.go @@ -1,10 +1,12 @@ package tracer +// Options struct type Options struct { // Size is the size of ring buffer Size int } +// Option func type Option func(o *Options) type ReadOptions struct { @@ -12,9 +14,10 @@ type ReadOptions struct { Trace string } +// ReadOption func type ReadOption func(o *ReadOptions) -// Read the given trace +// ReadTracer read the given trace func ReadTrace(t string) ReadOption { return func(o *ReadOptions) { o.Trace = t diff --git a/util/http/options.go b/util/http/options.go index 58fc3847..322c4534 100644 --- a/util/http/options.go +++ b/util/http/options.go @@ -2,12 +2,15 @@ package http import "github.com/unistack-org/micro/v3/router" +// Options struct type Options struct { Router router.Router } +// Option func type Option func(*Options) +// WithRouter sets the router.Router option func WithRouter(r router.Router) Option { return func(o *Options) { o.Router = r diff --git a/util/signal/signal.go b/util/signal/signal.go deleted file mode 100644 index aa1d1e18..00000000 --- a/util/signal/signal.go +++ /dev/null @@ -1,13 +0,0 @@ -package signal - -import ( - "os" - "syscall" -) - -// ShutDownSignals returns all the signals that are being watched for to shut down services. -func Shutdown() []os.Signal { - return []os.Signal{ - syscall.SIGTERM, syscall.SIGINT, syscall.SIGQUIT, syscall.SIGKILL, - } -} diff --git a/util/test/test.go b/util/test/test.go deleted file mode 100644 index 2d7dcdc3..00000000 --- a/util/test/test.go +++ /dev/null @@ -1,47 +0,0 @@ -package test - -import ( - "github.com/unistack-org/micro/v3/registry" -) - -var ( - // mock registry data - Data = map[string][]*registry.Service{ - "foo": { - { - Name: "foo", - Version: "1.0.0", - Nodes: []*registry.Node{ - { - Id: "foo-1.0.0-123", - Address: "localhost:9999", - }, - { - Id: "foo-1.0.0-321", - Address: "localhost:9999", - }, - }, - }, - { - Name: "foo", - Version: "1.0.1", - Nodes: []*registry.Node{ - { - Id: "foo-1.0.1-321", - Address: "localhost:6666", - }, - }, - }, - { - Name: "foo", - Version: "1.0.3", - Nodes: []*registry.Node{ - { - Id: "foo-1.0.3-345", - Address: "localhost:8888", - }, - }, - }, - }, - } -)