Documentation

Overview

    Package etcd provides an Instancer and Registrar implementation for etcd. If you use etcd as your service discovery system, this package will help you implement the registration and client-side load balancing patterns.

    Example
    Output:
    
    

    Index

    Examples

    Constants

    This section is empty.

    Variables

    View Source
    var (
    	// ErrNoKey indicates a client method needs a key but receives none.
    	ErrNoKey = errors.New("no key provided")
    
    	// ErrNoValue indicates a client method needs a value but receives none.
    	ErrNoValue = errors.New("no value provided")
    )

    Functions

    This section is empty.

    Types

    type Client

    type Client interface {
    	// GetEntries queries the given prefix in etcd and returns a slice
    	// containing the values of all keys found, recursively, underneath that
    	// prefix.
    	GetEntries(prefix string) ([]string, error)
    
    	// WatchPrefix watches the given prefix in etcd for changes. When a change
    	// is detected, it will signal on the passed channel. Clients are expected
    	// to call GetEntries to update themselves with the latest set of complete
    	// values. WatchPrefix will always send an initial sentinel value on the
    	// channel after establishing the watch, to ensure that clients always
    	// receive the latest set of values. WatchPrefix will block until the
    	// context passed to the NewClient constructor is terminated.
    	WatchPrefix(prefix string, ch chan struct{})
    
    	// Register a service with etcd.
    	Register(s Service) error
    
    	// Deregister a service with etcd.
    	Deregister(s Service) error
    }

      Client is a wrapper around the etcd client.

      func NewClient

      func NewClient(ctx context.Context, machines []string, options ClientOptions) (Client, error)

        NewClient returns Client with a connection to the named machines. It will return an error if a connection to the cluster cannot be made. The parameter machines needs to be a full URL with schemas. e.g. "http://localhost:2379" will work, but "localhost:2379" will not.

        type ClientOptions

        type ClientOptions struct {
        	Cert                    string
        	Key                     string
        	CACert                  string
        	DialTimeout             time.Duration
        	DialKeepAlive           time.Duration
        	HeaderTimeoutPerRequest time.Duration
        }

          ClientOptions defines options for the etcd client. All values are optional. If any duration is not specified, a default of 3 seconds will be used.

          type Instancer

          type Instancer struct {
          	// contains filtered or unexported fields
          }

            Instancer yields instances stored in a certain etcd keyspace. Any kind of change in that keyspace is watched and will update the Instancer's Instancers.

            func NewInstancer

            func NewInstancer(c Client, prefix string, logger log.Logger) (*Instancer, error)

              NewInstancer returns an etcd instancer. It will start watching the given prefix for changes, and update the subscribers.

              func (*Instancer) Deregister

              func (s *Instancer) Deregister(ch chan<- sd.Event)

                Deregister implements Instancer.

                func (*Instancer) Register

                func (s *Instancer) Register(ch chan<- sd.Event)

                  Register implements Instancer.

                  func (*Instancer) Stop

                  func (s *Instancer) Stop()

                    Stop terminates the Instancer.

                    type Registrar

                    type Registrar struct {
                    	// contains filtered or unexported fields
                    }

                      Registrar registers service instance liveness information to etcd.

                      func NewRegistrar

                      func NewRegistrar(client Client, service Service, logger log.Logger) *Registrar

                        NewRegistrar returns a etcd Registrar acting on the provided catalog registration (service).

                        func (*Registrar) Deregister

                        func (r *Registrar) Deregister()

                          Deregister implements the sd.Registrar interface. Call it when you want your service to be deregistered from etcd, typically just prior to shutdown.

                          func (*Registrar) Register

                          func (r *Registrar) Register()

                            Register implements the sd.Registrar interface. Call it when you want your service to be registered in etcd, typically at startup.

                            type Service

                            type Service struct {
                            	Key           string // unique key, e.g. "/service/foobar/1.2.3.4:8080"
                            	Value         string // returned to subscribers, e.g. "http://1.2.3.4:8080"
                            	TTL           *TTLOption
                            	DeleteOptions *etcd.DeleteOptions
                            }

                              Service holds the instance identifying data you want to publish to etcd. Key must be unique, and value is the string returned to subscribers, typically called the "instance" string in other parts of package sd.

                              type TTLOption

                              type TTLOption struct {
                              	// contains filtered or unexported fields
                              }

                                TTLOption allow setting a key with a TTL. This option will be used by a loop goroutine which regularly refreshes the lease of the key.

                                func NewTTLOption

                                func NewTTLOption(heartbeat, ttl time.Duration) *TTLOption

                                  NewTTLOption returns a TTLOption that contains proper TTL settings. Heartbeat is used to refresh the lease of the key periodically; its value should be at least 500ms. TTL defines the lease of the key; its value should be significantly greater than heartbeat.

                                  Good default values might be 3s heartbeat, 10s TTL.