kit: Index | Files | Directories

package sd

import ""

Package sd provides utilities related to service discovery. That includes the client-side loadbalancer pattern, where a microservice subscribes to a service discovery system in order to reach remote instances; as well as the registrator pattern, where a microservice registers itself in a service discovery system. Implementations are provided for most common systems.


Package Files

doc.go endpoint_cache.go endpointer.go factory.go instancer.go registrar.go

type DefaultEndpointer Uses

type DefaultEndpointer struct {
    // contains filtered or unexported fields

DefaultEndpointer implements an Endpointer interface. When created with NewEndpointer function, it automatically registers as a subscriber to events from the Instances and maintains a list of active Endpoints.

func NewEndpointer Uses

func NewEndpointer(src Instancer, f Factory, logger log.Logger, options ...EndpointerOption) *DefaultEndpointer

NewEndpointer creates an Endpointer that subscribes to updates from Instancer src and uses factory f to create Endpoints. If src notifies of an error, the Endpointer keeps returning previously created Endpoints assuming they are still good, unless this behavior is disabled via InvalidateOnError option.

func (*DefaultEndpointer) Close Uses

func (de *DefaultEndpointer) Close()

Close deregisters DefaultEndpointer from the Instancer and stops the internal go-routine.

func (*DefaultEndpointer) Endpoints Uses

func (de *DefaultEndpointer) Endpoints() ([]endpoint.Endpoint, error)

Endpoints implements Endpointer.

type Endpointer Uses

type Endpointer interface {
    Endpoints() ([]endpoint.Endpoint, error)

Endpointer listens to a service discovery system and yields a set of identical endpoints on demand. An error indicates a problem with connectivity to the service discovery system, or within the system itself; an Endpointer may yield no endpoints without error.

type EndpointerOption Uses

type EndpointerOption func(*endpointerOptions)

EndpointerOption allows control of endpointCache behavior.

func InvalidateOnError Uses

func InvalidateOnError(timeout time.Duration) EndpointerOption

InvalidateOnError returns EndpointerOption that controls how the Endpointer behaves when then Instancer publishes an Event containing an error. Without this option the Endpointer continues returning the last known endpoints. With this option, the Endpointer continues returning the last known endpoints until the timeout elapses, then closes all active endpoints and starts returning an error. Once the Instancer sends a new update with valid resource instances, the normal operation is resumed.

type Event Uses

type Event struct {
    Instances []string
    Err       error

Event represents a push notification generated from the underlying service discovery implementation. It contains either a full set of available resource instances, or an error indicating some issue with obtaining information from discovery backend. Examples of errors may include loosing connection to the discovery backend, or trying to look up resource instances using an incorrectly formatted key. After receiving an Event with an error the listenter should treat previously discovered resource instances as stale (although it may choose to continue using them). If the Instancer is able to restore connection to the discovery backend it must push another Event with the current set of resource instances.

type Factory Uses

type Factory func(instance string) (endpoint.Endpoint, io.Closer, error)

Factory is a function that converts an instance string (e.g. host:port) to a specific endpoint. Instances that provide multiple endpoints require multiple factories. A factory also returns an io.Closer that's invoked when the instance goes away and needs to be cleaned up. Factories may return nil closers.

Users are expected to provide their own factory functions that assume specific transports, or can deduce transports by parsing the instance string.

type FixedEndpointer Uses

type FixedEndpointer []endpoint.Endpoint

FixedEndpointer yields a fixed set of endpoints.

func (FixedEndpointer) Endpoints Uses

func (s FixedEndpointer) Endpoints() ([]endpoint.Endpoint, error)

Endpoints implements Endpointer.

type FixedInstancer Uses

type FixedInstancer []string

FixedInstancer yields a fixed set of instances.

func (FixedInstancer) Deregister Uses

func (d FixedInstancer) Deregister(ch chan<- Event)

Deregister implements Instancer.

func (FixedInstancer) Register Uses

func (d FixedInstancer) Register(ch chan<- Event)

Register implements Instancer.

func (FixedInstancer) Stop Uses

func (d FixedInstancer) Stop()

Stop implements Instancer.

type Instancer Uses

type Instancer interface {
    Register(chan<- Event)
    Deregister(chan<- Event)

Instancer listens to a service discovery system and notifies registered observers of changes in the resource instances. Every event sent to the channels contains a complete set of instances known to the Instancer. That complete set is sent immediately upon registering the channel, and on any future updates from discovery system.

type Registrar Uses

type Registrar interface {

Registrar registers instance information to a service discovery system when an instance becomes alive and healthy, and deregisters that information when the service becomes unhealthy or goes away.

Registrar implementations exist for various service discovery systems. Note that identifying instance information (e.g. host:port) must be given via the concrete constructor; this interface merely signals lifecycle changes.


consulPackage consul provides Instancer and Registrar implementations for Consul.
dnssrvPackage dnssrv provides an Instancer implementation for DNS SRV records.
etcdPackage etcd provides an Instancer and Registrar implementation for etcd.
etcdv3Package etcdv3 provides an Instancer and Registrar implementation for etcd v3.
eurekaPackage eureka provides Instancer and Registrar implementations for Netflix OSS's Eureka
lbPackage lb implements the client-side load balancer pattern.
zkPackage zk provides Instancer and Registrar implementations for ZooKeeper.

Package sd imports 6 packages (graph) and is imported by 525 packages. Updated 2020-09-13. Refresh now. Tools for package owners.