Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func NewClient ¶
NewClient creates a new etcd.Client with the specified config where blanks are filled from environment variables by NewConfig.
If the provided config is nil and no environment variables are set, it will return a client connecting without TLS via localhost:2379.
func NewConfig ¶
NewConfig creates a new etcd.Config using environment variables. If an existing config is passed, it will fill in missing configuration using environment variables or defaults if they exists on the local system.
If no environment variables are set, it will return a config set to connect without TLS via localhost:2379.
Types ¶
type Election ¶
type Election struct {
// contains filtered or unexported fields
}
func (*Election) Concede ¶
Release leadership and return true if we own it, else do nothing and return false
func (*Election) LeaderChan ¶
type ElectionConfig ¶
type ElectionConfig struct {
// The name of the election (IE: scout, blackbird, etc...)
Election string
// The name of this instance (IE: worker-n01, worker-n02, etc...)
Candidate string
// Seconds to wait before giving up the election if leader disconnected
TTL int
// Report not leader when etcd connection is interrupted
LoseLeaderOnDisconnect bool
// If we were leader before connection or service interruption attempt
// to resume leadership without initiating a new election
ResumeLeaderOnReconnect bool
// How long to wait until attempting to establish a new session between failures
ReconnectBackOff time.Duration
// The size of the leader channel buffer as returned by LeaderChan(). Set this to
// something other than zero to avoid losing leadership changes.
LeaderChannelSize int
}
type LeaderElectionMock ¶
type LeaderElectionMock struct{}
func (*LeaderElectionMock) Concede ¶
func (s *LeaderElectionMock) Concede() bool
func (*LeaderElectionMock) IsLeader ¶
func (s *LeaderElectionMock) IsLeader() bool
func (*LeaderElectionMock) LeaderChan ¶
func (s *LeaderElectionMock) LeaderChan() chan bool
func (*LeaderElectionMock) Start ¶
func (s *LeaderElectionMock) Start() error
func (*LeaderElectionMock) Stop ¶
func (s *LeaderElectionMock) Stop()
type LeaderElector ¶
type LeaderElector interface {
IsLeader() bool
LeaderChan() chan bool
Concede() bool
Start() error
Stop()
}
func NewElection ¶
func NewElection(client *etcd.Client, conf ElectionConfig) LeaderElector
Use leader election if you have several instances of a service running in production and you only want one of the service instances to preform a periodic task.
client, _ := etcdutil.NewClient(nil)
election := etcdutil.NewElection(client, etcdutil.ElectionConfig{
Election: "election-name",
Candidate: "",
TTL: 5,
})
// Start the leader election and attempt to become leader
if err := election.Start(); err != nil {
panic(err)
}
// Returns true if we are leader (thread safe)
if election.IsLeader() {
// Do periodic thing
}
select {
case isLeader := <-election.LeaderChan():
fmt.Printf("Leader: %t\n", isLeader)
}
NOTE: If this instance is elected leader and connection is interrupted to etcd, this library will continue to report it is leader until connection to etcd is resumed and a new leader is elected. If you wish to lose leadership on disconnect set `LoseLeaderOnDisconnect = true`