From a63480a81acdf4b548d5d2673a321c278a018a44 Mon Sep 17 00:00:00 2001 From: Dominic Wong Date: Wed, 1 Jul 2020 15:20:30 +0100 Subject: [PATCH] router/registry: fix fallback fails if service has been seen already (#1776) --- router/table.go | 12 +++++++----- router/table_test.go | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 5 deletions(-) diff --git a/router/table.go b/router/table.go index ad45c725..9508dd4b 100644 --- a/router/table.go +++ b/router/table.go @@ -98,6 +98,9 @@ func (t *table) Delete(r Route) error { } delete(t.routes[service], sum) + 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", Delete, r.Address) } @@ -253,8 +256,8 @@ func (t *table) Query(q ...QueryOption) ([]Route, error) { if opts.Service != "*" { // try and load services from the cache - if _, ok := t.routes[opts.Service]; ok { - return findRoutes(t.routes[opts.Service], opts.Address, opts.Gateway, opts.Network, opts.Router, opts.Strategy), nil + if routes, ok := t.routes[opts.Service]; ok && len(routes) > 0 { + return findRoutes(routes, opts.Address, opts.Gateway, opts.Network, opts.Router, opts.Strategy), nil } // load the cache and try again @@ -263,9 +266,8 @@ func (t *table) Query(q ...QueryOption) ([]Route, error) { return nil, err } t.RLock() - if _, ok := t.routes[opts.Service]; ok { - return findRoutes(t.routes[opts.Service], opts.Address, opts.Gateway, opts.Network, opts.Router, opts.Strategy), nil - + if routes, ok := t.routes[opts.Service]; ok && len(routes) > 0 { + return findRoutes(routes, opts.Address, opts.Gateway, opts.Network, opts.Router, opts.Strategy), nil } return nil, ErrRouteNotFound diff --git a/router/table_test.go b/router/table_test.go index 8b341123..7c9dd477 100644 --- a/router/table_test.go +++ b/router/table_test.go @@ -289,3 +289,44 @@ func TestQuery(t *testing.T) { t.Errorf("incorrect number of routes returned. Expected: %d, found: %d", 1, len(routes)) } } + +func TestFallback(t *testing.T) { + + r := &router{ + subscribers: make(map[string]chan *Advert), + options: DefaultOptions(), + } + route := Route{ + Service: "go.micro.service.foo", + Router: r.options.Id, + Link: DefaultLink, + Metric: DefaultLocalMetric, + } + r.table = newTable(func(s string) error { + r.table.Create(route) + return nil + }) + r.start() + + rts, err := r.Lookup(QueryService("go.micro.service.foo")) + if err != nil { + t.Errorf("error looking up service %s", err) + } + if len(rts) != 1 { + t.Errorf("incorrect number of routes returned %d", len(rts)) + } + + // deleting from the table but the next query should invoke the fallback that we passed during new table creation + if err := r.table.Delete(route); err != nil { + t.Errorf("error deleting route %s", err) + } + + rts, err = r.Lookup(QueryService("go.micro.service.foo")) + if err != nil { + t.Errorf("error looking up service %s", err) + } + if len(rts) != 1 { + t.Errorf("incorrect number of routes returned %d", len(rts)) + } + +}