2019-01-14 18:27:25 +03:00
|
|
|
// Package memory provides an in-memory registry
|
|
|
|
package memory
|
2015-12-09 22:23:16 +03:00
|
|
|
|
|
|
|
import (
|
2019-01-18 20:29:17 +03:00
|
|
|
"context"
|
2019-01-09 09:24:12 +03:00
|
|
|
"sync"
|
2019-01-18 20:29:17 +03:00
|
|
|
"time"
|
2019-01-09 09:24:12 +03:00
|
|
|
|
2019-01-18 20:29:17 +03:00
|
|
|
"github.com/google/uuid"
|
2020-03-11 20:55:39 +03:00
|
|
|
"github.com/micro/go-micro/v2/logger"
|
2020-01-30 14:39:00 +03:00
|
|
|
"github.com/micro/go-micro/v2/registry"
|
2015-12-09 22:23:16 +03:00
|
|
|
)
|
|
|
|
|
2019-09-25 03:57:37 +03:00
|
|
|
var (
|
2019-09-25 20:19:18 +03:00
|
|
|
sendEventTime = 10 * time.Millisecond
|
2019-11-16 13:55:11 +03:00
|
|
|
ttlPruneTime = time.Second
|
2020-06-18 14:39:19 +03:00
|
|
|
defaultDomain = "micro"
|
2019-09-25 03:57:37 +03:00
|
|
|
)
|
|
|
|
|
2019-09-25 18:20:42 +03:00
|
|
|
type node struct {
|
2019-11-16 13:55:11 +03:00
|
|
|
*registry.Node
|
|
|
|
TTL time.Duration
|
|
|
|
LastSeen time.Time
|
|
|
|
}
|
|
|
|
|
|
|
|
type record struct {
|
|
|
|
Name string
|
|
|
|
Version string
|
|
|
|
Metadata map[string]string
|
|
|
|
Nodes map[string]*node
|
|
|
|
Endpoints []*registry.Endpoint
|
2019-09-25 18:20:42 +03:00
|
|
|
}
|
|
|
|
|
2019-01-14 18:27:25 +03:00
|
|
|
type Registry struct {
|
2019-01-18 20:29:17 +03:00
|
|
|
options registry.Options
|
|
|
|
|
2019-01-09 09:24:12 +03:00
|
|
|
sync.RWMutex
|
2020-06-18 14:39:19 +03:00
|
|
|
// records is a KV map with domain name as the key and a services map as the value
|
|
|
|
records map[string]services
|
2019-11-16 13:55:11 +03:00
|
|
|
watchers map[string]*Watcher
|
2016-02-26 03:09:06 +03:00
|
|
|
}
|
2015-12-09 22:23:16 +03:00
|
|
|
|
2020-06-18 14:39:19 +03:00
|
|
|
// services is a KV map with service name as the key and a map of records as the value
|
|
|
|
type services map[string]map[string]*record
|
|
|
|
|
|
|
|
// NewRegistry returns an initialized in-memory registry
|
2019-09-25 03:57:37 +03:00
|
|
|
func NewRegistry(opts ...registry.Option) registry.Registry {
|
|
|
|
options := registry.Options{
|
|
|
|
Context: context.Background(),
|
|
|
|
}
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
2016-03-14 22:15:42 +03:00
|
|
|
|
2020-06-18 14:39:19 +03:00
|
|
|
// records can be passed for testing purposes
|
2019-11-16 13:55:11 +03:00
|
|
|
records := getServiceRecords(options.Context)
|
|
|
|
if records == nil {
|
2020-06-18 14:39:19 +03:00
|
|
|
records = make(services)
|
2019-09-25 03:57:37 +03:00
|
|
|
}
|
|
|
|
|
2019-09-25 20:19:18 +03:00
|
|
|
reg := &Registry{
|
2019-09-25 03:57:37 +03:00
|
|
|
options: options,
|
2020-06-18 14:39:19 +03:00
|
|
|
records: map[string]services{defaultDomain: records},
|
2019-11-16 13:55:11 +03:00
|
|
|
watchers: make(map[string]*Watcher),
|
2019-09-25 03:57:37 +03:00
|
|
|
}
|
2019-09-25 20:19:18 +03:00
|
|
|
|
|
|
|
go reg.ttlPrune()
|
|
|
|
|
|
|
|
return reg
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Registry) ttlPrune() {
|
|
|
|
prune := time.NewTicker(ttlPruneTime)
|
|
|
|
defer prune.Stop()
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-prune.C:
|
|
|
|
m.Lock()
|
2020-06-18 14:39:19 +03:00
|
|
|
for domain, services := range m.records {
|
|
|
|
for service, versions := range services {
|
|
|
|
for version, record := range versions {
|
|
|
|
for id, n := range record.Nodes {
|
|
|
|
if n.TTL != 0 && time.Since(n.LastSeen) > n.TTL {
|
|
|
|
if logger.V(logger.DebugLevel, logger.DefaultLogger) {
|
|
|
|
logger.Debugf("Registry TTL expired for node %s of service %s", n.Id, service)
|
|
|
|
}
|
|
|
|
delete(m.records[domain][service][version].Nodes, id)
|
2020-03-11 20:55:39 +03:00
|
|
|
}
|
2019-09-25 20:19:18 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
m.Unlock()
|
|
|
|
}
|
|
|
|
}
|
2019-09-25 03:57:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Registry) sendEvent(r *registry.Result) {
|
2019-01-18 20:29:17 +03:00
|
|
|
m.RLock()
|
2019-12-03 17:49:24 +03:00
|
|
|
watchers := make([]*Watcher, 0, len(m.watchers))
|
2019-11-16 13:55:11 +03:00
|
|
|
for _, w := range m.watchers {
|
2019-01-18 20:29:17 +03:00
|
|
|
watchers = append(watchers, w)
|
|
|
|
}
|
|
|
|
m.RUnlock()
|
|
|
|
|
|
|
|
for _, w := range watchers {
|
|
|
|
select {
|
|
|
|
case <-w.exit:
|
|
|
|
m.Lock()
|
2019-11-16 13:55:11 +03:00
|
|
|
delete(m.watchers, w.id)
|
2019-01-18 20:29:17 +03:00
|
|
|
m.Unlock()
|
|
|
|
default:
|
|
|
|
select {
|
|
|
|
case w.res <- r:
|
2019-09-25 20:19:18 +03:00
|
|
|
case <-time.After(sendEventTime):
|
2019-01-18 20:29:17 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Registry) Init(opts ...registry.Option) error {
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&m.options)
|
|
|
|
}
|
|
|
|
|
|
|
|
// add services
|
2019-01-09 09:24:12 +03:00
|
|
|
m.Lock()
|
2019-11-16 13:55:11 +03:00
|
|
|
defer m.Unlock()
|
|
|
|
|
2020-06-18 14:39:19 +03:00
|
|
|
// get the existing services from the records
|
|
|
|
srvs, ok := m.records[defaultDomain]
|
|
|
|
if !ok {
|
|
|
|
srvs = make(services)
|
|
|
|
}
|
|
|
|
|
|
|
|
// loop through the services and if it doesn't yet exist, add it to the slice. This is used for
|
|
|
|
// testing purposes.
|
|
|
|
for name, record := range getServiceRecords(m.options.Context) {
|
|
|
|
if _, ok := srvs[name]; !ok {
|
|
|
|
srvs[name] = record
|
2019-11-16 13:55:11 +03:00
|
|
|
continue
|
|
|
|
}
|
2020-06-18 14:39:19 +03:00
|
|
|
|
2019-11-16 13:55:11 +03:00
|
|
|
for version, r := range record {
|
2020-06-18 14:39:19 +03:00
|
|
|
if _, ok := srvs[name][version]; !ok {
|
|
|
|
srvs[name][version] = r
|
2019-11-16 13:55:11 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
2019-01-18 20:29:17 +03:00
|
|
|
}
|
2019-11-16 13:55:11 +03:00
|
|
|
|
2020-06-18 14:39:19 +03:00
|
|
|
// set the services in the registry
|
|
|
|
m.records[defaultDomain] = srvs
|
2019-01-18 20:29:17 +03:00
|
|
|
return nil
|
|
|
|
}
|
2019-01-09 09:24:12 +03:00
|
|
|
|
2019-01-18 20:29:17 +03:00
|
|
|
func (m *Registry) Options() registry.Options {
|
|
|
|
return m.options
|
|
|
|
}
|
|
|
|
|
2019-01-14 18:27:25 +03:00
|
|
|
func (m *Registry) Register(s *registry.Service, opts ...registry.RegisterOption) error {
|
2019-01-18 20:29:17 +03:00
|
|
|
m.Lock()
|
2019-09-25 03:57:37 +03:00
|
|
|
defer m.Unlock()
|
|
|
|
|
2020-06-18 14:39:19 +03:00
|
|
|
// parse the options, fallback to the default domain
|
2019-09-25 18:20:42 +03:00
|
|
|
var options registry.RegisterOptions
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
2020-06-18 14:39:19 +03:00
|
|
|
if len(options.Domain) == 0 {
|
|
|
|
options.Domain = defaultDomain
|
|
|
|
}
|
2019-09-25 18:20:42 +03:00
|
|
|
|
2020-06-18 14:39:19 +03:00
|
|
|
// get the services for this domain from the registry
|
|
|
|
srvs, ok := m.records[options.Domain]
|
|
|
|
if !ok {
|
|
|
|
srvs = make(services)
|
|
|
|
}
|
2019-11-16 13:55:11 +03:00
|
|
|
|
2020-06-18 14:39:19 +03:00
|
|
|
// ensure the service name exists
|
|
|
|
r := serviceToRecord(s, options.TTL)
|
|
|
|
if _, ok := srvs[s.Name]; !ok {
|
|
|
|
srvs[s.Name] = make(map[string]*record)
|
2019-11-16 13:55:11 +03:00
|
|
|
}
|
|
|
|
|
2020-06-18 14:39:19 +03:00
|
|
|
if _, ok := srvs[s.Name][s.Version]; !ok {
|
|
|
|
srvs[s.Name][s.Version] = r
|
2020-03-11 20:55:39 +03:00
|
|
|
if logger.V(logger.DebugLevel, logger.DefaultLogger) {
|
|
|
|
logger.Debugf("Registry added new service: %s, version: %s", s.Name, s.Version)
|
|
|
|
}
|
2020-06-18 14:39:19 +03:00
|
|
|
m.records[options.Domain] = srvs
|
2019-09-25 03:57:37 +03:00
|
|
|
go m.sendEvent(®istry.Result{Action: "update", Service: s})
|
2019-11-16 13:55:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
addedNodes := false
|
|
|
|
for _, n := range s.Nodes {
|
2020-06-18 14:39:19 +03:00
|
|
|
if _, ok := srvs[s.Name][s.Version].Nodes[n.Id]; !ok {
|
2019-11-16 13:55:11 +03:00
|
|
|
addedNodes = true
|
|
|
|
metadata := make(map[string]string)
|
|
|
|
for k, v := range n.Metadata {
|
|
|
|
metadata[k] = v
|
2020-06-18 14:39:19 +03:00
|
|
|
srvs[s.Name][s.Version].Nodes[n.Id] = &node{
|
2019-11-16 13:55:11 +03:00
|
|
|
Node: ®istry.Node{
|
|
|
|
Id: n.Id,
|
|
|
|
Address: n.Address,
|
|
|
|
Metadata: metadata,
|
|
|
|
},
|
|
|
|
TTL: options.TTL,
|
|
|
|
LastSeen: time.Now(),
|
2019-09-25 20:19:18 +03:00
|
|
|
}
|
|
|
|
}
|
2019-09-25 03:57:37 +03:00
|
|
|
}
|
2019-07-04 00:15:44 +03:00
|
|
|
}
|
|
|
|
|
2019-11-16 13:55:11 +03:00
|
|
|
if addedNodes {
|
2020-03-11 20:55:39 +03:00
|
|
|
if logger.V(logger.DebugLevel, logger.DefaultLogger) {
|
|
|
|
logger.Debugf("Registry added new node to service: %s, version: %s", s.Name, s.Version)
|
|
|
|
}
|
2019-11-16 13:55:11 +03:00
|
|
|
go m.sendEvent(®istry.Result{Action: "update", Service: s})
|
2020-06-18 14:39:19 +03:00
|
|
|
} else {
|
|
|
|
// refresh TTL and timestamp
|
|
|
|
for _, n := range s.Nodes {
|
|
|
|
if logger.V(logger.DebugLevel, logger.DefaultLogger) {
|
|
|
|
logger.Debugf("Updated registration for service: %s, version: %s", s.Name, s.Version)
|
|
|
|
}
|
|
|
|
srvs[s.Name][s.Version].Nodes[n.Id].TTL = options.TTL
|
|
|
|
srvs[s.Name][s.Version].Nodes[n.Id].LastSeen = time.Now()
|
2020-03-11 20:55:39 +03:00
|
|
|
}
|
2019-11-16 13:55:11 +03:00
|
|
|
}
|
|
|
|
|
2020-06-18 14:39:19 +03:00
|
|
|
m.records[options.Domain] = srvs
|
2015-12-09 22:23:16 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-04-14 14:32:59 +03:00
|
|
|
func (m *Registry) Deregister(s *registry.Service, opts ...registry.DeregisterOption) error {
|
2019-01-18 20:29:17 +03:00
|
|
|
m.Lock()
|
2019-09-25 03:57:37 +03:00
|
|
|
defer m.Unlock()
|
|
|
|
|
2020-06-18 14:39:19 +03:00
|
|
|
// parse the options, fallback to the default domain
|
|
|
|
var options registry.DeregisterOptions
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
if len(options.Domain) == 0 {
|
|
|
|
options.Domain = defaultDomain
|
|
|
|
}
|
|
|
|
|
|
|
|
// if the domain doesn't exist, there is nothing to deregister
|
|
|
|
services, ok := m.records[options.Domain]
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// if no services with this name and version exist, there is nothing to deregister
|
|
|
|
versions, ok := services[s.Name]
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
version, ok := versions[s.Version]
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// deregister all of the service nodes from this version
|
|
|
|
for _, n := range s.Nodes {
|
|
|
|
if _, ok := version.Nodes[n.Id]; ok {
|
2020-03-11 20:55:39 +03:00
|
|
|
if logger.V(logger.DebugLevel, logger.DefaultLogger) {
|
2020-06-18 14:39:19 +03:00
|
|
|
logger.Debugf("Registry removed node from service: %s, version: %s", s.Name, s.Version)
|
2020-03-11 20:55:39 +03:00
|
|
|
}
|
2020-06-18 14:39:19 +03:00
|
|
|
delete(version.Nodes, n.Id)
|
2019-11-16 13:55:11 +03:00
|
|
|
}
|
2020-06-18 14:39:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// if the nodes not empty, we replace the version in the store and exist, the rest of the logic
|
|
|
|
// is cleanup
|
|
|
|
if len(version.Nodes) > 0 {
|
|
|
|
m.records[options.Domain][s.Name][s.Version] = version
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// if this version was the only version of the service, we can remove the whole service from the
|
|
|
|
// registry and exit
|
|
|
|
if len(versions) == 1 {
|
|
|
|
delete(m.records[options.Domain], s.Name)
|
2019-11-16 13:55:11 +03:00
|
|
|
go m.sendEvent(®istry.Result{Action: "delete", Service: s})
|
2020-06-18 14:39:19 +03:00
|
|
|
|
|
|
|
if logger.V(logger.DebugLevel, logger.DefaultLogger) {
|
|
|
|
logger.Debugf("Registry removed service: %s", s.Name)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// there are other versions of the service running, so only remove this version of it
|
|
|
|
delete(m.records[options.Domain][s.Name], s.Version)
|
|
|
|
if logger.V(logger.DebugLevel, logger.DefaultLogger) {
|
|
|
|
logger.Debugf("Registry removed service: %s, version: %s", s.Name, s.Version)
|
2019-07-04 00:15:44 +03:00
|
|
|
}
|
|
|
|
|
2015-12-09 22:23:16 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-04-14 14:32:59 +03:00
|
|
|
func (m *Registry) GetService(name string, opts ...registry.GetOption) ([]*registry.Service, error) {
|
2020-06-18 14:39:19 +03:00
|
|
|
// parse the options, fallback to the default domain
|
|
|
|
var options registry.GetOptions
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
if len(options.Domain) == 0 {
|
|
|
|
options.Domain = defaultDomain
|
|
|
|
}
|
|
|
|
|
|
|
|
// if it's a wildcard domain, return from all domains
|
|
|
|
if options.Domain == registry.WildcardDomain {
|
|
|
|
m.RLock()
|
|
|
|
recs := m.records
|
|
|
|
m.RUnlock()
|
|
|
|
|
|
|
|
var services []*registry.Service
|
|
|
|
for domain := range recs {
|
|
|
|
srvs, err := m.GetService(name, append(opts, registry.GetDomain(domain))...)
|
|
|
|
if err == registry.ErrNotFound {
|
|
|
|
continue
|
|
|
|
} else if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
services = append(services, srvs...)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(services) == 0 {
|
|
|
|
return nil, registry.ErrNotFound
|
|
|
|
}
|
|
|
|
return services, nil
|
|
|
|
}
|
|
|
|
|
2019-11-16 13:55:11 +03:00
|
|
|
m.RLock()
|
|
|
|
defer m.RUnlock()
|
|
|
|
|
2020-06-18 14:39:19 +03:00
|
|
|
// check the domain exists
|
|
|
|
services, ok := m.records[options.Domain]
|
2019-11-16 13:55:11 +03:00
|
|
|
if !ok {
|
|
|
|
return nil, registry.ErrNotFound
|
|
|
|
}
|
|
|
|
|
2020-06-18 14:39:19 +03:00
|
|
|
// check the service exists
|
|
|
|
versions, ok := services[name]
|
|
|
|
if !ok || len(versions) == 0 {
|
|
|
|
return nil, registry.ErrNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
// serialize the response
|
|
|
|
result := make([]*registry.Service, len(versions))
|
2019-11-16 13:55:11 +03:00
|
|
|
i := 0
|
2020-06-18 14:39:19 +03:00
|
|
|
for _, r := range versions {
|
|
|
|
result[i] = recordToService(r, options.Domain)
|
2019-11-16 13:55:11 +03:00
|
|
|
i++
|
|
|
|
}
|
2020-06-18 14:39:19 +03:00
|
|
|
return result, nil
|
2019-11-16 13:55:11 +03:00
|
|
|
}
|
|
|
|
|
2020-04-14 14:32:59 +03:00
|
|
|
func (m *Registry) ListServices(opts ...registry.ListOption) ([]*registry.Service, error) {
|
2020-06-18 14:39:19 +03:00
|
|
|
// parse the options, fallback to the default domain
|
|
|
|
var options registry.ListOptions
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
if len(options.Domain) == 0 {
|
|
|
|
options.Domain = defaultDomain
|
|
|
|
}
|
|
|
|
|
|
|
|
// if it's a wildcard domain, list from all domains
|
|
|
|
if options.Domain == registry.WildcardDomain {
|
|
|
|
m.RLock()
|
|
|
|
recs := m.records
|
|
|
|
m.RUnlock()
|
|
|
|
|
|
|
|
var services []*registry.Service
|
|
|
|
for domain := range recs {
|
|
|
|
srvs, err := m.ListServices(append(opts, registry.ListDomain(domain))...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
services = append(services, srvs...)
|
|
|
|
}
|
|
|
|
|
|
|
|
return services, nil
|
|
|
|
}
|
|
|
|
|
2019-11-16 13:55:11 +03:00
|
|
|
m.RLock()
|
|
|
|
defer m.RUnlock()
|
|
|
|
|
2020-06-18 14:39:19 +03:00
|
|
|
// ensure the domain exists
|
|
|
|
services, ok := m.records[options.Domain]
|
|
|
|
if !ok {
|
|
|
|
return make([]*registry.Service, 0), nil
|
2019-11-16 13:55:11 +03:00
|
|
|
}
|
|
|
|
|
2020-06-18 14:39:19 +03:00
|
|
|
// serialize the result, each version counts as an individual service
|
|
|
|
var result []*registry.Service
|
|
|
|
for domain, service := range services {
|
|
|
|
for _, version := range service {
|
|
|
|
result = append(result, recordToService(version, domain))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result, nil
|
2019-11-16 13:55:11 +03:00
|
|
|
}
|
|
|
|
|
2019-01-14 18:27:25 +03:00
|
|
|
func (m *Registry) Watch(opts ...registry.WatchOption) (registry.Watcher, error) {
|
2020-06-18 14:39:19 +03:00
|
|
|
// parse the options, fallback to the default domain
|
2019-01-18 20:29:17 +03:00
|
|
|
var wo registry.WatchOptions
|
2018-02-19 20:12:37 +03:00
|
|
|
for _, o := range opts {
|
2019-01-18 20:29:17 +03:00
|
|
|
o(&wo)
|
|
|
|
}
|
2020-06-18 14:39:19 +03:00
|
|
|
if len(wo.Domain) == 0 {
|
|
|
|
wo.Domain = defaultDomain
|
|
|
|
}
|
2019-01-18 20:29:17 +03:00
|
|
|
|
2020-06-18 14:39:19 +03:00
|
|
|
// construct the watcher
|
2019-01-18 20:29:17 +03:00
|
|
|
w := &Watcher{
|
|
|
|
exit: make(chan bool),
|
|
|
|
res: make(chan *registry.Result),
|
|
|
|
id: uuid.New().String(),
|
|
|
|
wo: wo,
|
2018-02-19 20:12:37 +03:00
|
|
|
}
|
2019-01-18 20:29:17 +03:00
|
|
|
|
|
|
|
m.Lock()
|
2019-11-16 13:55:11 +03:00
|
|
|
m.watchers[w.id] = w
|
2019-01-18 20:29:17 +03:00
|
|
|
m.Unlock()
|
2019-11-16 13:55:11 +03:00
|
|
|
|
2019-01-18 20:29:17 +03:00
|
|
|
return w, nil
|
2015-12-09 22:23:16 +03:00
|
|
|
}
|
|
|
|
|
2019-01-14 18:27:25 +03:00
|
|
|
func (m *Registry) String() string {
|
|
|
|
return "memory"
|
2015-12-20 00:56:14 +03:00
|
|
|
}
|