registry

package
v0.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 9, 2023 License: Apache-2.0 Imports: 7 Imported by: 18

Documentation

Overview

Package registry is a component for service discovery

Index

Constants

View Source
const ComponentType = "registry"

ComponentType is the registry component type name.

Variables

View Source
var (
	DefaultRegistry = "mdns"
	DefaultTimeout  = 100
)
View Source
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")
)

Plugins is the plugins container for registry.

Functions

This section is empty.

Types

type Config

type Config struct {
	Plugin  string     `json:"plugin,omitempty" yaml:"plugin,omitempty"`
	Timeout int        `json:"timeout,omitempty" yaml:"timeout,omitempty"`
	Logger  log.Logger `json:"logger,omitempty" yaml:"logger,omitempty"`
}

Config is the configuration that can be used in a registry.

func NewConfig

func NewConfig() Config

NewConfig creates a new default config to use with a registry.

func (*Config) ApplyOptions

func (c *Config) ApplyOptions(opts ...Option)

ApplyOptions applies a set of options to the config.

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 DeregisterOption

type DeregisterOption func(*DeregisterOptions)

DeregisterOption is functional option type for the deregister config.

func DeregisterContext

func DeregisterContext(ctx context.Context) DeregisterOption

DeregisterContext is the context used to deregister a service.

type DeregisterOptions

type DeregisterOptions struct {
	Context context.Context
}

DeregisterOptions are the options used to deregister services.

type Endpoint

type Endpoint struct {
	Name     string            `json:"name"`
	Request  *Value            `json:"request"`
	Response *Value            `json:"response"`
	Metadata map[string]string `json:"metadata"`
}

Endpoint represents a service endpoint in a registry.

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 *Service
}

Event is registry event.

type EventType

type EventType int

EventType defines registry event type.

const (
	// Create is emitted when a new service is registered.
	Create EventType = iota
	// Delete is emitted when an existing service is deregsitered.
	Delete
	// Update is emitted when an existing servicec is updated.
	Update
)

func (EventType) String

func (t EventType) String() string

String returns human readable event type.

type GetOption

type GetOption func(*GetOptions)

GetOption is functional option type for the get config.

func GetContext

func GetContext(ctx context.Context) GetOption

GetContext is the context used when fetching a service.

type GetOptions

type GetOptions struct {
	Context context.Context
}

GetOptions are the options used to fetch a service.

type ListOption

type ListOption func(*ListOptions)

ListOption is functional option type for the list config.

func ListContext

func ListContext(ctx context.Context) ListOption

ListContext is the context used when listing a service.

type ListOptions

type ListOptions struct {
	Context context.Context
}

ListOptions are the options used to list services.

type MicroRegistry

type MicroRegistry struct {
	Registry
}

MicroRegistry is the registry type is returned when you use the dynamic registry provider that selects a registry to use based on the plugin configuration.

type Node

type Node struct {
	ID string `json:"id"`
	// ip:port
	Address string `json:"address"`
	// frpc/grpc/http uvm., since v5!
	Scheme   string            `json:"scheme"`
	Metadata map[string]string `json:"metadata"`
}

Node represents a service node in a registry. One service can be comprised of multiple nodes.

type Option

type Option func(ConfigType)

Option is a functional option type for the registry.

func WithLogger

func WithLogger(logger log.Logger) Option

WithLogger sets a specific logger to use.

func WithTimeout

func WithTimeout(timeout int) Option

WithTimeout sets the default registry timeout used.

type ProviderFunc

type ProviderFunc func(
	name types.ServiceName,
	data types.ConfigData,
	logger log.Logger,
	opts ...Option,
) (*MicroRegistry, error)

ProviderFunc is provider function type used by plugins to create a new registry.

type RegisterOption

type RegisterOption func(*RegisterOptions)

RegisterOption is functional option type for the register config.

func RegisterContext

func RegisterContext(ctx context.Context) RegisterOption

RegisterContext sets the context that is used when registering a service.

func RegisterTTL

func RegisterTTL(t time.Duration) RegisterOption

RegisterTTL sets the TTL for service registration.

type RegisterOptions

type RegisterOptions struct {
	TTL time.Duration
	// Other options for implementations of the interface
	// can be stored in a context
	Context context.Context
}

RegisterOptions are the options used to register services.

type Registry

type Registry interface {
	types.Component

	// Register registers a service within the registry.
	Register(*Service, ...RegisterOption) error

	// Deregister deregisters a service within the registry.
	Deregister(*Service, ...DeregisterOption) error

	// GetService returns a service from the registry.
	GetService(string, ...GetOption) ([]*Service, error)

	// ListServices lists services within the registry.
	ListServices(...ListOption) ([]*Service, error)

	// Watch returns a Watcher which you can watch on.
	Watch(...WatchOption) (Watcher, error)
}

Registry provides an interface for service discovery and an abstraction over varying implementations {consul, etcd, zookeeper, ...}.

type Result

type Result struct {
	Action  string
	Service *Service
}

Result is returned by a call to Next on the watcher. Actions can be create, update, delete.

type Service

type Service struct {
	Name      string            `json:"name"`
	Version   string            `json:"version"`
	Metadata  map[string]string `json:"metadata"`
	Endpoints []*Endpoint       `json:"endpoints"`
	Nodes     []*Node           `json:"nodes"`
}

Service represents a service in a registry.

type Value

type Value struct {
	Name   string   `json:"name"`
	Type   string   `json:"type"`
	Values []*Value `json:"values"`
}

Value is a value container used in the registry.

type WatchOption

type WatchOption func(*WatchOptions)

WatchOption is functional option type for the watch config.

func WatchContext

func WatchContext(ctx context.Context) WatchOption

WatchContext sets a context that is used to watch.

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
	// Other options for implementations of the interface
	// can be stored in a context
	Context context.Context
}

WatchOptions are the options used by the registry watcher.

type Watcher

type Watcher interface {
	// Next is a blocking call
	Next() (*Result, error)
	Stop() error
}

Watcher is an interface that returns updates about services within the registry.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL