2019-01-14 15:27:25 +00:00
|
|
|
// Package memory provides an in-memory registry
|
|
|
|
package memory
|
2015-12-09 19:23:16 +00:00
|
|
|
|
|
|
|
import (
|
2019-01-18 17:29:17 +00:00
|
|
|
"context"
|
2019-01-09 14:24:12 +08:00
|
|
|
"sync"
|
2019-01-18 17:29:17 +00:00
|
|
|
"time"
|
2019-01-09 14:24:12 +08:00
|
|
|
|
2019-01-18 17:29:17 +00:00
|
|
|
"github.com/google/uuid"
|
2020-01-30 14:39:00 +03:00
|
|
|
"github.com/micro/go-micro/v2/registry"
|
|
|
|
"github.com/micro/go-micro/v2/util/log"
|
2015-12-09 19:23:16 +00:00
|
|
|
)
|
|
|
|
|
2019-09-25 01:57:37 +01:00
|
|
|
var (
|
2019-09-25 18:19:18 +01:00
|
|
|
sendEventTime = 10 * time.Millisecond
|
2019-11-16 02:55:11 -08:00
|
|
|
ttlPruneTime = time.Second
|
2019-09-25 01:57:37 +01:00
|
|
|
)
|
|
|
|
|
2019-09-25 16:20:42 +01:00
|
|
|
type node struct {
|
2019-11-16 02:55:11 -08: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 16:20:42 +01:00
|
|
|
}
|
|
|
|
|
2019-01-14 15:27:25 +00:00
|
|
|
type Registry struct {
|
2019-01-18 17:29:17 +00:00
|
|
|
options registry.Options
|
|
|
|
|
2019-01-09 14:24:12 +08:00
|
|
|
sync.RWMutex
|
2019-11-16 02:55:11 -08:00
|
|
|
records map[string]map[string]*record
|
|
|
|
watchers map[string]*Watcher
|
2016-02-26 00:09:06 +00:00
|
|
|
}
|
2015-12-09 19:23:16 +00:00
|
|
|
|
2019-09-25 01:57:37 +01:00
|
|
|
func NewRegistry(opts ...registry.Option) registry.Registry {
|
|
|
|
options := registry.Options{
|
|
|
|
Context: context.Background(),
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
2016-03-14 19:15:42 +00:00
|
|
|
|
2019-11-16 02:55:11 -08:00
|
|
|
records := getServiceRecords(options.Context)
|
|
|
|
if records == nil {
|
|
|
|
records = make(map[string]map[string]*record)
|
2019-09-25 01:57:37 +01:00
|
|
|
}
|
|
|
|
|
2019-09-25 18:19:18 +01:00
|
|
|
reg := &Registry{
|
2019-09-25 01:57:37 +01:00
|
|
|
options: options,
|
2019-11-16 02:55:11 -08:00
|
|
|
records: records,
|
|
|
|
watchers: make(map[string]*Watcher),
|
2019-09-25 01:57:37 +01:00
|
|
|
}
|
2019-09-25 18:19:18 +01:00
|
|
|
|
|
|
|
go reg.ttlPrune()
|
|
|
|
|
|
|
|
return reg
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Registry) ttlPrune() {
|
|
|
|
prune := time.NewTicker(ttlPruneTime)
|
|
|
|
defer prune.Stop()
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-prune.C:
|
|
|
|
m.Lock()
|
2019-11-16 02:55:11 -08:00
|
|
|
for name, records := range m.records {
|
|
|
|
for version, record := range records {
|
|
|
|
for id, n := range record.Nodes {
|
|
|
|
if n.TTL != 0 && time.Since(n.LastSeen) > n.TTL {
|
|
|
|
log.Debugf("Registry TTL expired for node %s of service %s", n.Id, name)
|
|
|
|
delete(m.records[name][version].Nodes, id)
|
2019-09-25 18:19:18 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
m.Unlock()
|
|
|
|
}
|
|
|
|
}
|
2019-09-25 01:57:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Registry) sendEvent(r *registry.Result) {
|
2019-01-18 17:29:17 +00:00
|
|
|
m.RLock()
|
2019-12-03 14:49:24 +00:00
|
|
|
watchers := make([]*Watcher, 0, len(m.watchers))
|
2019-11-16 02:55:11 -08:00
|
|
|
for _, w := range m.watchers {
|
2019-01-18 17:29:17 +00:00
|
|
|
watchers = append(watchers, w)
|
|
|
|
}
|
|
|
|
m.RUnlock()
|
|
|
|
|
|
|
|
for _, w := range watchers {
|
|
|
|
select {
|
|
|
|
case <-w.exit:
|
|
|
|
m.Lock()
|
2019-11-16 02:55:11 -08:00
|
|
|
delete(m.watchers, w.id)
|
2019-01-18 17:29:17 +00:00
|
|
|
m.Unlock()
|
|
|
|
default:
|
|
|
|
select {
|
|
|
|
case w.res <- r:
|
2019-09-25 18:19:18 +01:00
|
|
|
case <-time.After(sendEventTime):
|
2019-01-18 17:29:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Registry) Init(opts ...registry.Option) error {
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&m.options)
|
|
|
|
}
|
|
|
|
|
|
|
|
// add services
|
2019-01-09 14:24:12 +08:00
|
|
|
m.Lock()
|
2019-11-16 02:55:11 -08:00
|
|
|
defer m.Unlock()
|
|
|
|
|
|
|
|
records := getServiceRecords(m.options.Context)
|
|
|
|
for name, record := range records {
|
|
|
|
// add a whole new service including all of its versions
|
|
|
|
if _, ok := m.records[name]; !ok {
|
|
|
|
m.records[name] = record
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// add the versions of the service we dont track yet
|
|
|
|
for version, r := range record {
|
|
|
|
if _, ok := m.records[name][version]; !ok {
|
|
|
|
m.records[name][version] = r
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
2019-01-18 17:29:17 +00:00
|
|
|
}
|
2019-11-16 02:55:11 -08:00
|
|
|
|
2019-01-18 17:29:17 +00:00
|
|
|
return nil
|
|
|
|
}
|
2019-01-09 14:24:12 +08:00
|
|
|
|
2019-01-18 17:29:17 +00:00
|
|
|
func (m *Registry) Options() registry.Options {
|
|
|
|
return m.options
|
|
|
|
}
|
|
|
|
|
2019-01-14 15:27:25 +00:00
|
|
|
func (m *Registry) Register(s *registry.Service, opts ...registry.RegisterOption) error {
|
2019-01-18 17:29:17 +00:00
|
|
|
m.Lock()
|
2019-09-25 01:57:37 +01:00
|
|
|
defer m.Unlock()
|
|
|
|
|
2019-09-25 16:20:42 +01:00
|
|
|
var options registry.RegisterOptions
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
|
2019-11-16 02:55:11 -08:00
|
|
|
r := serviceToRecord(s, options.TTL)
|
|
|
|
|
|
|
|
if _, ok := m.records[s.Name]; !ok {
|
|
|
|
m.records[s.Name] = make(map[string]*record)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := m.records[s.Name][s.Version]; !ok {
|
|
|
|
m.records[s.Name][s.Version] = r
|
|
|
|
log.Debugf("Registry added new service: %s, version: %s", s.Name, s.Version)
|
2019-09-25 01:57:37 +01:00
|
|
|
go m.sendEvent(®istry.Result{Action: "update", Service: s})
|
2019-09-30 12:48:06 +01:00
|
|
|
return nil
|
2019-11-16 02:55:11 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
addedNodes := false
|
|
|
|
for _, n := range s.Nodes {
|
|
|
|
if _, ok := m.records[s.Name][s.Version].Nodes[n.Id]; !ok {
|
|
|
|
addedNodes = true
|
|
|
|
metadata := make(map[string]string)
|
|
|
|
for k, v := range n.Metadata {
|
|
|
|
metadata[k] = v
|
|
|
|
m.records[s.Name][s.Version].Nodes[n.Id] = &node{
|
|
|
|
Node: ®istry.Node{
|
|
|
|
Id: n.Id,
|
|
|
|
Address: n.Address,
|
|
|
|
Metadata: metadata,
|
|
|
|
},
|
|
|
|
TTL: options.TTL,
|
|
|
|
LastSeen: time.Now(),
|
2019-09-25 18:19:18 +01:00
|
|
|
}
|
|
|
|
}
|
2019-09-25 01:57:37 +01:00
|
|
|
}
|
2019-07-04 00:15:44 +03:00
|
|
|
}
|
|
|
|
|
2019-11-16 02:55:11 -08:00
|
|
|
if addedNodes {
|
|
|
|
log.Debugf("Registry added new node to service: %s, version: %s", s.Name, s.Version)
|
|
|
|
go m.sendEvent(®istry.Result{Action: "update", Service: s})
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// refresh TTL and timestamp
|
|
|
|
for _, n := range s.Nodes {
|
|
|
|
log.Debugf("Updated registration for service: %s, version: %s", s.Name, s.Version)
|
|
|
|
m.records[s.Name][s.Version].Nodes[n.Id].TTL = options.TTL
|
|
|
|
m.records[s.Name][s.Version].Nodes[n.Id].LastSeen = time.Now()
|
|
|
|
}
|
|
|
|
|
2015-12-09 19:23:16 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-01-14 15:27:25 +00:00
|
|
|
func (m *Registry) Deregister(s *registry.Service) error {
|
2019-01-18 17:29:17 +00:00
|
|
|
m.Lock()
|
2019-09-25 01:57:37 +01:00
|
|
|
defer m.Unlock()
|
|
|
|
|
2019-11-16 02:55:11 -08:00
|
|
|
if _, ok := m.records[s.Name]; ok {
|
|
|
|
if _, ok := m.records[s.Name][s.Version]; ok {
|
|
|
|
for _, n := range s.Nodes {
|
|
|
|
if _, ok := m.records[s.Name][s.Version].Nodes[n.Id]; ok {
|
|
|
|
log.Debugf("Registry removed node from service: %s, version: %s", s.Name, s.Version)
|
|
|
|
delete(m.records[s.Name][s.Version].Nodes, n.Id)
|
2019-09-25 16:20:42 +01:00
|
|
|
}
|
|
|
|
}
|
2019-11-16 02:55:11 -08:00
|
|
|
if len(m.records[s.Name][s.Version].Nodes) == 0 {
|
|
|
|
delete(m.records[s.Name], s.Version)
|
|
|
|
log.Debugf("Registry removed service: %s, version: %s", s.Name, s.Version)
|
2019-09-25 16:20:42 +01:00
|
|
|
}
|
2019-07-04 00:15:44 +03:00
|
|
|
}
|
2019-11-16 02:55:11 -08:00
|
|
|
if len(m.records[s.Name]) == 0 {
|
|
|
|
delete(m.records, s.Name)
|
|
|
|
log.Debugf("Registry removed service: %s", s.Name)
|
|
|
|
}
|
|
|
|
go m.sendEvent(®istry.Result{Action: "delete", Service: s})
|
2019-07-04 00:15:44 +03:00
|
|
|
}
|
|
|
|
|
2015-12-09 19:23:16 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-11-16 02:55:11 -08:00
|
|
|
func (m *Registry) GetService(name string) ([]*registry.Service, error) {
|
|
|
|
m.RLock()
|
|
|
|
defer m.RUnlock()
|
|
|
|
|
|
|
|
records, ok := m.records[name]
|
|
|
|
if !ok {
|
|
|
|
return nil, registry.ErrNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
services := make([]*registry.Service, len(m.records[name]))
|
|
|
|
i := 0
|
|
|
|
for _, record := range records {
|
|
|
|
services[i] = recordToService(record)
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
|
|
|
|
return services, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Registry) ListServices() ([]*registry.Service, error) {
|
|
|
|
m.RLock()
|
|
|
|
defer m.RUnlock()
|
|
|
|
|
|
|
|
var services []*registry.Service
|
|
|
|
for _, records := range m.records {
|
|
|
|
for _, record := range records {
|
|
|
|
services = append(services, recordToService(record))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return services, nil
|
|
|
|
}
|
|
|
|
|
2019-01-14 15:27:25 +00:00
|
|
|
func (m *Registry) Watch(opts ...registry.WatchOption) (registry.Watcher, error) {
|
2019-01-18 17:29:17 +00:00
|
|
|
var wo registry.WatchOptions
|
2018-02-19 17:12:37 +00:00
|
|
|
for _, o := range opts {
|
2019-01-18 17:29:17 +00:00
|
|
|
o(&wo)
|
|
|
|
}
|
|
|
|
|
|
|
|
w := &Watcher{
|
|
|
|
exit: make(chan bool),
|
|
|
|
res: make(chan *registry.Result),
|
|
|
|
id: uuid.New().String(),
|
|
|
|
wo: wo,
|
2018-02-19 17:12:37 +00:00
|
|
|
}
|
2019-01-18 17:29:17 +00:00
|
|
|
|
|
|
|
m.Lock()
|
2019-11-16 02:55:11 -08:00
|
|
|
m.watchers[w.id] = w
|
2019-01-18 17:29:17 +00:00
|
|
|
m.Unlock()
|
2019-11-16 02:55:11 -08:00
|
|
|
|
2019-01-18 17:29:17 +00:00
|
|
|
return w, nil
|
2015-12-09 19:23:16 +00:00
|
|
|
}
|
|
|
|
|
2019-01-14 15:27:25 +00:00
|
|
|
func (m *Registry) String() string {
|
|
|
|
return "memory"
|
2015-12-19 21:56:14 +00:00
|
|
|
}
|