Compare commits
9 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
558c6f4d7c | ||
d7dd6fbeb2 | |||
a00cf2c8d9 | |||
|
a3e8ab2492 | ||
06da500ef4 | |||
277f04ba19 | |||
|
470263ff5f | ||
b8232e02be | |||
|
f8c5e10c1d |
@ -1,5 +1,5 @@
|
||||
# Micro
|
||||
![Coverage](https://img.shields.io/badge/Coverage-45.1%25-yellow)
|
||||
![Coverage](https://img.shields.io/badge/Coverage-44.8%25-yellow)
|
||||
|
||||
Micro is a standard library for microservices.
|
||||
|
||||
|
@ -149,7 +149,7 @@ func (m *memory) Register(_ context.Context, s *register.Service, opts ...regist
|
||||
m.opts.Logger.Debug(m.opts.Context, fmt.Sprintf("Register added new service: %s, version: %s", s.Name, s.Version))
|
||||
}
|
||||
m.records[options.Namespace] = srvs
|
||||
go m.sendEvent(®ister.Result{Action: "create", Service: s})
|
||||
go m.sendEvent(®ister.Result{Action: register.EventCreate, Service: s})
|
||||
}
|
||||
|
||||
var addedNodes bool
|
||||
@ -185,7 +185,7 @@ func (m *memory) Register(_ context.Context, s *register.Service, opts ...regist
|
||||
if m.opts.Logger.V(logger.DebugLevel) {
|
||||
m.opts.Logger.Debug(m.opts.Context, fmt.Sprintf("Register added new node to service: %s, version: %s", s.Name, s.Version))
|
||||
}
|
||||
go m.sendEvent(®ister.Result{Action: "update", Service: s})
|
||||
go m.sendEvent(®ister.Result{Action: register.EventUpdate, Service: s})
|
||||
} else {
|
||||
// refresh TTL and timestamp
|
||||
for _, n := range s.Nodes {
|
||||
@ -238,7 +238,7 @@ func (m *memory) Deregister(ctx context.Context, s *register.Service, opts ...re
|
||||
// is cleanup
|
||||
if len(version.Nodes) > 0 {
|
||||
m.records[options.Namespace][s.Name][s.Version] = version
|
||||
go m.sendEvent(®ister.Result{Action: "update", Service: s})
|
||||
go m.sendEvent(®ister.Result{Action: register.EventUpdate, Service: s})
|
||||
return nil
|
||||
}
|
||||
|
||||
@ -246,7 +246,7 @@ func (m *memory) Deregister(ctx context.Context, s *register.Service, opts ...re
|
||||
// register and exit
|
||||
if len(versions) == 1 {
|
||||
delete(m.records[options.Namespace], s.Name)
|
||||
go m.sendEvent(®ister.Result{Action: "delete", Service: s})
|
||||
go m.sendEvent(®ister.Result{Action: register.EventDelete, Service: s})
|
||||
|
||||
if m.opts.Logger.V(logger.DebugLevel) {
|
||||
m.opts.Logger.Debug(m.opts.Context, fmt.Sprintf("Register removed service: %s", s.Name))
|
||||
@ -256,7 +256,7 @@ func (m *memory) Deregister(ctx context.Context, s *register.Service, opts ...re
|
||||
|
||||
// there are other versions of the service running, so only remove this version of it
|
||||
delete(m.records[options.Namespace][s.Name], s.Version)
|
||||
go m.sendEvent(®ister.Result{Action: "delete", Service: s})
|
||||
go m.sendEvent(®ister.Result{Action: register.EventDelete, Service: s})
|
||||
if m.opts.Logger.V(logger.DebugLevel) {
|
||||
m.opts.Logger.Debug(m.opts.Context, fmt.Sprintf("Register removed service: %s, version: %s", s.Name, s.Version))
|
||||
}
|
||||
|
@ -5,6 +5,7 @@ import (
|
||||
"crypto/tls"
|
||||
"time"
|
||||
|
||||
"go.unistack.org/micro/v3/codec"
|
||||
"go.unistack.org/micro/v3/logger"
|
||||
"go.unistack.org/micro/v3/meter"
|
||||
"go.unistack.org/micro/v3/tracer"
|
||||
@ -26,6 +27,8 @@ type Options struct {
|
||||
Name string
|
||||
// Addrs specifies register addrs
|
||||
Addrs []string
|
||||
// Codec used to marshal/unmarshal data in register
|
||||
Codec codec.Codec
|
||||
// Timeout specifies timeout
|
||||
Timeout time.Duration
|
||||
}
|
||||
@ -37,6 +40,7 @@ func NewOptions(opts ...Option) Options {
|
||||
Meter: meter.DefaultMeter,
|
||||
Tracer: tracer.DefaultTracer,
|
||||
Context: context.Background(),
|
||||
Codec: codec.NewCodec(),
|
||||
}
|
||||
for _, o := range opts {
|
||||
o(&options)
|
||||
@ -130,6 +134,7 @@ func NewLookupOptions(opts ...LookupOption) LookupOptions {
|
||||
|
||||
// ListOptions holds the list options for list method
|
||||
type ListOptions struct {
|
||||
// Context used to store additional options
|
||||
Context context.Context
|
||||
// Namespace to scope the request to
|
||||
Namespace string
|
||||
@ -300,3 +305,9 @@ func Name(n string) Option {
|
||||
o.Name = n
|
||||
}
|
||||
}
|
||||
|
||||
func Codec(c codec.Codec) Option {
|
||||
return func(o *Options) {
|
||||
o.Codec = c
|
||||
}
|
||||
}
|
||||
|
@ -15,31 +15,31 @@ type Watcher interface {
|
||||
// the watcher. Actions can be create, update, delete
|
||||
type Result struct {
|
||||
// Service holds register service
|
||||
Service *Service
|
||||
Service *Service `json:"service,omitempty"`
|
||||
// Action holds the action
|
||||
Action string
|
||||
Action EventType `json:"action,omitempty"`
|
||||
}
|
||||
|
||||
// EventType defines register event type
|
||||
type EventType int
|
||||
|
||||
const (
|
||||
// Create is emitted when a new service is registered
|
||||
Create EventType = iota
|
||||
// Delete is emitted when an existing service is deregistered
|
||||
Delete
|
||||
// Update is emitted when an existing service is updated
|
||||
Update
|
||||
// EventCreate is emitted when a new service is registered
|
||||
EventCreate EventType = iota
|
||||
// EventDelete is emitted when an existing service is deregistered
|
||||
EventDelete
|
||||
// EventUpdate is emitted when an existing service is updated
|
||||
EventUpdate
|
||||
)
|
||||
|
||||
// String returns human readable event type
|
||||
func (t EventType) String() string {
|
||||
switch t {
|
||||
case Create:
|
||||
case EventCreate:
|
||||
return "create"
|
||||
case Delete:
|
||||
case EventDelete:
|
||||
return "delete"
|
||||
case Update:
|
||||
case EventUpdate:
|
||||
return "update"
|
||||
default:
|
||||
return "unknown"
|
||||
@ -49,11 +49,11 @@ func (t EventType) String() string {
|
||||
// Event is register event
|
||||
type Event struct {
|
||||
// Timestamp is event timestamp
|
||||
Timestamp time.Time
|
||||
Timestamp time.Time `json:"timestamp,omitempty"`
|
||||
// Service is register service
|
||||
Service *Service
|
||||
Service *Service `json:"service,omitempty"`
|
||||
// ID is register id
|
||||
ID string
|
||||
ID string `json:"id,omitempty"`
|
||||
// Type defines type of event
|
||||
Type EventType
|
||||
Type EventType `json:"type,omitempty"`
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user