micro/registry/memory/memory.go

136 lines
2.5 KiB
Go
Raw Normal View History

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-09 09:24:12 +03:00
"sync"
2015-12-09 22:23:16 +03:00
"github.com/micro/go-micro/registry"
)
2019-01-14 18:27:25 +03:00
type Registry struct {
2019-01-09 09:24:12 +03:00
sync.RWMutex
2016-02-26 03:09:06 +03:00
Services map[string][]*registry.Service
}
2015-12-09 22:23:16 +03:00
2016-03-14 22:15:42 +03:00
var (
2019-01-14 18:27:25 +03:00
// mock data
Data = map[string][]*registry.Service{
2016-03-14 22:15:42 +03:00
"foo": []*registry.Service{
{
Name: "foo",
Version: "1.0.0",
Nodes: []*registry.Node{
{
Id: "foo-1.0.0-123",
Address: "localhost",
Port: 9999,
},
{
Id: "foo-1.0.0-321",
Address: "localhost",
Port: 9999,
},
2015-12-09 22:23:16 +03:00
},
},
2016-03-14 22:15:42 +03:00
{
Name: "foo",
Version: "1.0.1",
Nodes: []*registry.Node{
{
Id: "foo-1.0.1-321",
Address: "localhost",
Port: 6666,
},
2015-12-09 22:23:16 +03:00
},
},
2016-03-14 22:15:42 +03:00
{
Name: "foo",
Version: "1.0.3",
Nodes: []*registry.Node{
{
Id: "foo-1.0.3-345",
Address: "localhost",
Port: 8888,
},
2015-12-09 22:23:16 +03:00
},
},
},
2016-02-26 03:09:06 +03:00
}
2016-03-14 22:15:42 +03:00
)
2019-01-14 18:27:25 +03:00
// Setup sets mock data
func (m *Registry) Setup() {
2019-01-09 09:24:12 +03:00
m.Lock()
defer m.Unlock()
2019-01-14 18:27:25 +03:00
// add some memory data
m.Services = Data
2016-02-26 03:09:06 +03:00
}
2019-01-14 18:27:25 +03:00
func (m *Registry) GetService(service string) ([]*registry.Service, error) {
2019-01-09 09:24:12 +03:00
m.Lock()
defer m.Unlock()
2016-02-26 03:09:06 +03:00
s, ok := m.Services[service]
if !ok || len(s) == 0 {
2016-02-26 03:09:06 +03:00
return nil, registry.ErrNotFound
}
return s, nil
2015-12-09 22:23:16 +03:00
}
2019-01-14 18:27:25 +03:00
func (m *Registry) ListServices() ([]*registry.Service, error) {
2019-01-09 09:24:12 +03:00
m.Lock()
defer m.Unlock()
2016-02-26 03:09:06 +03:00
var services []*registry.Service
for _, service := range m.Services {
services = append(services, service...)
}
return services, nil
2015-12-09 22:23:16 +03:00
}
2019-01-14 18:27:25 +03:00
func (m *Registry) Register(s *registry.Service, opts ...registry.RegisterOption) error {
2019-01-09 09:24:12 +03:00
m.Lock()
defer m.Unlock()
2016-03-14 22:15:42 +03:00
services := addServices(m.Services[s.Name], []*registry.Service{s})
m.Services[s.Name] = services
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-09 09:24:12 +03:00
m.Lock()
defer m.Unlock()
2016-03-14 22:15:42 +03:00
services := delServices(m.Services[s.Name], []*registry.Service{s})
m.Services[s.Name] = services
2015-12-09 22:23:16 +03:00
return nil
}
2019-01-14 18:27:25 +03:00
func (m *Registry) Watch(opts ...registry.WatchOption) (registry.Watcher, error) {
2018-02-19 20:12:37 +03:00
var wopts registry.WatchOptions
for _, o := range opts {
o(&wopts)
}
2019-01-14 18:27:25 +03:00
return &memoryWatcher{exit: make(chan bool), opts: wopts}, 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
}
2019-01-14 18:27:25 +03:00
func (m *Registry) Init(opts ...registry.Option) error {
return nil
}
2019-01-14 18:27:25 +03:00
func (m *Registry) Options() registry.Options {
2017-09-28 13:16:56 +03:00
return registry.Options{}
}
2019-01-14 18:27:25 +03:00
func NewRegistry(opts ...registry.Option) registry.Registry {
return &Registry{
Services: make(map[string][]*registry.Service),
}
2015-12-09 22:23:16 +03:00
}