noop impl (#32)
* improve logger usage * add noop client and server Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
@@ -9,6 +9,10 @@ type HandlerOptions struct {
|
||||
Metadata map[string]map[string]string
|
||||
}
|
||||
|
||||
func NewHandlerOptions() HandlerOptions {
|
||||
return HandlerOptions{}
|
||||
}
|
||||
|
||||
type SubscriberOption func(*SubscriberOptions)
|
||||
|
||||
type SubscriberOptions struct {
|
||||
@@ -20,6 +24,19 @@ type SubscriberOptions struct {
|
||||
Context context.Context
|
||||
}
|
||||
|
||||
func NewSubscriberOptions(opts ...SubscriberOption) SubscriberOptions {
|
||||
opt := SubscriberOptions{
|
||||
AutoAck: true,
|
||||
Context: context.Background(),
|
||||
}
|
||||
|
||||
for _, o := range opts {
|
||||
o(&opt)
|
||||
}
|
||||
|
||||
return opt
|
||||
}
|
||||
|
||||
// EndpointMetadata is a Handler option that allows metadata to be added to
|
||||
// individual endpoints.
|
||||
func EndpointMetadata(name string, md map[string]string) HandlerOption {
|
||||
@@ -44,18 +61,6 @@ func InternalSubscriber(b bool) SubscriberOption {
|
||||
o.Internal = b
|
||||
}
|
||||
}
|
||||
func NewSubscriberOptions(opts ...SubscriberOption) SubscriberOptions {
|
||||
opt := SubscriberOptions{
|
||||
AutoAck: true,
|
||||
Context: context.Background(),
|
||||
}
|
||||
|
||||
for _, o := range opts {
|
||||
o(&opt)
|
||||
}
|
||||
|
||||
return opt
|
||||
}
|
||||
|
||||
// DisableAutoAck will disable auto acking of messages
|
||||
// after they have been handled.
|
||||
|
108
server/noop.go
Normal file
108
server/noop.go
Normal file
@@ -0,0 +1,108 @@
|
||||
package server
|
||||
|
||||
import "github.com/unistack-org/micro/v3/registry"
|
||||
|
||||
type noopServer struct {
|
||||
h Handler
|
||||
opts Options
|
||||
}
|
||||
|
||||
type noopHandler struct {
|
||||
opts HandlerOptions
|
||||
h interface{}
|
||||
}
|
||||
|
||||
type noopSubscriber struct {
|
||||
topic string
|
||||
opts SubscriberOptions
|
||||
h interface{}
|
||||
}
|
||||
|
||||
func (n *noopSubscriber) Topic() string {
|
||||
return n.topic
|
||||
}
|
||||
|
||||
func (n *noopSubscriber) Subscriber() interface{} {
|
||||
return n.h
|
||||
}
|
||||
|
||||
func (n *noopSubscriber) Endpoints() []*registry.Endpoint {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (n *noopSubscriber) Options() SubscriberOptions {
|
||||
return n.opts
|
||||
}
|
||||
|
||||
func (n *noopHandler) Endpoints() []*registry.Endpoint {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (n *noopHandler) Handler() interface{} {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (n *noopHandler) Options() HandlerOptions {
|
||||
return n.opts
|
||||
}
|
||||
|
||||
func (n *noopHandler) Name() string {
|
||||
return "noop"
|
||||
}
|
||||
|
||||
func (n *noopServer) Handle(handler Handler) error {
|
||||
n.h = handler
|
||||
return nil
|
||||
}
|
||||
|
||||
func (n *noopServer) Subscribe(subscriber Subscriber) error {
|
||||
// n.s = handler
|
||||
return nil
|
||||
}
|
||||
|
||||
func (n *noopServer) NewHandler(h interface{}, opts ...HandlerOption) Handler {
|
||||
options := NewHandlerOptions()
|
||||
for _, o := range opts {
|
||||
o(&options)
|
||||
}
|
||||
return &noopHandler{opts: options, h: h}
|
||||
}
|
||||
|
||||
func (n *noopServer) NewSubscriber(topic string, h interface{}, opts ...SubscriberOption) Subscriber {
|
||||
options := NewSubscriberOptions()
|
||||
for _, o := range opts {
|
||||
o(&options)
|
||||
}
|
||||
return &noopSubscriber{topic: topic, opts: options, h: h}
|
||||
}
|
||||
|
||||
func (n *noopServer) Init(opts ...Option) error {
|
||||
for _, o := range opts {
|
||||
o(&n.opts)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (n *noopServer) Start() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (n *noopServer) Stop() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (n *noopServer) Options() Options {
|
||||
return n.opts
|
||||
}
|
||||
|
||||
func (n *noopServer) String() string {
|
||||
return "noop"
|
||||
}
|
||||
|
||||
func newServer(opts ...Option) Server {
|
||||
options := NewOptions()
|
||||
for _, o := range opts {
|
||||
o(&options)
|
||||
}
|
||||
return &noopServer{opts: options}
|
||||
}
|
@@ -51,39 +51,19 @@ type Options struct {
|
||||
Context context.Context
|
||||
}
|
||||
|
||||
func newOptions(opt ...Option) Options {
|
||||
opts := Options{
|
||||
func NewOptions() Options {
|
||||
return Options{
|
||||
Codecs: make(map[string]codec.NewCodec),
|
||||
Context: context.Background(),
|
||||
Metadata: map[string]string{},
|
||||
RegisterInterval: DefaultRegisterInterval,
|
||||
RegisterTTL: DefaultRegisterTTL,
|
||||
RegisterCheck: DefaultRegisterCheck,
|
||||
Address: DefaultAddress,
|
||||
Name: DefaultName,
|
||||
Version: DefaultVersion,
|
||||
Id: DefaultId,
|
||||
}
|
||||
|
||||
for _, o := range opt {
|
||||
o(&opts)
|
||||
}
|
||||
|
||||
if opts.RegisterCheck == nil {
|
||||
opts.RegisterCheck = DefaultRegisterCheck
|
||||
}
|
||||
|
||||
if len(opts.Address) == 0 {
|
||||
opts.Address = DefaultAddress
|
||||
}
|
||||
|
||||
if len(opts.Name) == 0 {
|
||||
opts.Name = DefaultName
|
||||
}
|
||||
|
||||
if len(opts.Id) == 0 {
|
||||
opts.Id = DefaultId
|
||||
}
|
||||
|
||||
if len(opts.Version) == 0 {
|
||||
opts.Version = DefaultVersion
|
||||
}
|
||||
|
||||
return opts
|
||||
}
|
||||
|
||||
// Server name
|
||||
|
@@ -11,7 +11,7 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
DefaultServer Server
|
||||
DefaultServer Server = newServer()
|
||||
)
|
||||
|
||||
// Server is a simple micro server abstraction
|
||||
@@ -138,7 +138,7 @@ type Option func(*Options)
|
||||
|
||||
var (
|
||||
DefaultAddress = ":0"
|
||||
DefaultName = "go.micro.server"
|
||||
DefaultName = "server"
|
||||
DefaultVersion = "latest"
|
||||
DefaultId = uuid.New().String()
|
||||
DefaultRegisterCheck = func(context.Context) error { return nil }
|
||||
|
Reference in New Issue
Block a user