election

package
v0.0.0-...-8ba21cc Latest Latest
Warning

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

Go to latest
Published: Dec 18, 2024 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Index

Constants

View Source
const (

	// IsLeader means current compaigner become leader.
	IsLeader = "isLeader"
	// RetireFromLeader means current compaigner is old leader, and retired.
	RetireFromLeader = "retireFromLeader"
	// IsNotLeader means current compaigner is not old leader and current leader.
	IsNotLeader = "isNotLeader"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type CampaignerInfo

type CampaignerInfo struct {
	ID string `json:"id"`
	// addr is the campaigner's advertise address
	Addr string `json:"addr"`
}

CampaignerInfo is the campaigner's information.

func (*CampaignerInfo) String

func (c *CampaignerInfo) String() string

type Election

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

Election implements the leader election based on etcd.

func NewElection

func NewElection(ctx context.Context, cli *clientv3.Client, sessionTTL int, key, id, addr string, notifyBlockTime time.Duration) (*Election, error)

NewElection creates a new etcd leader Election instance and starts the campaign loop.

func (*Election) CancelEvictLeader

func (e *Election) CancelEvictLeader()

CancelEvictLeader set evictLeader to false, and this member can campaign leader again.

func (*Election) ClearSessionIfNeeded

func (e *Election) ClearSessionIfNeeded(ctx context.Context, id string) (bool, error)

ClearSessionIfNeeded will clear session when deleted master quited abnormally returns (triggered deleting session, error).

func (*Election) Close

func (e *Election) Close()

Close closes the election instance and release the resources.

func (*Election) ErrorNotify

func (e *Election) ErrorNotify() <-chan error

ErrorNotify returns a channel that can fetch errors occurred for campaign.

func (*Election) EvictLeader

func (e *Election) EvictLeader()

EvictLeader set evictLeader to true, and this member can't be leader.

func (*Election) ID

func (e *Election) ID() string

ID returns the current member's ID.

func (*Election) IsLeader

func (e *Election) IsLeader() bool

IsLeader returns whether this member is the leader.

func (*Election) LeaderInfo

func (e *Election) LeaderInfo(ctx context.Context) (string, string, string, error)

LeaderInfo returns the current leader's key, ID and address. it's similar with https://github.com/etcd-io/etcd/blob/v3.4.3/clientv3/concurrency/election.go#L147.

func (*Election) LeaderNotify

func (e *Election) LeaderNotify() <-chan *CampaignerInfo

LeaderNotify returns a channel that can fetch the leader's information when the member become the leader or retire from the leader, or get a new leader. leader's information can be nil which means this member is leader and retire.

func (*Election) Resign

func (e *Election) Resign()

Resign resign the leader.

Jump to

Keyboard shortcuts

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