Documentation
¶
Overview ¶
Package discovery is a minimal in-process service registry keyed by server name and concrete type, resolved by interface. Servers register their service implementations at start-up and handlers look them up by the interface they need, which decouples packages from concrete types.
Usage:
d := discovery.New()
if err := d.Register("api", &userService{}); err != nil {
return err
}
var svc UserService // an interface type
if err := d.Find("api", &svc); err != nil { // "" matches any server
return err
}
var closer io.Closer
err := d.ForEach(&closer, func(key string) error { // key is "<server>/<type>"
return closer.Close()
})
The registry is a plain map with no locking: perform all Register calls before concurrent Find/ForEach use. When several registered services implement the requested interface, Find returns an arbitrary one.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Discovery ¶
type Discovery interface {
// Register adds service under server; the key is "<server>/<concrete type>".
// It returns an error if the same server/type pair is already registered.
Register(server string, service any) error
// Find sets *v (v must be a non-nil pointer to an interface) to a
// registered service that implements that interface. server "" matches
// any server. It returns an error if v is not a pointer to interface or
// nothing matches.
Find(server string, v any) error
// ForEach sets *v to each registered service implementing the interface
// in turn and calls f with the registry key; the first error from f
// aborts iteration.
ForEach(v any, f func(typ string) error) error
}
Discovery is an in-process registry of service implementations, resolved by the interface a caller needs. Not safe for concurrent modification; register everything before concurrent lookups.