2019-01-14 17:27:25 +02:00
|
|
|
// Package memory provides an in-memory registry
|
|
|
|
package memory
|
2015-12-09 21:23:16 +02:00
|
|
|
|
|
|
|
import (
|
2019-01-18 19:29:17 +02:00
|
|
|
"context"
|
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"
|
2015-12-09 21:23:16 +02:00
|
|
|
"github.com/micro/go-micro/registry"
|
|
|
|
)
|
|
|
|
|
2019-01-14 17:27:25 +02:00
|
|
|
type Registry struct {
|
2019-01-18 19:29:17 +02:00
|
|
|
options registry.Options
|
|
|
|
|
2019-01-09 08:24:12 +02:00
|
|
|
sync.RWMutex
|
2016-02-26 02:09:06 +02:00
|
|
|
Services map[string][]*registry.Service
|
2019-01-18 19:29:17 +02:00
|
|
|
Watchers map[string]*Watcher
|
2016-02-26 02:09:06 +02:00
|
|
|
}
|
2015-12-09 21:23:16 +02:00
|
|
|
|
2016-03-14 21:15:42 +02:00
|
|
|
var (
|
2019-01-18 19:29:17 +02:00
|
|
|
timeout = time.Millisecond * 10
|
2016-03-14 21:15:42 +02:00
|
|
|
)
|
|
|
|
|
2019-01-18 19:29:17 +02:00
|
|
|
func (m *Registry) watch(r *registry.Result) {
|
|
|
|
var watchers []*Watcher
|
|
|
|
|
|
|
|
m.RLock()
|
|
|
|
for _, w := range m.Watchers {
|
|
|
|
watchers = append(watchers, w)
|
|
|
|
}
|
|
|
|
m.RUnlock()
|
|
|
|
|
|
|
|
for _, w := range watchers {
|
|
|
|
select {
|
|
|
|
case <-w.exit:
|
|
|
|
m.Lock()
|
|
|
|
delete(m.Watchers, w.id)
|
|
|
|
m.Unlock()
|
|
|
|
default:
|
|
|
|
select {
|
|
|
|
case w.res <- r:
|
|
|
|
case <-time.After(timeout):
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Registry) Init(opts ...registry.Option) error {
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&m.options)
|
|
|
|
}
|
|
|
|
|
|
|
|
// add services
|
2019-01-09 08:24:12 +02:00
|
|
|
m.Lock()
|
2019-01-18 19:29:17 +02:00
|
|
|
for k, v := range getServices(m.options.Context) {
|
|
|
|
s := m.Services[k]
|
2019-07-03 23:15:44 +02:00
|
|
|
m.Services[k] = registry.AddServices(s, v)
|
2019-01-18 19:29:17 +02:00
|
|
|
}
|
|
|
|
m.Unlock()
|
|
|
|
return nil
|
|
|
|
}
|
2019-01-09 08:24:12 +02:00
|
|
|
|
2019-01-18 19:29:17 +02:00
|
|
|
func (m *Registry) Options() registry.Options {
|
|
|
|
return m.options
|
|
|
|
}
|
|
|
|
|
2019-07-03 23:15:44 +02:00
|
|
|
func (m *Registry) GetService(name string) ([]*registry.Service, error) {
|
2019-01-18 19:29:17 +02:00
|
|
|
m.RLock()
|
2019-07-03 23:15:44 +02:00
|
|
|
service, ok := m.Services[name]
|
|
|
|
m.RUnlock()
|
|
|
|
if !ok {
|
2016-02-26 02:09:06 +02:00
|
|
|
return nil, registry.ErrNotFound
|
|
|
|
}
|
2019-07-03 23:15:44 +02:00
|
|
|
|
|
|
|
return service, nil
|
2015-12-09 21:23:16 +02:00
|
|
|
}
|
|
|
|
|
2019-01-14 17:27:25 +02:00
|
|
|
func (m *Registry) ListServices() ([]*registry.Service, error) {
|
2016-02-26 02:09:06 +02:00
|
|
|
var services []*registry.Service
|
2019-07-03 23:15:44 +02:00
|
|
|
m.RLock()
|
2016-02-26 02:09:06 +02:00
|
|
|
for _, service := range m.Services {
|
|
|
|
services = append(services, service...)
|
|
|
|
}
|
2019-01-18 19:29:17 +02:00
|
|
|
m.RUnlock()
|
2016-02-26 02:09:06 +02:00
|
|
|
return services, nil
|
2015-12-09 21:23:16 +02:00
|
|
|
}
|
|
|
|
|
2019-01-14 17:27:25 +02:00
|
|
|
func (m *Registry) Register(s *registry.Service, opts ...registry.RegisterOption) error {
|
2019-01-18 19:29:17 +02:00
|
|
|
go m.watch(®istry.Result{Action: "update", Service: s})
|
|
|
|
m.Lock()
|
2019-07-03 23:15:44 +02:00
|
|
|
if service, ok := m.Services[s.Name]; !ok {
|
|
|
|
m.Services[s.Name] = []*registry.Service{s}
|
|
|
|
} else {
|
|
|
|
m.Services[s.Name] = registry.AddServices(service, []*registry.Service{s})
|
|
|
|
}
|
2019-01-18 19:29:17 +02:00
|
|
|
m.Unlock()
|
2019-07-03 23:15:44 +02:00
|
|
|
|
2015-12-09 21:23:16 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-01-14 17:27:25 +02:00
|
|
|
func (m *Registry) Deregister(s *registry.Service) error {
|
2019-01-18 19:29:17 +02:00
|
|
|
go m.watch(®istry.Result{Action: "delete", Service: s})
|
2019-01-09 08:24:12 +02:00
|
|
|
|
2019-01-18 19:29:17 +02:00
|
|
|
m.Lock()
|
2019-07-03 23:15:44 +02:00
|
|
|
if service, ok := m.Services[s.Name]; ok {
|
|
|
|
if service := registry.DelServices(service, []*registry.Service{s}); len(service) == 0 {
|
|
|
|
delete(m.Services, s.Name)
|
|
|
|
} else {
|
|
|
|
m.Services[s.Name] = service
|
|
|
|
}
|
|
|
|
}
|
2019-01-18 19:29:17 +02:00
|
|
|
m.Unlock()
|
2019-07-03 23:15:44 +02:00
|
|
|
|
2015-12-09 21:23:16 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-01-14 17:27:25 +02:00
|
|
|
func (m *Registry) Watch(opts ...registry.WatchOption) (registry.Watcher, error) {
|
2019-01-18 19:29:17 +02:00
|
|
|
var wo registry.WatchOptions
|
2018-02-19 19:12:37 +02:00
|
|
|
for _, o := range opts {
|
2019-01-18 19:29:17 +02:00
|
|
|
o(&wo)
|
|
|
|
}
|
|
|
|
|
|
|
|
w := &Watcher{
|
|
|
|
exit: make(chan bool),
|
|
|
|
res: make(chan *registry.Result),
|
|
|
|
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
|
|
|
|
m.Unlock()
|
|
|
|
return w, nil
|
2015-12-09 21:23:16 +02:00
|
|
|
}
|
|
|
|
|
2019-01-14 17:27:25 +02:00
|
|
|
func (m *Registry) String() string {
|
|
|
|
return "memory"
|
2015-12-19 23:56:14 +02:00
|
|
|
}
|
|
|
|
|
2019-01-18 19:29:17 +02:00
|
|
|
func NewRegistry(opts ...registry.Option) registry.Registry {
|
|
|
|
options := registry.Options{
|
|
|
|
Context: context.Background(),
|
|
|
|
}
|
2018-08-08 19:57:29 +02:00
|
|
|
|
2019-01-18 19:29:17 +02:00
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
|
|
|
|
services := getServices(options.Context)
|
|
|
|
if services == nil {
|
|
|
|
services = make(map[string][]*registry.Service)
|
|
|
|
}
|
2017-09-28 12:16:56 +02:00
|
|
|
|
2019-01-14 17:27:25 +02:00
|
|
|
return &Registry{
|
2019-01-18 19:29:17 +02:00
|
|
|
options: options,
|
|
|
|
Services: services,
|
|
|
|
Watchers: make(map[string]*Watcher),
|
2019-01-14 17:27:25 +02:00
|
|
|
}
|
2015-12-09 21:23:16 +02:00
|
|
|
}
|