-
Notifications
You must be signed in to change notification settings - Fork 0
/
router.go
399 lines (342 loc) · 7.88 KB
/
router.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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
/*
* Copyright (c) 2019-2021. Abstrium SAS <team (at) pydio.com>
* This file is part of Pydio Cells.
*
* Pydio Cells is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Pydio Cells is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Pydio Cells. If not, see <http://www.gnu.org/licenses/>.
*
* The latest code can be found at <https://pydio.com>.
*/
// Package router provides api service routing
package router
import (
"errors"
"fmt"
"log"
"net/http"
"regexp"
"strings"
"sync"
"time"
"github.com/micro/go-api"
microrouter "github.com/micro/go-api/router"
"github.com/micro/go-micro/registry"
"github.com/pydio/cells/common/micro/rcache"
)
// router is the default router
type router struct {
exit chan bool
opts microrouter.Options
// registry cache
rc rcache.Cache
sync.RWMutex
eps map[string]*api.Service
}
func (r *router) isClosed() bool {
select {
case <-r.exit:
return true
default:
return false
}
}
// refresh list of api services
func (r *router) refresh() {
var attempts int
for {
services, err := r.opts.Registry.ListServices()
if err != nil {
attempts++
log.Println("Error listing endpoints", err)
time.Sleep(time.Duration(attempts) * time.Second)
continue
}
attempts = 0
// for each service, get service and store endpoints
for _, s := range services {
// only get services for this namespace
if !strings.HasPrefix(s.Name, r.opts.Namespace) {
continue
}
service, err := r.rc.GetService(s.Name)
if err != nil {
continue
}
r.store(service)
}
// refresh list in 10 minutes... cruft
select {
case <-time.After(time.Minute * 10):
case <-r.exit:
return
}
}
}
// process watch event
func (r *router) process(res *registry.Result) {
// skip these things
if res == nil || res.Service == nil || !strings.HasPrefix(res.Service.Name, r.opts.Namespace) {
return
}
// get entry from cache
service, err := r.rc.GetService(res.Service.Name)
if err != nil {
return
}
// update our local endpoints
r.store(service)
}
// store local endpoint cache
func (r *router) store(services []*registry.Service) {
// endpoints
eps := map[string]*api.Service{}
// services
names := map[string]bool{}
// create a new endpoint mapping
for _, service := range services {
// set names we need later
names[service.Name] = true
// map per endpoint
for _, endpoint := range service.Endpoints {
// create a key service:endpoint_name
key := fmt.Sprintf("%s:%s", service.Name, endpoint.Name)
// decode endpoint
end := api.Decode(endpoint.Metadata)
// if we got nothing skip
if err := api.Validate(end); err != nil {
continue
}
// try get endpoint
ep, ok := eps[key]
if !ok {
ep = &api.Service{Name: service.Name}
}
// overwrite the endpoint
ep.Endpoint = end
// append services
ep.Services = append(ep.Services, service)
// store it
eps[key] = ep
}
}
r.Lock()
defer r.Unlock()
// delete any existing eps for services we know
for key, service := range r.eps {
// skip what we don't care about
if !names[service.Name] {
continue
}
// ok we know this thing
// delete delete delete
delete(r.eps, key)
}
// now set the eps we have
for name, endpoint := range eps {
r.eps[name] = endpoint
}
}
// watch for endpoint changes
func (r *router) watch() {
var attempts int
for {
if r.isClosed() {
return
}
// watch for changes
w, err := r.opts.Registry.Watch()
if err != nil {
attempts++
log.Println("Error watching endpoints", err)
time.Sleep(time.Duration(attempts) * time.Second)
continue
}
ch := make(chan bool)
go func() {
select {
case <-ch:
w.Stop()
case <-r.exit:
w.Stop()
}
}()
// reset if we get here
attempts = 0
for {
// process next event
res, err := w.Next()
if err != nil {
log.Println("Error getting next endpoint", err)
close(ch)
break
}
r.process(res)
}
}
}
func (r *router) Options() microrouter.Options {
return r.opts
}
func (r *router) Close() error {
select {
case <-r.exit:
return nil
default:
close(r.exit)
r.rc.Stop()
}
return nil
}
func (r *router) Endpoint(req *http.Request) (*api.Service, error) {
if r.isClosed() {
return nil, errors.New("router closed")
}
r.RLock()
defer r.RUnlock()
// use the first match
// TODO: weighted matching
for _, e := range r.eps {
ep := e.Endpoint
// match
var pathMatch, hostMatch, methodMatch bool
// 1. try method GET, POST, PUT, etc
// 2. try host example.com, foobar.com, etc
// 3. try path /foo/bar, /bar/baz, etc
// 1. try match method
for _, m := range ep.Method {
if req.Method == m {
methodMatch = true
break
}
}
// no match on method pass
if len(ep.Method) > 0 && !methodMatch {
continue
}
// 2. try match host
for _, h := range ep.Host {
if req.Host == h {
hostMatch = true
break
}
}
// no match on host pass
if len(ep.Host) > 0 && !hostMatch {
continue
}
// 3. try match paths
for _, p := range ep.Path {
re, err := regexp.CompilePOSIX(p)
if err == nil && re.MatchString(req.URL.Path) {
pathMatch = true
break
}
}
// no match pass
if len(ep.Path) > 0 && !pathMatch {
continue
}
// TODO: Percentage traffic
// we got here, so its a match
return e, nil
}
// no match
return nil, errors.New("not found")
}
func (r *router) Route(req *http.Request) (*api.Service, error) {
if r.isClosed() {
return nil, errors.New("router closed")
}
// only use endpoint matching when the meta handler is set aka api.Default
switch r.opts.Handler {
// meta handler
case api.Default:
// try get an endpoint from metadata
if ep, err := r.Endpoint(req); err == nil {
return ep, nil
}
// no ep found, shit; default to api handler
// get service route
name, method := apiRoute(r.opts.Namespace, req.URL.Path)
// get service
services, err := r.rc.GetService(name)
if err != nil {
return nil, err
}
// construct api service
return &api.Service{
Name: name,
Endpoint: &api.Endpoint{
Name: method,
Handler: api.Api,
},
Services: services,
}, nil
// http handler
case api.Http, api.Proxy, api.Web:
// get proxy route
name := proxyRoute(r.opts.Namespace, req.URL.Path)
// get service
services, err := r.rc.GetService(name)
if err != nil {
return nil, err
}
// construct api service
return &api.Service{
Name: name,
Endpoint: &api.Endpoint{
Name: req.URL.String(),
Handler: r.opts.Handler,
Host: []string{req.Host},
Method: []string{req.Method},
Path: []string{req.URL.Path},
},
Services: services,
}, nil
// go-micro service handler
case api.Api, api.Rpc:
// get service route
name, method := apiRoute(r.opts.Namespace, req.URL.Path)
// get service
services, err := r.rc.GetService(name)
if err != nil {
return nil, err
}
// construct api service
return &api.Service{
Name: name,
Endpoint: &api.Endpoint{
Name: method,
Handler: r.opts.Handler,
},
Services: services,
}, nil
}
return nil, errors.New("unknown handler")
}
func newRouter(opts ...microrouter.Option) *router {
options := newOptions(opts...)
r := &router{
exit: make(chan bool),
opts: options,
rc: rcache.New(options.Registry),
eps: make(map[string]*api.Service),
}
go r.watch()
go r.refresh()
return r
}
// NewRouter returns the default router
func NewRouter(opts ...microrouter.Option) microrouter.Router {
return newRouter(opts...)
}