candidate

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: May 17, 2019 License: MPL-2.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const (
	IDRegex = `^[a-zA-Z0-9:.-]+$` // only allow certain characters in an ID

	SessionKeyPrefix = "candidate-"
)

Variables

View Source
var (
	InvalidCandidateID = fmt.Errorf("candidate ID must obey \"%s\"", IDRegex)
)

Functions

This section is empty.

Types

type Candidate

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

func New

func New(conf *Config) (*Candidate, error)

func (*Candidate) Elected

func (c *Candidate) Elected() bool

Elected will return true if this candidate's session is "locking" the kv

func (*Candidate) ForeignLeaderIP

func (c *Candidate) ForeignLeaderIP(dc string) (net.IP, error)

ForeignLeaderIP will attempt to parse the body of the locked kv key to locate the current leader

func (*Candidate) ForeignLeaderService

func (c *Candidate) ForeignLeaderService(dc string) (*api.SessionEntry, error)

ForeignLeaderService will attempt to locate the leader's session entry in a datacenter of your choosing

func (*Candidate) ID

func (c *Candidate) ID() string

ID returns the unique identifier given at construct

func (*Candidate) LeaderIP

func (c *Candidate) LeaderIP() (net.IP, error)

Return the leader, assuming its ID can be interpreted as an IP address

func (*Candidate) LeaderService

func (c *Candidate) LeaderService() (*api.SessionEntry, error)

LeaderService will attempt to locate the leader's session entry in your local agent's datacenter

func (*Candidate) RemoveWatchers

func (c *Candidate) RemoveWatchers()

RemoveWatchers will clear all watchers

func (*Candidate) Resign

func (c *Candidate) Resign()

Resign will remove this candidate from the election pool

func (*Candidate) Run

func (c *Candidate) Run() error

Run will enter this candidate into the election pool

func (*Candidate) Running

func (c *Candidate) Running() bool

func (*Candidate) SessionID

func (c *Candidate) SessionID() string

SessionID is the name of this candidate's session

func (*Candidate) SessionTTL

func (c *Candidate) SessionTTL() time.Duration

SessionTTL returns the parsed TTL

func (*Candidate) State

func (c *Candidate) State() State

func (*Candidate) Unwatch

func (c *Candidate) Unwatch(id string)

Unwatch will remove a function from the list of watchers.

func (*Candidate) UpdateWatchers

func (c *Candidate) UpdateWatchers()

UpdateWatchers will immediately push the current state of this Candidate to all currently registered Watchers

func (*Candidate) Wait

func (c *Candidate) Wait() error

Wait will block until a leader has been elected, regardless of candidate.

func (*Candidate) WaitFor

func (c *Candidate) WaitFor(td time.Duration) error

WaitFor will wait for a candidate to be elected or until duration has passed

func (*Candidate) WaitUntil

func (c *Candidate) WaitUntil(t time.Time) error

WaitUntil will for a candidate to be elected or until the deadline is breached

func (*Candidate) Watch

func (c *Candidate) Watch(id string, fn WatchFunc) string

Watch allows you to register a function that will be called when the election State has changed

type Config

type Config struct {
	// KVKey [required]
	//
	// Must be the key to attempt to acquire a session lock on.  This key must be considered ephemeral, and not contain
	// anything you don't want overwritten / destroyed.
	KVKey string

	// ID [suggested]
	//
	// Should be a unique identifier that makes sense within the scope of your implementation.
	// If left blank it will attempt to use the local IP address, otherwise a random string will be generated.
	ID string

	// SessionTTL [optional]
	//
	// The duration of time a given candidate can be elected without re-trying.  A "good" value for this depends
	// entirely upon your implementation.  Keep in mind that once the KVKey lock is acquired with the session, it will
	// remain locked until either the specified session TTL is up or Resign() is explicitly called.
	//
	// If not defined, will default to value of session.DefaultTTL
	SessionTTL string

	// Client [optional]
	//
	// Consul API client.  If not specified, one will be created using api.DefaultConfig()
	Client *api.Client

	// AutoRun [optional]
	//
	// If set to true, the Candidate will immediately enter its election pool after successful construction
	AutoRun bool
}

type ElectionUpdate

type ElectionUpdate struct {
	// Elected tracks whether this specific candidate has been elected
	Elected bool
	// State tracks the current state of this candidate
	State State
}

ElectionUpdate is sent to watchers on election state change

type LeaderKVValue

type LeaderKVValue struct {
	LeaderAddress string
}

LeaderKVValue is the body of the acquired KV

type SessionNameParts

type SessionNameParts struct {
	session.NameParts
	CandidateID string
}

func ParseSessionName

func ParseSessionName(name string) (*SessionNameParts, error)

type State

type State uint8
const (
	StateResigned State = iota
	StateRunning
)

type WatchFunc

type WatchFunc func(update ElectionUpdate)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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