Documentation
¶
Overview ¶
Package registry is a component for service discovery
Index ¶
- Constants
- Variables
- type Config
- type ConfigType
- type Event
- type EventType
- type Option
- type ProviderFunc
- type Registry
- type Result
- type ServiceNode
- type Type
- func New(configData map[string]any, components *types.Components, logger log.Logger, ...) (Type, error)
- func Provide(svcCtx *cli.ServiceContextWithConfig, components *types.Components, ...) (Type, error)
- func ProvideNoOpts(svcCtx *cli.ServiceContextWithConfig, components *types.Components, ...) (Type, error)
- type WatchOption
- type WatchOptions
- type Watcher
Constants ¶
const ComponentType = "registry"
ComponentType is the components name.
Variables ¶
var ( // DefaultConfigSection is the section in the config to use. DefaultConfigSection = "registry" // DefaultRegistry is the default registry to use. DefaultRegistry = "mdns" // DefaultTimeout is the default timeout for the registry. DefaultTimeout = config.Duration(500 * time.Millisecond) )
var ( // ErrNotFound is a not found error when GetService is called. ErrNotFound = errors.New("service not found") // ErrWatcherStopped is a error when watcher is stopped. ErrWatcherStopped = errors.New("watcher stopped") )
var Plugins = container.NewMap[string, ProviderFunc]()
Plugins is the plugins container for registries.
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct {
Plugin string `json:"plugin,omitempty" yaml:"plugin,omitempty"`
Timeout config.Duration `json:"timeout,omitempty" yaml:"timeout,omitempty"`
}
Config is the configuration that can be used in a registry.
type ConfigType ¶
type ConfigType interface {
// contains filtered or unexported methods
}
ConfigType is used in the functional options as type to identify a registry option. It is used over a static *Config type as this way plugins can also easilty set functional options without the complication of contexts, as was done in v4. This is possible because plugins will nest the registry.Config type, and thus inherit the interface that is used to identify the registry config.
Plugin specific option example:
// WithLogger option located in the MDNS registry package.
func WithLogger(logger log.Logger) registry.Option {
return func(c registry.ConfigType) {
// The config type used here is *mdns.Config
cfg, ok := c.(*Config)
if ok {
cfg.Logger = logger
}
}
}
type Event ¶
type Event struct {
// ID is registry id
ID string
// Type defines type of event
Type EventType
// Timestamp is event timestamp
Timestamp time.Time
// Service is registry service
Service ServiceNode
}
Event is registry event.
type Option ¶
type Option func(ConfigType)
Option is a functional option type for the registry.
func WithPlugin ¶ added in v0.1.0
WithPlugin set the implementation to use.
func WithTimeout ¶
WithTimeout sets the default registry timeout used.
type ProviderFunc ¶
type ProviderFunc func( configData map[string]any, components *types.Components, logger log.Logger, opts ...Option, ) (Type, error)
ProviderFunc is provider function type used by plugins to create a new registry.
type Registry ¶
type Registry interface {
types.Component
// Register registers a service within the registry.
Register(ctx context.Context, srv ServiceNode) error
// Deregister deregisters a service within the registry.
Deregister(ctx context.Context, srv ServiceNode) error
// GetService returns a service from the registry.
// Leave schemes empty to get all schemes.
GetService(ctx context.Context, namespace, region, name string, schemes []string) ([]ServiceNode, error)
// ListServices lists services within the registry.
// Leave schemes empty to get all schemes.
ListServices(ctx context.Context, namespace, region string, schemes []string) ([]ServiceNode, error)
// Watch returns a Watcher which you can watch on.
Watch(ctx context.Context, opts ...WatchOption) (Watcher, error)
}
Registry is a component for service discovery.
type Result ¶
type Result struct {
Action EventType `json:"action"`
Node ServiceNode `json:"node"`
}
Result is returned by a call to Next on the watcher. Actions can be create, update, delete.
type ServiceNode ¶ added in v0.3.0
type ServiceNode struct {
// Name is the name of the service. Should be DNS compatible.
Name string `json:"name,omitempty"`
// Version is the version of the service.
Version string `json:"version,omitempty"`
// Metadata is the metadata of the service.
Metadata map[string]string `json:"metadata,omitempty"`
// Node is the name of the node, this is normally the entrypoint name.
Node string `json:"node,omitempty"`
// Network is the network of the service, tcp, udp or unix.
// Empty is tcp and the default.
Network string `json:"network,omitempty"`
// Scheme is the scheme of the service.
Scheme string `json:"scheme,omitempty"`
// Address is the address of the service.
Address string `json:"address,omitempty"`
// Namespace is the namespace of the node.
Namespace string `json:"namespace,omitempty"`
// Region is the region of the node.
Region string `json:"region,omitempty"`
// TTL is the time to live for the service.
// Keep it 0 if you don't want to use TTL.
TTL time.Duration `json:"ttl,omitempty"`
}
ServiceNode is a service node.
func (ServiceNode) String ¶ added in v0.3.0
func (r ServiceNode) String() string
func (ServiceNode) Valid ¶ added in v0.3.0
func (r ServiceNode) Valid() error
Valid checks if a serviceNode has a valid namespace, region, and name.
lowercase ascii characters, numbers, hyphens, and periods are allowed.
type Type ¶ added in v0.1.0
type Type struct {
Registry
}
Type is the registry type it is returned when you use ProvideRegistry which selects a registry to use based on the plugin configuration.
func New ¶ added in v0.3.0
func New( configData map[string]any, components *types.Components, logger log.Logger, opts ...Option, ) (Type, error)
New creates a new registry without side-effects.
func Provide ¶ added in v0.1.0
func Provide( svcCtx *cli.ServiceContextWithConfig, components *types.Components, logger log.Logger, opts ...Option, ) (Type, error)
Provide is the registry provider for wire. It parses the config from "configs", fetches the "Plugin" from the config and then forwards all it's arguments to the factory which it get's from "Plugins".
func ProvideNoOpts ¶ added in v0.3.0
func ProvideNoOpts( svcCtx *cli.ServiceContextWithConfig, components *types.Components, logger log.Logger, ) (Type, error)
ProvideNoOpts is the registry provider for wire without options.
type WatchOption ¶
type WatchOption func(*WatchOptions)
WatchOption is functional option type for the watch config.
func WatchService ¶
func WatchService(name string) WatchOption
WatchService sets a service name to watch.
type WatchOptions ¶
type WatchOptions struct {
// Specify a service to watch
// If blank, the watch is for all services
Service string
}
WatchOptions are the options used by the registry watcher.