Skip to content

Commit

Permalink
client/selector: query across multiple domains (micro#1725)
Browse files Browse the repository at this point in the history
* client/selector: query across multiple domains

* client/selector: check for nil services

* config/cmd: fix merge bug
  • Loading branch information
ben-toogood authored Jun 25, 2020
1 parent bc60f23 commit a8fc559
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
27 changes: 19 additions & 8 deletions client/selector/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,28 @@ func (c *registrySelector) Select(service string, opts ...SelectOption) (Next, e
opt(&sopts)
}

// get the service
// try the cache first
// if that fails go directly to the registry
services, err := c.rc.GetService(service)
if err != nil {
if err == registry.ErrNotFound {
return nil, ErrNotFound
}
// get the service. Because the service could be running in the current or the default domain,
// we call both. For example, go.micro.service.foo could be running in the services current domain,
// however the runtime (go.micro.runtime) will always be run in the default domain.
services, err := c.rc.GetService(service, registry.GetDomain(c.so.Domain))
if err != nil && err != registry.ErrNotFound {
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
for _, filter := range sopts.Filters {
services = filter(services)
Expand Down
10 changes: 10 additions & 0 deletions client/selector/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ type Options struct {
Registry registry.Registry
Strategy Strategy

// Domain to lookup services from within the registry
Domain string

// Other options for implementations of the interface
// can be stored in a context
Context context.Context
Expand Down Expand Up @@ -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
func SetStrategy(fn Strategy) Option {
return func(o *Options) {
Expand Down

0 comments on commit a8fc559

Please sign in to comment.