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"
|
2015-12-09 22:23:16 +03:00
|
|
|
"github.com/micro/go-micro/registry"
|
2019-09-25 18:20:42 +03:00
|
|
|
"github.com/micro/go-micro/util/log"
|
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
|
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
|
2019-11-16 13:55:11 +03:00
|
|
|
records map[string]map[string]*record
|
|
|
|
watchers map[string]*Watcher
|
2016-02-26 03:09:06 +03:00
|
|
|
}
|
2015-12-09 22:23:16 +03:00
|
|
|
|
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
|
|
|
|
2019-11-16 13:55:11 +03:00
|
|
|
records := getServiceRecords(options.Context)
|
|
|
|
if records == nil {
|
|
|
|
records = make(map[string]map[string]*record)
|
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,
|
2019-11-16 13:55:11 +03:00
|
|
|
records: records,
|
|
|
|
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()
|
2019-11-16 13:55:11 +03: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 20:19:18 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
m.Unlock()
|
|
|
|
}
|
|
|
|
}
|
2019-09-25 03:57:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Registry) sendEvent(r *registry.Result) {
|
2019-11-16 13:55:11 +03:00
|
|
|
watchers := make([]*Watcher, 0, len(m.watchers))
|
2019-01-18 20:29:17 +03:00
|
|
|
|
|
|
|
m.RLock()
|
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()
|
|
|
|
|
|
|
|
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 20:29:17 +03:00
|
|
|
}
|
2019-11-16 13:55:11 +03:00
|
|
|
|
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()
|
|
|
|
|
2019-09-25 18:20:42 +03:00
|
|
|
var options registry.RegisterOptions
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
|
2019-11-16 13:55:11 +03: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 03:57:37 +03:00
|
|
|
go m.sendEvent(®istry.Result{Action: "update", Service: s})
|
2019-09-30 14:48:06 +03:00
|
|
|
return nil
|
2019-11-16 13:55:11 +03: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 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 {
|
|
|
|
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 22:23:16 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-01-14 18:27:25 +03:00
|
|
|
func (m *Registry) Deregister(s *registry.Service) error {
|
2019-01-18 20:29:17 +03:00
|
|
|
m.Lock()
|
2019-09-25 03:57:37 +03:00
|
|
|
defer m.Unlock()
|
|
|
|
|
2019-11-16 13:55:11 +03: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 18:20:42 +03:00
|
|
|
}
|
|
|
|
}
|
2019-11-16 13:55:11 +03: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 18:20:42 +03:00
|
|
|
}
|
2019-07-04 00:15:44 +03:00
|
|
|
}
|
2019-11-16 13:55:11 +03: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 22:23:16 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-11-16 13:55:11 +03: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 18:27:25 +03:00
|
|
|
func (m *Registry) Watch(opts ...registry.WatchOption) (registry.Watcher, error) {
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|