-
Notifications
You must be signed in to change notification settings - Fork 2
/
local.go
194 lines (165 loc) · 3.49 KB
/
local.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package disco
import (
"context"
"errors"
"sync"
"github.com/google/uuid"
"github.com/deixis/spine/log"
)
var activeLocalAgent = NewLocalAgent()
// agent is a local-only service discovery agent
// This agent is used when service discovery is disabled
type agent struct {
mu sync.RWMutex
Registry map[string]*Instance
// subs contains all event subscriptions
Subs map[chan *Event]struct{}
}
// NewLocalAgent returns a new local-only service discovery agent. This agent
// is used when service discovery is disabled.
func NewLocalAgent() Agent {
return &agent{
Registry: map[string]*Instance{},
Subs: map[chan *Event]struct{}{},
}
}
func (a *agent) Register(ctx context.Context, r *Registration) (string, error) {
a.mu.Lock()
defer a.mu.Unlock()
id := r.ID
if id == "" {
id = uuid.New().String()
}
if _, ok := a.Registry[id]; ok {
return "", errors.New("service already registered")
}
instance := &Instance{
Local: true,
ID: id,
Name: r.Name,
Host: r.Addr,
Port: r.Port,
Tags: r.Tags,
}
a.Registry[id] = instance
// Notifiy subscribers
for sub := range a.Subs {
sub <- &Event{
Op: Add,
Instance: instance,
}
}
return id, nil
}
func (a *agent) Deregister(ctx context.Context, id string) error {
a.mu.Lock()
defer a.mu.Unlock()
return a.deregister(ctx, id)
}
func (a *agent) Services(
ctx context.Context, tags ...string,
) (map[string]Service, error) {
a.mu.RLock()
defer a.mu.RUnlock()
return a.services(ctx, tags...)
}
func (a *agent) Service(
ctx context.Context, name string, tags ...string,
) (Service, error) {
a.mu.RLock()
defer a.mu.RUnlock()
services, err := a.services(ctx, tags...)
if err != nil {
return nil, err
}
s, ok := services[name]
if !ok {
return nil, errors.New("service does not exist")
}
return s, nil
}
func (a *agent) Leave(ctx context.Context) {
a.mu.Lock()
defer a.mu.Unlock()
for id := range a.Registry {
err := a.deregister(ctx, id)
if err != nil {
log.FromContext(ctx).Warning(
"leave.failure",
"Could not de-register service",
log.String("service_id", id),
)
}
}
a.Registry = map[string]*Instance{}
a.Subs = map[chan *Event]struct{}{}
}
func (a *agent) services(
ctx context.Context, tags ...string,
) (map[string]Service, error) {
services := map[string]Service{}
for _, instance := range a.Registry {
services[instance.Name] = &service{
name: instance.Name,
instances: []*Instance{instance},
watch: func() Watcher {
sub := make(chan *Event, 1)
unsub := func() {
delete(a.Subs, sub)
}
a.Subs[sub] = struct{}{}
return &watcher{
sub: sub,
unsub: unsub,
}
},
}
}
return services, nil
}
func (a *agent) deregister(ctx context.Context, id string) error {
instance, ok := a.Registry[id]
if !ok {
return nil
}
delete(a.Registry, id)
// Notifiy subscribers
for sub := range a.Subs {
sub <- &Event{
Op: Delete,
Instance: instance,
}
}
return nil
}
// service implements Service
type service struct {
name string
instances []*Instance
watch func() Watcher
}
func (s *service) Name() string {
return s.name
}
func (s *service) Watch() Watcher {
return s.watch()
}
func (s *service) Instances() []*Instance {
return s.instances
}
type watcher struct {
sub chan *Event
unsub func()
}
func (w *watcher) Next() ([]*Event, error) {
e, ok := <-w.sub
if !ok {
return nil, ErrWatcherClosed
}
return []*Event{e}, nil
}
func (w *watcher) Close() error {
w.unsub()
close(w.sub)
return nil
}