Documentation
¶
Index ¶
- func NewNoopLeaderManager() *noopLeaderManager
- type Config
- type DlockLeaderManager
- func (l *DlockLeaderManager) Await(ctx context.Context) bool
- func (l *DlockLeaderManager) Campaign(ctx context.Context)
- func (l *DlockLeaderManager) Extend(ctx context.Context) error
- func (l *DlockLeaderManager) Identity() string
- func (l *DlockLeaderManager) OnElected(cb ElectedCallback)
- func (l *DlockLeaderManager) OnError(cb ErrorCallback)
- func (l *DlockLeaderManager) OnOusted(cb OustedCallback)
- func (l *DlockLeaderManager) Stop() error
- type ElectedCallback
- type ErrorCallback
- type LeaderManager
- type LeaderStatus
- type OustedCallback
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func NewNoopLeaderManager ¶
func NewNoopLeaderManager() *noopLeaderManager
Types ¶
type Config ¶
type Config struct {
// Leader election enabled or not?
Enabled bool
// Unique identifier for the leader
ID string `default:"leader"`
// Duration of the leader term
Lease time.Duration `default:"1m"`
// The time interval between retries of becoming the leader
Retry time.Duration `default:"5s"`
// The time interval trying to renew the leader term
Renew time.Duration `default:"15s"`
}
Config holds the configuration for the leader election.
type DlockLeaderManager ¶
type DlockLeaderManager struct {
Config // election configuration
// contains filtered or unexported fields
}
DlockLeaderManager manages the leader election process using distributed lock.
func NewDlockLeaderManager ¶
func NewDlockLeaderManager(dlm *dlock.LockManager, conf Config, elecKey string) *DlockLeaderManager
NewDlockLeaderManager creates a new `LeaderManager` with the provided configuration and election key.
func (*DlockLeaderManager) Await ¶
func (l *DlockLeaderManager) Await(ctx context.Context) bool
Await blocks until being elected as leader or context canceled.
func (*DlockLeaderManager) Campaign ¶
func (l *DlockLeaderManager) Campaign(ctx context.Context)
Campaign starts the election process, which will run in a goroutine until context canceled.
func (*DlockLeaderManager) Extend ¶
func (l *DlockLeaderManager) Extend(ctx context.Context) error
Extend extends leadership lease.
func (*DlockLeaderManager) Identity ¶
func (l *DlockLeaderManager) Identity() string
func (*DlockLeaderManager) OnElected ¶
func (l *DlockLeaderManager) OnElected(cb ElectedCallback)
OnElected registers a callback function to be invoked on leader elected.
func (*DlockLeaderManager) OnError ¶
func (l *DlockLeaderManager) OnError(cb ErrorCallback)
OnError registers a callback function to be invoked on election error.
func (*DlockLeaderManager) OnOusted ¶
func (l *DlockLeaderManager) OnOusted(cb OustedCallback)
OnOusted registers a callback function to be invoked on leader ousted.
func (*DlockLeaderManager) Stop ¶
func (l *DlockLeaderManager) Stop() error
Stop stops the leader election process and resigns from the leadership if appliable.
type ElectedCallback ¶
type ElectedCallback func(ctx context.Context, lm LeaderManager)
ElectedCallback is a type alias for the callback function executed upon leader elected.
type ErrorCallback ¶
type ErrorCallback func(ctx context.Context, lm LeaderManager, err error)
ErrorCallback is a type alias for the callback function executed upon election error.
type LeaderManager ¶
type LeaderManager interface {
// Identity returns leader identity
Identity() string
// Wait until being elected as leader or context canceled
Await(ctx context.Context) bool
// Extend extends the leadership lease
Extend(ctx context.Context) error
// Campaign starts the leader election process
Campaign(ctx context.Context)
// Stop stops the leader election process
Stop() error
// OnElected registers a leader elected callback function.
OnElected(cb ElectedCallback)
// OnOusted registers a leader ousted callback function.
OnOusted(cb OustedCallback)
// OnError registers an election error callback function.
OnError(cb ErrorCallback)
}
func MustNewLeaderManagerFromViper ¶
func MustNewLeaderManagerFromViper(dlm *dlock.LockManager, elecKey string) LeaderManager
MustNewLeaderManagerFromViper creates a new LeaderManager with the given LockManager and election key.
type LeaderStatus ¶
type LeaderStatus = int32
const ( StatusInit LeaderStatus = iota StatusElected StatusOusted )
type OustedCallback ¶
type OustedCallback func(ctx context.Context, lm LeaderManager)
OustedCallback is a type alias for the callback function executed upon leader ousted.