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()
})
Register, Find, and ForEach may run concurrently. ForEach visits a snapshot of matching services, so callbacks may register more services without changing the current iteration. Callers must synchronize destination values shared between goroutines. 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 for a nil service or 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. Its methods are safe for concurrent use. Callers must synchronize access to destination values passed to Find and ForEach if they share them between goroutines.