2020-12-29 17:49:26 +02:00
|
|
|
package registry
|
2015-12-09 21:23:16 +02:00
|
|
|
|
|
|
|
import (
|
2019-01-09 08:24:12 +02:00
|
|
|
"sync"
|
2019-01-18 19:29:17 +02:00
|
|
|
"time"
|
2019-01-09 08:24:12 +02:00
|
|
|
|
2019-01-18 19:29:17 +02:00
|
|
|
"github.com/google/uuid"
|
2022-09-29 16:44:53 +02:00
|
|
|
log "go-micro.dev/v4/logger"
|
2015-12-09 21:23:16 +02:00
|
|
|
)
|
|
|
|
|
2019-09-25 02:57:37 +02:00
|
|
|
var (
|
2019-09-25 19:19:18 +02:00
|
|
|
sendEventTime = 10 * time.Millisecond
|
2019-11-16 12:55:11 +02:00
|
|
|
ttlPruneTime = time.Second
|
2019-09-25 02:57:37 +02:00
|
|
|
)
|
|
|
|
|
2019-09-25 17:20:42 +02:00
|
|
|
type node struct {
|
2020-12-29 17:49:26 +02:00
|
|
|
*Node
|
2019-11-16 12:55:11 +02:00
|
|
|
TTL time.Duration
|
|
|
|
LastSeen time.Time
|
|
|
|
}
|
|
|
|
|
|
|
|
type record struct {
|
|
|
|
Name string
|
|
|
|
Version string
|
|
|
|
Metadata map[string]string
|
|
|
|
Nodes map[string]*node
|
2020-12-29 17:49:26 +02:00
|
|
|
Endpoints []*Endpoint
|
2019-09-25 17:20:42 +02:00
|
|
|
}
|
|
|
|
|
2020-12-29 17:49:26 +02:00
|
|
|
type memRegistry struct {
|
2022-09-29 16:44:53 +02:00
|
|
|
options *Options
|
2019-01-18 19:29:17 +02:00
|
|
|
|
2019-01-09 08:24:12 +02:00
|
|
|
sync.RWMutex
|
2019-11-16 12:55:11 +02:00
|
|
|
records map[string]map[string]*record
|
2020-12-29 17:49:26 +02:00
|
|
|
watchers map[string]*memWatcher
|
2016-02-26 02:09:06 +02:00
|
|
|
}
|
2015-12-09 21:23:16 +02:00
|
|
|
|
2020-12-29 17:49:26 +02:00
|
|
|
func NewMemoryRegistry(opts ...Option) Registry {
|
2022-09-29 16:44:53 +02:00
|
|
|
options := NewOptions(opts...)
|
2016-03-14 21:15:42 +02:00
|
|
|
|
2019-11-16 12:55:11 +02:00
|
|
|
records := getServiceRecords(options.Context)
|
|
|
|
if records == nil {
|
|
|
|
records = make(map[string]map[string]*record)
|
2019-09-25 02:57:37 +02:00
|
|
|
}
|
|
|
|
|
2020-12-29 17:49:26 +02:00
|
|
|
reg := &memRegistry{
|
2019-09-25 02:57:37 +02:00
|
|
|
options: options,
|
2019-11-16 12:55:11 +02:00
|
|
|
records: records,
|
2020-12-29 17:49:26 +02:00
|
|
|
watchers: make(map[string]*memWatcher),
|
2019-09-25 02:57:37 +02:00
|
|
|
}
|
2019-09-25 19:19:18 +02:00
|
|
|
|
|
|
|
go reg.ttlPrune()
|
|
|
|
|
|
|
|
return reg
|
|
|
|
}
|
|
|
|
|
2020-12-29 17:49:26 +02:00
|
|
|
func (m *memRegistry) ttlPrune() {
|
2022-09-29 16:44:53 +02:00
|
|
|
logger := m.options.Logger
|
2019-09-25 19:19:18 +02:00
|
|
|
prune := time.NewTicker(ttlPruneTime)
|
|
|
|
defer prune.Stop()
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-prune.C:
|
|
|
|
m.Lock()
|
2019-11-16 12:55:11 +02: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 {
|
2022-09-29 16:44:53 +02:00
|
|
|
logger.Logf(log.DebugLevel, "Registry TTL expired for node %s of service %s", n.Id, name)
|
2019-11-16 12:55:11 +02:00
|
|
|
delete(m.records[name][version].Nodes, id)
|
2019-09-25 19:19:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
m.Unlock()
|
|
|
|
}
|
|
|
|
}
|
2019-09-25 02:57:37 +02:00
|
|
|
}
|
|
|
|
|
2020-12-29 17:49:26 +02:00
|
|
|
func (m *memRegistry) sendEvent(r *Result) {
|
2019-01-18 19:29:17 +02:00
|
|
|
m.RLock()
|
2020-12-29 17:49:26 +02:00
|
|
|
watchers := make([]*memWatcher, 0, len(m.watchers))
|
2019-11-16 12:55:11 +02:00
|
|
|
for _, w := range m.watchers {
|
2019-01-18 19:29:17 +02:00
|
|
|
watchers = append(watchers, w)
|
|
|
|
}
|
|
|
|
m.RUnlock()
|
|
|
|
|
|
|
|
for _, w := range watchers {
|
|
|
|
select {
|
|
|
|
case <-w.exit:
|
|
|
|
m.Lock()
|
2019-11-16 12:55:11 +02:00
|
|
|
delete(m.watchers, w.id)
|
2019-01-18 19:29:17 +02:00
|
|
|
m.Unlock()
|
|
|
|
default:
|
|
|
|
select {
|
|
|
|
case w.res <- r:
|
2019-09-25 19:19:18 +02:00
|
|
|
case <-time.After(sendEventTime):
|
2019-01-18 19:29:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-29 17:49:26 +02:00
|
|
|
func (m *memRegistry) Init(opts ...Option) error {
|
2019-01-18 19:29:17 +02:00
|
|
|
for _, o := range opts {
|
2022-09-29 16:44:53 +02:00
|
|
|
o(m.options)
|
2019-01-18 19:29:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// add services
|
2019-01-09 08:24:12 +02:00
|
|
|
m.Lock()
|
2019-11-16 12:55:11 +02: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 19:29:17 +02:00
|
|
|
}
|
2019-11-16 12:55:11 +02:00
|
|
|
|
2019-01-18 19:29:17 +02:00
|
|
|
return nil
|
|
|
|
}
|
2019-01-09 08:24:12 +02:00
|
|
|
|
2020-12-29 17:49:26 +02:00
|
|
|
func (m *memRegistry) Options() Options {
|
2022-09-29 16:44:53 +02:00
|
|
|
return *m.options
|
2019-01-18 19:29:17 +02:00
|
|
|
}
|
|
|
|
|
2020-12-29 17:49:26 +02:00
|
|
|
func (m *memRegistry) Register(s *Service, opts ...RegisterOption) error {
|
2019-01-18 19:29:17 +02:00
|
|
|
m.Lock()
|
2019-09-25 02:57:37 +02:00
|
|
|
defer m.Unlock()
|
2022-09-29 16:44:53 +02:00
|
|
|
logger := m.options.Logger
|
2020-12-29 17:49:26 +02:00
|
|
|
var options RegisterOptions
|
2019-09-25 17:20:42 +02:00
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
|
2019-11-16 12:55:11 +02: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
|
2022-09-29 16:44:53 +02:00
|
|
|
logger.Logf(log.DebugLevel, "Registry added new service: %s, version: %s", s.Name, s.Version)
|
2020-12-29 17:49:26 +02:00
|
|
|
go m.sendEvent(&Result{Action: "update", Service: s})
|
2019-09-30 13:48:06 +02:00
|
|
|
return nil
|
2019-11-16 12:55:11 +02: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
|
2022-08-13 12:23:42 +02:00
|
|
|
}
|
|
|
|
m.records[s.Name][s.Version].Nodes[n.Id] = &node{
|
|
|
|
Node: &Node{
|
|
|
|
Id: n.Id,
|
|
|
|
Address: n.Address,
|
|
|
|
Metadata: metadata,
|
|
|
|
},
|
|
|
|
TTL: options.TTL,
|
|
|
|
LastSeen: time.Now(),
|
2019-09-25 19:19:18 +02:00
|
|
|
}
|
2019-09-25 02:57:37 +02:00
|
|
|
}
|
2019-07-03 23:15:44 +02:00
|
|
|
}
|
|
|
|
|
2019-11-16 12:55:11 +02:00
|
|
|
if addedNodes {
|
2022-09-29 16:44:53 +02:00
|
|
|
logger.Logf(log.DebugLevel, "Registry added new node to service: %s, version: %s", s.Name, s.Version)
|
2020-12-29 17:49:26 +02:00
|
|
|
go m.sendEvent(&Result{Action: "update", Service: s})
|
2019-11-16 12:55:11 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// refresh TTL and timestamp
|
|
|
|
for _, n := range s.Nodes {
|
2022-09-29 16:44:53 +02:00
|
|
|
logger.Logf(log.DebugLevel, "Updated registration for service: %s, version: %s", s.Name, s.Version)
|
2019-11-16 12:55:11 +02:00
|
|
|
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 21:23:16 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-12-29 17:49:26 +02:00
|
|
|
func (m *memRegistry) Deregister(s *Service, opts ...DeregisterOption) error {
|
2019-01-18 19:29:17 +02:00
|
|
|
m.Lock()
|
2019-09-25 02:57:37 +02:00
|
|
|
defer m.Unlock()
|
2022-09-29 16:44:53 +02:00
|
|
|
logger := m.options.Logger
|
2019-11-16 12:55:11 +02: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 {
|
2022-09-29 16:44:53 +02:00
|
|
|
logger.Logf(log.DebugLevel, "Registry removed node from service: %s, version: %s", s.Name, s.Version)
|
2019-11-16 12:55:11 +02:00
|
|
|
delete(m.records[s.Name][s.Version].Nodes, n.Id)
|
2019-09-25 17:20:42 +02:00
|
|
|
}
|
|
|
|
}
|
2019-11-16 12:55:11 +02:00
|
|
|
if len(m.records[s.Name][s.Version].Nodes) == 0 {
|
|
|
|
delete(m.records[s.Name], s.Version)
|
2022-09-29 16:44:53 +02:00
|
|
|
logger.Logf(log.DebugLevel, "Registry removed service: %s, version: %s", s.Name, s.Version)
|
2019-09-25 17:20:42 +02:00
|
|
|
}
|
2019-07-03 23:15:44 +02:00
|
|
|
}
|
2019-11-16 12:55:11 +02:00
|
|
|
if len(m.records[s.Name]) == 0 {
|
|
|
|
delete(m.records, s.Name)
|
2022-09-29 16:44:53 +02:00
|
|
|
logger.Logf(log.DebugLevel, "Registry removed service: %s", s.Name)
|
2019-11-16 12:55:11 +02:00
|
|
|
}
|
2020-12-29 17:49:26 +02:00
|
|
|
go m.sendEvent(&Result{Action: "delete", Service: s})
|
2019-07-03 23:15:44 +02:00
|
|
|
}
|
|
|
|
|
2015-12-09 21:23:16 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-12-29 17:49:26 +02:00
|
|
|
func (m *memRegistry) GetService(name string, opts ...GetOption) ([]*Service, error) {
|
2019-11-16 12:55:11 +02:00
|
|
|
m.RLock()
|
|
|
|
defer m.RUnlock()
|
|
|
|
|
|
|
|
records, ok := m.records[name]
|
|
|
|
if !ok {
|
2020-12-29 17:49:26 +02:00
|
|
|
return nil, ErrNotFound
|
2019-11-16 12:55:11 +02:00
|
|
|
}
|
|
|
|
|
2020-12-29 17:49:26 +02:00
|
|
|
services := make([]*Service, len(m.records[name]))
|
2019-11-16 12:55:11 +02:00
|
|
|
i := 0
|
|
|
|
for _, record := range records {
|
|
|
|
services[i] = recordToService(record)
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
|
|
|
|
return services, nil
|
|
|
|
}
|
|
|
|
|
2020-12-29 17:49:26 +02:00
|
|
|
func (m *memRegistry) ListServices(opts ...ListOption) ([]*Service, error) {
|
2019-11-16 12:55:11 +02:00
|
|
|
m.RLock()
|
|
|
|
defer m.RUnlock()
|
|
|
|
|
2020-12-29 17:49:26 +02:00
|
|
|
var services []*Service
|
2019-11-16 12:55:11 +02:00
|
|
|
for _, records := range m.records {
|
|
|
|
for _, record := range records {
|
|
|
|
services = append(services, recordToService(record))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return services, nil
|
|
|
|
}
|
|
|
|
|
2020-12-29 17:49:26 +02:00
|
|
|
func (m *memRegistry) Watch(opts ...WatchOption) (Watcher, error) {
|
|
|
|
var wo WatchOptions
|
2018-02-19 19:12:37 +02:00
|
|
|
for _, o := range opts {
|
2019-01-18 19:29:17 +02:00
|
|
|
o(&wo)
|
|
|
|
}
|
|
|
|
|
2020-12-29 17:49:26 +02:00
|
|
|
w := &memWatcher{
|
2019-01-18 19:29:17 +02:00
|
|
|
exit: make(chan bool),
|
2020-12-29 17:49:26 +02:00
|
|
|
res: make(chan *Result),
|
2019-01-18 19:29:17 +02:00
|
|
|
id: uuid.New().String(),
|
|
|
|
wo: wo,
|
2018-02-19 19:12:37 +02:00
|
|
|
}
|
2019-01-18 19:29:17 +02:00
|
|
|
|
|
|
|
m.Lock()
|
2019-11-16 12:55:11 +02:00
|
|
|
m.watchers[w.id] = w
|
2019-01-18 19:29:17 +02:00
|
|
|
m.Unlock()
|
2019-11-16 12:55:11 +02:00
|
|
|
|
2019-01-18 19:29:17 +02:00
|
|
|
return w, nil
|
2015-12-09 21:23:16 +02:00
|
|
|
}
|
|
|
|
|
2020-12-29 17:49:26 +02:00
|
|
|
func (m *memRegistry) String() string {
|
2019-01-14 17:27:25 +02:00
|
|
|
return "memory"
|
2015-12-19 23:56:14 +02:00
|
|
|
}
|