router/registry: fix fallback fails if service has been seen already (#1776)

This commit is contained in:
Dominic Wong 2020-07-01 15:20:30 +01:00 committed by GitHub
parent 6d9d94b105
commit a63480a81a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 48 additions and 5 deletions

View File

@ -98,6 +98,9 @@ func (t *table) Delete(r Route) error {
} }
delete(t.routes[service], sum) delete(t.routes[service], sum)
if len(t.routes[service]) == 0 {
delete(t.routes, service)
}
if logger.V(logger.DebugLevel, logger.DefaultLogger) { if logger.V(logger.DebugLevel, logger.DefaultLogger) {
logger.Debugf("Router emitting %s for route: %s", Delete, r.Address) 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 != "*" { if opts.Service != "*" {
// try and load services from the cache // try and load services from the cache
if _, ok := t.routes[opts.Service]; ok { if routes, ok := t.routes[opts.Service]; ok && len(routes) > 0 {
return findRoutes(t.routes[opts.Service], opts.Address, opts.Gateway, opts.Network, opts.Router, opts.Strategy), nil return findRoutes(routes, opts.Address, opts.Gateway, opts.Network, opts.Router, opts.Strategy), nil
} }
// load the cache and try again // load the cache and try again
@ -263,9 +266,8 @@ func (t *table) Query(q ...QueryOption) ([]Route, error) {
return nil, err return nil, err
} }
t.RLock() t.RLock()
if _, ok := t.routes[opts.Service]; ok { if routes, ok := t.routes[opts.Service]; ok && len(routes) > 0 {
return findRoutes(t.routes[opts.Service], opts.Address, opts.Gateway, opts.Network, opts.Router, opts.Strategy), nil return findRoutes(routes, opts.Address, opts.Gateway, opts.Network, opts.Router, opts.Strategy), nil
} }
return nil, ErrRouteNotFound return nil, ErrRouteNotFound

View File

@ -289,3 +289,44 @@ func TestQuery(t *testing.T) {
t.Errorf("incorrect number of routes returned. Expected: %d, found: %d", 1, len(routes)) 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))
}
}