-
Notifications
You must be signed in to change notification settings - Fork 1
/
table.go
281 lines (227 loc) · 6.2 KB
/
table.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
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Original source: github.com/micro/micro/v3/router/registry/table.go
package registry
import (
"sync"
"time"
"github.com/google/uuid"
"github.com/micro/micro/v3/service/logger"
"github.com/micro/micro/v3/service/router"
)
// table is an in-memory routing table
type table struct {
sync.RWMutex
// routes stores service routes
routes map[string]map[uint64]*route
// watchers stores table watchers
watchers map[string]*tableWatcher
}
type route struct {
route router.Route
updated time.Time
}
// newtable creates a new routing table and returns it
func newTable() *table {
return &table{
routes: make(map[string]map[uint64]*route),
watchers: make(map[string]*tableWatcher),
}
}
// pruneRoutes will prune routes older than the time specified
func (t *table) pruneRoutes(olderThan time.Duration) {
var routes []router.Route
t.Lock()
// search for all the routes
for _, routeList := range t.routes {
for _, r := range routeList {
// if any route is older than
if time.Since(r.updated).Seconds() > olderThan.Seconds() {
routes = append(routes, r.route)
}
}
}
t.Unlock()
// delete the routes we've found
for _, route := range routes {
t.Delete(route)
}
}
// deleteService removes the entire service
func (t *table) deleteService(service, network string) {
t.Lock()
defer t.Unlock()
routes, ok := t.routes[service]
if !ok {
return
}
// delete the routes for the service
for hash, rt := range routes {
// TODO: check if this causes a problem
// with * in the network if that is a thing
// or blank strings
if rt.route.Network != network {
continue
}
delete(routes, hash)
}
// delete the map for the service if its empty
if len(routes) == 0 {
delete(t.routes, service)
return
}
// save the routes
t.routes[service] = routes
}
// sendEvent sends events to all subscribed watchers
func (t *table) sendEvent(e *router.Event) {
t.RLock()
defer t.RUnlock()
if len(e.Id) == 0 {
e.Id = uuid.New().String()
}
for _, w := range t.watchers {
select {
case w.resChan <- e:
case <-w.done:
// don't block forever
case <-time.After(time.Second):
}
}
}
// Create creates new route in the routing table
func (t *table) Create(r router.Route) error {
service := r.Service
sum := r.Hash()
t.Lock()
defer t.Unlock()
// check if there are any routes in the table for the route destination
if _, ok := t.routes[service]; !ok {
t.routes[service] = make(map[uint64]*route)
}
// add new route to the table for the route destination
if _, ok := t.routes[service][sum]; ok {
return router.ErrDuplicateRoute
}
// create the route
t.routes[service][sum] = &route{r, time.Now()}
if logger.V(logger.DebugLevel, logger.DefaultLogger) {
logger.Debugf("Router emitting %s for route: %s", router.Create, r.Address)
}
// send a route created event
go t.sendEvent(&router.Event{Type: router.Create, Timestamp: time.Now(), Route: r})
return nil
}
// Delete deletes the route from the routing table
func (t *table) Delete(r router.Route) error {
service := r.Service
sum := r.Hash()
t.Lock()
defer t.Unlock()
if _, ok := t.routes[service]; !ok {
return router.ErrRouteNotFound
}
if _, ok := t.routes[service][sum]; !ok {
return router.ErrRouteNotFound
}
// delete the route from the service
delete(t.routes[service], sum)
// delete the whole map if there are no routes left
if len(t.routes[service]) == 0 {
delete(t.routes, service)
}
if logger.V(logger.DebugLevel, logger.DefaultLogger) {
logger.Debugf("Router emitting %s for route: %s", router.Delete, r.Address)
}
go t.sendEvent(&router.Event{Type: router.Delete, Timestamp: time.Now(), Route: r})
return nil
}
// Update updates routing table with the new route
func (t *table) Update(r router.Route) error {
service := r.Service
sum := r.Hash()
t.Lock()
defer t.Unlock()
// check if the route destination has any routes in the table
if _, ok := t.routes[service]; !ok {
t.routes[service] = make(map[uint64]*route)
}
if _, ok := t.routes[service][sum]; !ok {
// update the route
t.routes[service][sum] = &route{r, time.Now()}
if logger.V(logger.DebugLevel, logger.DefaultLogger) {
logger.Debugf("Router emitting %s for route: %s", router.Update, r.Address)
}
go t.sendEvent(&router.Event{Type: router.Update, Timestamp: time.Now(), Route: r})
return nil
}
// just update the route, but dont emit Update event
t.routes[service][sum] = &route{r, time.Now()}
return nil
}
// Read entries from the table
func (t *table) Read(opts ...router.ReadOption) ([]router.Route, error) {
var options router.ReadOptions
for _, o := range opts {
o(&options)
}
t.RLock()
defer t.RUnlock()
var routes []router.Route
// get the routes based on options passed
if len(options.Service) > 0 {
routeMap, ok := t.routes[options.Service]
if !ok {
return nil, router.ErrRouteNotFound
}
for _, rt := range routeMap {
routes = append(routes, rt.route)
}
return routes, nil
}
// otherwise get all routes
for _, serviceRoutes := range t.routes {
for _, rt := range serviceRoutes {
routes = append(routes, rt.route)
}
}
return routes, nil
}
// Watch returns routing table entry watcher
func (t *table) Watch(opts ...router.WatchOption) (router.Watcher, error) {
// by default watch everything
wopts := router.WatchOptions{
Service: "*",
}
for _, o := range opts {
o(&wopts)
}
w := &tableWatcher{
id: uuid.New().String(),
opts: wopts,
resChan: make(chan *router.Event, 10),
done: make(chan struct{}),
}
// when the watcher is stopped delete it
go func() {
<-w.done
t.Lock()
delete(t.watchers, w.id)
t.Unlock()
}()
// save the watcher
t.Lock()
t.watchers[w.id] = w
t.Unlock()
return w, nil
}