Documentation
¶
Index ¶
- type Election
- func (e *Election) Campaign(ctx context.Context) error
- func (e *Election) Done() <-chan struct{}
- func (e *Election) IsLeader() bool
- func (e *Election) Leader(ctx context.Context) (string, error)
- func (e *Election) Observe(ctx context.Context) <-chan string
- func (e *Election) Resign(ctx context.Context) error
- type ElectionOptions
- type EtcdRegistry
- func (e *EtcdRegistry) Close() error
- func (e *EtcdRegistry) DeleteServiceByPrefix(ctx context.Context, namePrefix string) (int64, error)
- func (e *EtcdRegistry) DeleteServiceKey(ctx context.Context, name, id string) error
- func (e *EtcdRegistry) Deregister() error
- func (e *EtcdRegistry) GetNamespaceServices(ctx context.Context) ([]*ServiceInfo, error)
- func (e *EtcdRegistry) GetService(ctx context.Context, name string) ([]*ServiceInfo, error)
- func (e *EtcdRegistry) NewElection(opts ElectionOptions) (*Election, error)
- func (e *EtcdRegistry) Register(ctx context.Context, info *ServiceInfo) error
- func (e *EtcdRegistry) WatchNamespace(ctx context.Context, callback func([]*ServiceInfo)) error
- func (e *EtcdRegistry) WatchService(ctx context.Context, name string, callback func([]*ServiceInfo)) error
- type LogLevel
- type Logger
- type Option
- func WithAuth(username, password string) Option
- func WithDialTimeout(timeout time.Duration) Option
- func WithKeyPrefix(prefix string) Option
- func WithLogLevel(level LogLevel) Option
- func WithLogger(logger Logger) Option
- func WithLoggerAndLevel(logger interface{ ... }, level LogLevel) Option
- func WithNamespace(namespace string) Option
- func WithTTL(ttl int64) Option
- type Options
- type ServiceInfo
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Election ¶ added in v0.1.0
type Election struct {
// contains filtered or unexported fields
}
Election manages a leadership election campaign.
func (*Election) Campaign ¶ added in v0.1.0
Campaign puts a candidate into a leadership election. This method blocks until the candidate wins the election or the context is cancelled. After winning, it will hold the leadership until the context is cancelled, the underlying session is closed, or Resign is called.
func (*Election) Done ¶ added in v0.1.0
func (e *Election) Done() <-chan struct{}
Done returns a channel that is closed when the underlying session is lost. This indicates that leadership is lost and the candidate should stop any leader-specific work.
func (*Election) IsLeader ¶ added in v0.1.0
IsLeader returns true if this candidate is currently the leader. Note: This method performs a network call to etcd to get the current leader.
func (*Election) Leader ¶ added in v0.1.0
Leader returns the current leader's proposal. This is a non-blocking call and may return an error if the leader is not yet known.
type ElectionOptions ¶ added in v0.1.0
type ElectionOptions struct {
// ElectionName is the name of the election, which will be used as a prefix for etcd keys.
ElectionName string
// Proposal is the value this candidate proposes for the election.
// If it's empty, a default value will be generated.
Proposal string
// TTL is the session TTL in seconds for the leadership election.
TTL int
}
ElectionOptions holds configuration for an Election.
type EtcdRegistry ¶
type EtcdRegistry struct {
// contains filtered or unexported fields
}
EtcdRegistry provides service registration and discovery using etcd.
func NewEtcdRegistry ¶
func NewEtcdRegistry(endpoints []string, opts ...Option) (*EtcdRegistry, error)
NewEtcdRegistry creates a new EtcdRegistry instance.
func (*EtcdRegistry) Close ¶
func (e *EtcdRegistry) Close() error
Close safely shuts down the registry.
func (*EtcdRegistry) DeleteServiceByPrefix ¶
DeleteServiceByPrefix deletes multiple service registration keys by prefix.
func (*EtcdRegistry) DeleteServiceKey ¶
func (e *EtcdRegistry) DeleteServiceKey(ctx context.Context, name, id string) error
DeleteServiceKey manually deletes a service registration key.
func (*EtcdRegistry) Deregister ¶
func (e *EtcdRegistry) Deregister() error
Deregister unregisters the service from etcd.
func (*EtcdRegistry) GetNamespaceServices ¶ added in v0.2.1
func (e *EtcdRegistry) GetNamespaceServices(ctx context.Context) ([]*ServiceInfo, error)
GetNamespaceServices retrieves all service instances in the current namespace.
func (*EtcdRegistry) GetService ¶
func (e *EtcdRegistry) GetService(ctx context.Context, name string) ([]*ServiceInfo, error)
GetService retrieves all instances of a specific service.
func (*EtcdRegistry) NewElection ¶ added in v0.1.0
func (e *EtcdRegistry) NewElection(opts ElectionOptions) (*Election, error)
NewElection creates a new Election instance for a leadership campaign. The proposal is the value that this candidate is putting forward for the election. Typically this would be the candidate's own ID or address.
func (*EtcdRegistry) Register ¶
func (e *EtcdRegistry) Register(ctx context.Context, info *ServiceInfo) error
Register registers a service with etcd.
func (*EtcdRegistry) WatchNamespace ¶ added in v0.2.1
func (e *EtcdRegistry) WatchNamespace(ctx context.Context, callback func([]*ServiceInfo)) error
WatchNamespace watches for all service changes in the current namespace and triggers a callback with the full service instance list in that namespace.
func (*EtcdRegistry) WatchService ¶
func (e *EtcdRegistry) WatchService(ctx context.Context, name string, callback func([]*ServiceInfo)) error
WatchService watches for changes in a service and triggers a callback.
type Logger ¶
type Logger interface {
Errorf(format string, v ...interface{})
Warnf(format string, v ...interface{})
Infof(format string, v ...interface{})
Debugf(format string, v ...interface{})
}
Logger defines the interface for logging. This allows users to use their own logger.
type Option ¶
type Option func(*Options)
Option configures an EtcdRegistry.
func WithDialTimeout ¶
WithDialTimeout sets the dial timeout for the etcd client.
func WithKeyPrefix ¶
WithKeyPrefix sets the root prefix for all keys.
func WithLogLevel ¶ added in v0.2.0
WithLogLevel sets the log level for the registry.
func WithLogger ¶
WithLogger sets the logger for the registry.
func WithLoggerAndLevel ¶ added in v0.2.0
func WithLoggerAndLevel(logger interface {
Printf(format string, v ...interface{})
}, level LogLevel) Option
WithLoggerAndLevel sets both the logger and log level for the registry. This is a convenience function that wraps the provided logger with a leveled logger.
func WithNamespace ¶
WithNamespace sets the namespace for service discovery.
type Options ¶
type Options struct {
Namespace string
TTL int64
Username string
Password string
DialTimeout time.Duration
DialKeepAliveTime time.Duration
KeyPrefix string
Logger Logger
LogLevel LogLevel
}
Options holds configuration for the EtcdRegistry.
type ServiceInfo ¶
type ServiceInfo struct {
Name string `json:"name"`
ID string `json:"id"`
Address string `json:"address"`
Port string `json:"port"`
Version string `json:"version"`
Weight int `json:"weight"`
Metadata map[string]string `json:"metadata"`
}
ServiceInfo holds information about a registered service.