router/registry: fix fallback fails if service has been seen already (#1776)
This commit is contained in:
parent
6d9d94b105
commit
a63480a81a
@ -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
|
||||
|
@ -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))
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user