1
0
mirror of https://github.com/go-micro/go-micro.git synced 2024-12-18 08:26:38 +02:00
go-micro/registry/memory.go

277 lines
5.8 KiB
Go
Raw Normal View History

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"
log "go-micro.dev/v4/logger"
2015-12-09 21:23:16 +02:00
)
var (
2019-09-25 19:19:18 +02:00
sendEventTime = 10 * time.Millisecond
ttlPruneTime = time.Second
)
type node struct {
2020-12-29 17:49:26 +02:00
*Node
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
}
2020-12-29 17:49:26 +02:00
type memRegistry struct {
options *Options
2019-01-18 19:29:17 +02:00
2019-01-09 08:24:12 +02:00
sync.RWMutex
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 {
options := NewOptions(opts...)
2016-03-14 21:15:42 +02:00
records := getServiceRecords(options.Context)
if records == nil {
records = make(map[string]map[string]*record)
}
2020-12-29 17:49:26 +02:00
reg := &memRegistry{
options: options,
records: records,
2020-12-29 17:49:26 +02:00
watchers: make(map[string]*memWatcher),
}
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() {
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()
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 {
logger.Logf(log.DebugLevel, "Registry TTL expired for node %s of service %s", n.Id, name)
delete(m.records[name][version].Nodes, id)
2019-09-25 19:19:18 +02:00
}
}
}
}
m.Unlock()
}
}
}
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))
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()
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 {
o(m.options)
2019-01-18 19:29:17 +02:00
}
// add services
2019-01-09 08:24:12 +02:00
m.Lock()
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-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 {
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()
defer m.Unlock()
logger := m.options.Logger
2020-12-29 17:49:26 +02:00
var options RegisterOptions
for _, o := range opts {
o(&options)
}
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
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})
return nil
}
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: &Node{
Id: n.Id,
Address: n.Address,
Metadata: metadata,
},
TTL: options.TTL,
LastSeen: time.Now(),
2019-09-25 19:19:18 +02:00
}
}
}
if addedNodes {
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})
return nil
}
// refresh TTL and timestamp
for _, n := range s.Nodes {
logger.Logf(log.DebugLevel, "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 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()
defer m.Unlock()
logger := m.options.Logger
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 {
logger.Logf(log.DebugLevel, "Registry removed node from service: %s, version: %s", s.Name, s.Version)
delete(m.records[s.Name][s.Version].Nodes, n.Id)
}
}
if len(m.records[s.Name][s.Version].Nodes) == 0 {
delete(m.records[s.Name], s.Version)
logger.Logf(log.DebugLevel, "Registry removed service: %s, version: %s", s.Name, s.Version)
}
}
if len(m.records[s.Name]) == 0 {
delete(m.records, s.Name)
logger.Logf(log.DebugLevel, "Registry removed service: %s", s.Name)
}
2020-12-29 17:49:26 +02:00
go m.sendEvent(&Result{Action: "delete", Service: s})
}
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) {
m.RLock()
defer m.RUnlock()
records, ok := m.records[name]
if !ok {
2020-12-29 17:49:26 +02:00
return nil, ErrNotFound
}
2020-12-29 17:49:26 +02:00
services := make([]*Service, len(m.records[name]))
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) {
m.RLock()
defer m.RUnlock()
2020-12-29 17:49:26 +02:00
var services []*Service
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()
m.watchers[w.id] = w
2019-01-18 19:29:17 +02:00
m.Unlock()
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
}