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:
@@ -1,12 +1,7 @@
|
||||
package registry
|
||||
|
||||
import (
|
||||
"net"
|
||||
|
||||
"github.com/unistack-org/micro/v3/metadata"
|
||||
"github.com/unistack-org/micro/v3/registry"
|
||||
"github.com/unistack-org/micro/v3/server"
|
||||
"github.com/unistack-org/micro/v3/util/addr"
|
||||
)
|
||||
|
||||
func addNodes(old, neu []*registry.Node) []*registry.Node {
|
||||
@@ -60,13 +55,13 @@ func delNodes(old, del []*registry.Node) []*registry.Node {
|
||||
// CopyService make a copy of service
|
||||
func CopyService(service *registry.Service) *registry.Service {
|
||||
// copy service
|
||||
s := new(registry.Service)
|
||||
s := ®istry.Service{}
|
||||
*s = *service
|
||||
|
||||
// copy nodes
|
||||
nodes := make([]*registry.Node, len(service.Nodes))
|
||||
for j, node := range service.Nodes {
|
||||
n := new(registry.Node)
|
||||
n := ®istry.Node{}
|
||||
*n = *node
|
||||
nodes[j] = n
|
||||
}
|
||||
@@ -75,7 +70,7 @@ func CopyService(service *registry.Service) *registry.Service {
|
||||
// copy endpoints
|
||||
eps := make([]*registry.Endpoint, len(service.Endpoints))
|
||||
for j, ep := range service.Endpoints {
|
||||
e := new(registry.Endpoint)
|
||||
e := ®istry.Endpoint{}
|
||||
*e = *ep
|
||||
eps[j] = e
|
||||
}
|
||||
@@ -100,7 +95,7 @@ func Merge(olist []*registry.Service, nlist []*registry.Service) []*registry.Ser
|
||||
var seen bool
|
||||
for _, o := range olist {
|
||||
if o.Version == n.Version {
|
||||
sp := new(registry.Service)
|
||||
sp := ®istry.Service{}
|
||||
// make copy
|
||||
*sp = *o
|
||||
// set nodes
|
||||
@@ -111,7 +106,7 @@ func Merge(olist []*registry.Service, nlist []*registry.Service) []*registry.Ser
|
||||
srv = append(srv, sp)
|
||||
break
|
||||
} else {
|
||||
sp := new(registry.Service)
|
||||
sp := ®istry.Service{}
|
||||
// make copy
|
||||
*sp = *o
|
||||
srv = append(srv, sp)
|
||||
@@ -129,7 +124,7 @@ func Remove(old, del []*registry.Service) []*registry.Service {
|
||||
var services []*registry.Service
|
||||
|
||||
for _, o := range old {
|
||||
srv := new(registry.Service)
|
||||
srv := ®istry.Service{}
|
||||
*srv = *o
|
||||
|
||||
var rem bool
|
||||
@@ -151,38 +146,3 @@ func Remove(old, del []*registry.Service) []*registry.Service {
|
||||
|
||||
return services
|
||||
}
|
||||
|
||||
func NewService(s server.Server) (*registry.Service, error) {
|
||||
opts := s.Options()
|
||||
|
||||
advt := opts.Address
|
||||
if len(opts.Advertise) > 0 {
|
||||
advt = opts.Advertise
|
||||
}
|
||||
|
||||
host, port, err := net.SplitHostPort(advt)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
addr, err := addr.Extract(host)
|
||||
if err != nil {
|
||||
addr = host
|
||||
}
|
||||
|
||||
node := ®istry.Node{
|
||||
Id: opts.Name + "-" + opts.Id,
|
||||
Address: net.JoinHostPort(addr, port),
|
||||
}
|
||||
node.Metadata = metadata.Copy(opts.Metadata)
|
||||
|
||||
node.Metadata["server"] = s.String()
|
||||
node.Metadata["broker"] = opts.Broker.String()
|
||||
node.Metadata["registry"] = opts.Registry.String()
|
||||
|
||||
return ®istry.Service{
|
||||
Name: opts.Name,
|
||||
Version: opts.Version,
|
||||
Nodes: []*registry.Node{node},
|
||||
}, nil
|
||||
}
|
||||
|
@@ -42,12 +42,20 @@ func NewSync(opts ...Option) Sync {
|
||||
return c
|
||||
}
|
||||
|
||||
func (c *syncStore) Connect(ctx context.Context) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *syncStore) Disconnect(ctx context.Context) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *syncStore) Close(ctx context.Context) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Init initialises the storeOptions
|
||||
func (c *syncStore) Init(ctx context.Context, opts ...store.Option) error {
|
||||
func (c *syncStore) Init(opts ...store.Option) error {
|
||||
for _, o := range opts {
|
||||
o(&c.storeOpts)
|
||||
}
|
||||
@@ -55,7 +63,7 @@ func (c *syncStore) Init(ctx context.Context, opts ...store.Option) error {
|
||||
return fmt.Errorf("the sync has no stores")
|
||||
}
|
||||
for _, s := range c.syncOpts.Stores {
|
||||
if err := s.Init(ctx); err != nil {
|
||||
if err := s.Init(); err != nil {
|
||||
return fmt.Errorf("Store %s failed to Init(): %w", s.String(), err)
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user