client/selector: query across multiple domains (#1725)

* client/selector: query across multiple domains

* client/selector: check for nil services

* config/cmd: fix merge bug
This commit is contained in:
ben-toogood 2020-06-25 15:40:23 +01:00 committed by GitHub
parent bc60f23ff6
commit a8fc5590a8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 8 deletions

View File

@ -46,17 +46,28 @@ func (c *registrySelector) Select(service string, opts ...SelectOption) (Next, e
opt(&sopts) opt(&sopts)
} }
// get the service // get the service. Because the service could be running in the current or the default domain,
// try the cache first // we call both. For example, go.micro.service.foo could be running in the services current domain,
// if that fails go directly to the registry // however the runtime (go.micro.runtime) will always be run in the default domain.
services, err := c.rc.GetService(service) services, err := c.rc.GetService(service, registry.GetDomain(c.so.Domain))
if err != nil { if err != nil && err != registry.ErrNotFound {
if err == registry.ErrNotFound {
return nil, ErrNotFound
}
return nil, err return nil, err
} }
if c.so.Domain != registry.DefaultDomain {
srvs, err := c.rc.GetService(service, registry.GetDomain(registry.DefaultDomain))
if err != nil && err != registry.ErrNotFound {
return nil, err
}
if err == nil {
services = append(services, srvs...)
}
}
if services == nil {
return nil, ErrNoneAvailable
}
// apply the filters // apply the filters
for _, filter := range sopts.Filters { for _, filter := range sopts.Filters {
services = filter(services) services = filter(services)

View File

@ -10,6 +10,9 @@ type Options struct {
Registry registry.Registry Registry registry.Registry
Strategy Strategy Strategy Strategy
// Domain to lookup services from within the registry
Domain string
// Other options for implementations of the interface // Other options for implementations of the interface
// can be stored in a context // can be stored in a context
Context context.Context Context context.Context
@ -37,6 +40,13 @@ func Registry(r registry.Registry) Option {
} }
} }
// Domain sets the domain used by the selector
func Domain(d string) Option {
return func(o *Options) {
o.Domain = d
}
}
// SetStrategy sets the default strategy for the selector // SetStrategy sets the default strategy for the selector
func SetStrategy(fn Strategy) Option { func SetStrategy(fn Strategy) Option {
return func(o *Options) { return func(o *Options) {