proxy/mucp: add support for multi-tenancy (#1746)

This commit is contained in:
ben-toogood 2020-06-26 10:31:06 +01:00 committed by GitHub
parent a8fc5590a8
commit ee02511658
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -129,7 +129,7 @@ func (p *Proxy) filterRoutes(ctx context.Context, routes []router.Route) []route
// only process routes with this network // only process routes with this network
if net, ok := md.Get("Micro-Network"); ok && len(net) > 0 { if net, ok := md.Get("Micro-Network"); ok && len(net) > 0 {
if route.Network != net { if route.Network != router.DefaultNetwork && route.Network != net {
// skip routes that don't mwatch // skip routes that don't mwatch
continue continue
} }
@ -183,14 +183,12 @@ func (p *Proxy) getLink(r router.Route) (client.Client, error) {
func (p *Proxy) getRoute(ctx context.Context, service string) ([]router.Route, error) { func (p *Proxy) getRoute(ctx context.Context, service string) ([]router.Route, error) {
// lookup the route cache first // lookup the route cache first
p.Lock() p.RLock()
cached, ok := p.Routes[service] cached, ok := p.Routes[service]
p.RUnlock()
if ok { if ok {
p.Unlock() return p.filterRoutes(ctx, toSlice(cached)), nil
routes := toSlice(cached)
return p.filterRoutes(ctx, routes), nil
} }
p.Unlock()
// cache routes for the service // cache routes for the service
routes, err := p.cacheRoutes(service) routes, err := p.cacheRoutes(service)
@ -203,7 +201,7 @@ func (p *Proxy) getRoute(ctx context.Context, service string) ([]router.Route, e
func (p *Proxy) cacheRoutes(service string) ([]router.Route, error) { func (p *Proxy) cacheRoutes(service string) ([]router.Route, error) {
// lookup the routes in the router // lookup the routes in the router
results, err := p.Router.Lookup(router.QueryService(service)) results, err := p.Router.Lookup(router.QueryService(service), router.QueryNetwork("*"))
if err != nil { if err != nil {
// assumption that we're ok with stale routes // assumption that we're ok with stale routes
logger.Debugf("Failed to lookup route for %s: %v", service, err) logger.Debugf("Failed to lookup route for %s: %v", service, err)
@ -290,6 +288,7 @@ func (p *Proxy) watchRoutes() {
// route watcher // route watcher
w, err := p.Router.Watch() w, err := p.Router.Watch()
if err != nil { if err != nil {
logger.Debugf("Error watching router: %v", err)
return return
} }
defer w.Stop() defer w.Stop()
@ -297,6 +296,7 @@ func (p *Proxy) watchRoutes() {
for { for {
event, err := w.Next() event, err := w.Next()
if err != nil { if err != nil {
logger.Debugf("Error watching router: %v", err)
return return
} }