-
Notifications
You must be signed in to change notification settings - Fork 2
/
memory.go
307 lines (260 loc) · 7.2 KB
/
memory.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
// MIT License
//
// Copyright (c) 2020 Lack
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
// Package memory provides an in-memory registry
package memory
import (
"context"
"sync"
"time"
"github.com/google/uuid"
"github.com/vine-io/vine/core/registry"
"github.com/vine-io/vine/lib/logger"
)
var (
sendEventTime = 10 * time.Millisecond
ttlPruneTime = time.Second
)
type node struct {
*registry.Node
TTL time.Duration
LastSeen time.Time
}
type record struct {
Name string
Version string
Metadata map[string]string
Nodes map[string]*node
Endpoints []*registry.Endpoint
}
type Registry struct {
options registry.Options
sync.RWMutex
records map[string]map[string]*record
watchers map[string]*Watcher
}
func NewRegistry(opts ...registry.Option) registry.Registry {
options := registry.Options{
Context: context.Background(),
}
for _, o := range opts {
o(&options)
}
records := getServiceRecords(options.Context)
if records == nil {
records = make(map[string]map[string]*record)
}
reg := &Registry{
options: options,
records: records,
watchers: make(map[string]*Watcher),
}
go reg.ttlPrune()
return reg
}
func (m *Registry) ttlPrune() {
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.Debugf("Registry TTL expired for node %s of service %s", n.Id, name)
}
delete(m.records[name][version].Nodes, id)
}
}
}
m.Unlock()
}
}
}
func (m *Registry) sendEvent(r *registry.Result) {
m.RLock()
watchers := make([]*Watcher, 0, len(m.watchers))
for i := range m.watchers {
w := m.watchers[i]
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(sendEventTime):
}
}
}
}
func (m *Registry) Init(opts ...registry.Option) error {
for _, o := range opts {
o(&m.options)
}
// add services
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 don't track yet
for version, r := range record {
if _, ok := m.records[name][version]; !ok {
m.records[name][version] = r
continue
}
}
}
return nil
}
func (m *Registry) Options() registry.Options {
return m.options
}
func (m *Registry) Register(ctx context.Context, s *registry.Service, opts ...registry.RegisterOption) error {
m.Lock()
defer m.Unlock()
var options registry.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.Debugf("Registry added new service: %s, version: %s", s.Name, s.Version)
go m.sendEvent(®istry.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: ®istry.Node{
Id: n.Id,
Address: n.Address,
Metadata: metadata,
},
TTL: options.TTL,
LastSeen: time.Now(),
}
}
}
}
if addedNodes {
logger.Debugf("Registry added new node to service: %s, version: %s", s.Name, s.Version)
go m.sendEvent(®istry.Result{Action: "update", Service: s})
return nil
}
// refresh TTL and timestamp
for _, n := range s.Nodes {
logger.Debugf("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()
}
return nil
}
func (m *Registry) Deregister(ctx context.Context, s *registry.Service, opts ...registry.DeregisterOption) error {
m.Lock()
defer m.Unlock()
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.Debugf("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.Debugf("Registry removed service: %s, version: %s", s.Name, s.Version)
}
}
if len(m.records[s.Name]) == 0 {
delete(m.records, s.Name)
logger.Debugf("Registry removed service: %s", s.Name)
}
go m.sendEvent(®istry.Result{Action: "delete", Service: s})
}
return nil
}
func (m *Registry) GetService(ctx context.Context, name string, opts ...registry.GetOption) ([]*registry.Service, error) {
m.RLock()
defer m.RUnlock()
records, ok := m.records[name]
if !ok {
return nil, registry.ErrNotFound
}
services := make([]*registry.Service, len(m.records[name]))
i := 0
for _, record := range records {
services[i] = recordToService(record)
i++
}
return services, nil
}
func (m *Registry) ListServices(ctx context.Context, opts ...registry.ListOption) ([]*registry.Service, error) {
m.RLock()
defer m.RUnlock()
var services []*registry.Service
for _, records := range m.records {
for _, record := range records {
services = append(services, recordToService(record))
}
}
return services, nil
}
func (m *Registry) Watch(ctx context.Context, opts ...registry.WatchOption) (registry.Watcher, error) {
var wo registry.WatchOptions
for _, o := range opts {
o(&wo)
}
w := &Watcher{
exit: make(chan bool),
res: make(chan *registry.Result),
id: uuid.New().String(),
wo: wo,
}
m.Lock()
m.watchers[w.id] = w
m.Unlock()
return w, nil
}
func (m *Registry) String() string {
return "memory"
}