server

package
v0.0.0-...-c283e9f Latest Latest
Warning

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

Go to latest
Published: Sep 4, 2021 License: BSD-3-Clause Imports: 31 Imported by: 2

Documentation

Index

Constants

View Source
const (
	// DefaultInstanceTTL is the length of time after a heartbeat from an instance before it expires.
	DefaultInstanceTTL = 10 * time.Second

	// DefaultExpiryCheckInterval is the default interval between checks for expired instances.
	DefaultExpiryCheckInterval = 1 * time.Second
)
View Source
const StoreHdr = byte('\xff')

StoreHdr is the header byte used by the multiplexer.

View Source
const StreamBufferSize = 64 // TODO: Figure out how big this buffer should be.

StreamBufferSize is the size of the channel buffer used for event subscription.

Variables

View Source
var (
	ErrUnsetService = errors.New("discoverd: service name must not be empty")

	ErrInvalidService = errors.New("discoverd: service must be lowercase alphanumeric plus dash")

	ErrSendBlocked = errors.New("discoverd: channel send failed due to blocked receiver")

	ErrListenerRequired = errors.New("discoverd: listener required")

	ErrAdvertiseRequired = errors.New("discoverd: advertised address required")

	// ErrNotLeader is returned when performing an operation on the store when
	// it is not the current cluster leader.
	ErrNotLeader = errors.New("discoverd: not leader")

	// ErrNoKnownLeader is returned when there is not a current know cluster leader.
	ErrNoKnownLeader = errors.New("discoverd: no known leader")

	// ErrLeaderWait is returned when trying to expire instances when the store
	// hasn't been leader for long enough.
	ErrLeaderWait = errors.New("discoverd: new leader, waiting for 2x TTL")

	ErrShutdown = errors.New("discoverd: shutting down")
)
View Source
var DefaultServiceConfig = &discoverd.ServiceConfig{
	LeaderType: discoverd.LeaderTypeOldest,
}

DefaultServiceConfig is the default configuration for a service when one is not specified.

Functions

func IsNotFound

func IsNotFound(err error) bool

func IsServiceExists

func IsServiceExists(err error) bool

func ValidServiceName

func ValidServiceName(service string) error

ValidServiceName returns nil if service is valid. Otherwise returns an error.

Types

type DNSServer

type DNSServer struct {
	UDPAddr   string
	TCPAddr   string
	Domain    string
	Recursors []string
	// contains filtered or unexported fields
}

func (*DNSServer) Close

func (srv *DNSServer) Close() error

func (*DNSServer) GetStore

func (srv *DNSServer) GetStore() DNSStore

func (*DNSServer) ListenAndServe

func (srv *DNSServer) ListenAndServe() error

func (*DNSServer) SetStore

func (srv *DNSServer) SetStore(s DNSStore)

type DNSStore

type DNSStore interface {
	Instances(service string) ([]*discoverd.Instance, error)
	ServiceLeader(service string) (*discoverd.Instance, error)
}

type Handler

type Handler struct {
	http.Handler
	Shutdown atomic.Value // bool
	Proxy    atomic.Value // bool
	Main     interface {
		Deregister() error
		Close() (dt.TargetLogIndex, error)
		Promote() error
		Demote() error
	}
	Store interface {
		Leader() string
		AddService(service string, config *discoverd.ServiceConfig) error
		RemoveService(service string) error
		SetServiceMeta(service string, meta *discoverd.ServiceMeta) error
		ServiceMeta(service string) *discoverd.ServiceMeta
		AddInstance(service string, inst *discoverd.Instance) error
		RemoveInstance(service, id string) error
		Instances(service string) ([]*discoverd.Instance, error)
		Config(service string) *discoverd.ServiceConfig
		SetServiceLeader(service, id string) error
		ServiceLeader(service string) (*discoverd.Instance, error)
		Subscribe(service string, sendCurrent bool, kinds discoverd.EventKind, ch chan *discoverd.Event) stream.Stream

		AddPeer(peer string) error
		RemovePeer(peer string) error
		GetPeers() ([]string, error)
		LastIndex() uint64
	}
	Peers []string
}

Handler represents an HTTP handler for the Store.

func NewHandler

func NewHandler(proxy bool, peers []string) *Handler

NewHandler returns a new instance of Handler.

func (*Handler) ServeHTTP

func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request)

type NotFoundError

type NotFoundError struct {
	Service  string
	Instance string
}

func (NotFoundError) Error

func (e NotFoundError) Error() string

type ProxyStore

type ProxyStore struct {
	Peers []string
}

ProxyStore implements some of the Store methods as proxy calls. Only the subset of methods required for DNSServer.Store are implemented.

func (*ProxyStore) Instances

func (s *ProxyStore) Instances(service string) ([]*discoverd.Instance, error)

Instances returns a list of instances for a service.

func (*ProxyStore) ServiceLeader

func (s *ProxyStore) ServiceLeader(service string) (*discoverd.Instance, error)

ServiceLeader returns the leader for a service.

type ServiceExistsError

type ServiceExistsError string

func (ServiceExistsError) Error

func (e ServiceExistsError) Error() string

type Store

type Store struct {

	// The underlying network listener.
	Listener net.Listener

	// The address the raft server uses to represent itself in the peer list.
	Advertise net.Addr

	// Raft settings.
	HeartbeatTimeout   time.Duration
	ElectionTimeout    time.Duration
	LeaderLeaseTimeout time.Duration
	CommitTimeout      time.Duration
	EnableSingleNode   bool

	// The writer where logs are written to.
	LogOutput io.Writer

	// The duration without a heartbeat before an instance is expired.
	InstanceTTL time.Duration

	// The interval between checks for instance expiry on the leader.
	ExpiryCheckInterval time.Duration

	// Returns the current time.
	// This defaults to time.Now and can be changed for mocking.
	Now func() time.Time
	// contains filtered or unexported fields
}

Store represents a storage backend using the raft protocol.

func NewStore

func NewStore(path string) *Store

NewStore returns an instance of Store.

func (*Store) AddInstance

func (s *Store) AddInstance(service string, inst *discoverd.Instance) error

func (*Store) AddPeer

func (s *Store) AddPeer(peer string) error

AddPeer adds a peer to the raft cluster. Panic if store is not open yet.

func (*Store) AddService

func (s *Store) AddService(service string, config *discoverd.ServiceConfig) error

AddService creates a service with a configuration. Returns an error if the service already exists.

func (*Store) Apply

func (s *Store) Apply(l *raft.Log) interface{}

func (*Store) Close

func (s *Store) Close() (lastIdx uint64, err error)

Close shuts down the transport and store.

func (*Store) Config

func (s *Store) Config(service string) *discoverd.ServiceConfig

Config returns the configuration for service.

func (*Store) EnforceExpiry

func (s *Store) EnforceExpiry() error

EnforceExpiry checks all instances for expiration and issues an expiration command, if necessary. This function returns raft.ErrNotLeader if this store is not the current leader.

func (*Store) GetPeers

func (s *Store) GetPeers() ([]string, error)

GetPeers returns a list of peers in the raft cluster.

func (*Store) Instances

func (s *Store) Instances(service string) ([]*discoverd.Instance, error)

Instances returns a list of instances for service.

func (*Store) IsLeader

func (s *Store) IsLeader() bool

IsLeader returns true if the store is currently the leader.

func (*Store) LastIndex

func (s *Store) LastIndex() uint64

func (*Store) Leader

func (s *Store) Leader() string

Leader returns the host of the current leader. Returns empty string if there is no leader. Panic if called before store is opened.

func (*Store) LeaderCh

func (s *Store) LeaderCh() <-chan bool

LeaderCh returns a channel that signals leadership change. Panic if called before store is opened.

func (*Store) Open

func (s *Store) Open() error

Open starts the raft consensus and opens the store.

func (*Store) Path

func (s *Store) Path() string

Path returns the path that the store was initialized with.

func (*Store) RemoveInstance

func (s *Store) RemoveInstance(service, id string) error

func (*Store) RemovePeer

func (s *Store) RemovePeer(peer string) error

RemovePeer removes a peer from the raft cluster. Panic if store is not open yet.

func (*Store) RemoveService

func (s *Store) RemoveService(service string) error

RemoveService deletes the service from the store.

func (*Store) Restore

func (s *Store) Restore(r io.ReadCloser) error

Restore implements raft.FSM.

func (*Store) ServiceLeader

func (s *Store) ServiceLeader(service string) (*discoverd.Instance, error)

func (*Store) ServiceMeta

func (s *Store) ServiceMeta(service string) *discoverd.ServiceMeta

ServiceMeta returns the meta data for a service.

func (*Store) ServiceNames

func (s *Store) ServiceNames() []string

ServiceNames returns a sorted list of existing service names.

func (*Store) SetPeers

func (s *Store) SetPeers(peers []string) error

SetPeers sets a list of peers in the raft cluster. Panic if store is not open yet.

func (*Store) SetServiceLeader

func (s *Store) SetServiceLeader(service, id string) error

SetServiceLeader manually sets the leader for a service.

func (*Store) SetServiceMeta

func (s *Store) SetServiceMeta(service string, meta *discoverd.ServiceMeta) error

func (*Store) Snapshot

func (s *Store) Snapshot() (raft.FSMSnapshot, error)

Snapshot implements raft.FSM.

func (*Store) Subscribe

func (s *Store) Subscribe(service string, sendCurrent bool, kinds discoverd.EventKind, ch chan *discoverd.Event) stream.Stream

Subscribe creates a subscription to events on a given service.

func (*Store) TriggerSnapshot

func (s *Store) TriggerSnapshot() error

Jump to

Keyboard shortcuts

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