Documentation
¶
Overview ¶
Package elector elects one leader per cluster with a lease row whose times come from the client clock. The leader runs the maintenance services; it renews every TTL/3 and stops its services as soon as a renewal fails.
e, err := elector.New(elector.Config{ClientID: id, Store: store, Clock: clk, Services: runMaintenance})
go e.Run(ctx)
Index ¶
Constants ¶
const ( DefaultName = "default" DefaultTTL = 15 * time.Second DefaultReleaseTimeout = 5 * time.Second )
Defaults.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct {
Name string
ClientID string
// TTL is the lease duration; the lease is renewed every TTL/3.
TTL time.Duration
Store job.LeaseStore
Clock clock.Clock
// Services runs the leader-only services until its context is cancelled.
Services func(ctx context.Context)
// OnChange is called with true when leadership is gained and false when lost.
OnChange func(leader bool)
Logger *slog.Logger
ReleaseTimeout time.Duration
}
Config configures an Elector.
type Elector ¶
type Elector struct {
// contains filtered or unexported fields
}
Elector holds a lease row to elect one leader per cluster. Clients that are not leader try to acquire the lease every TTL/3 (and on Wake); the leader renews it every TTL/3 and stops its services immediately when a renewal fails. On shutdown the lease is released so another client can take over at once.
func (*Elector) Crash ¶
func (e *Elector) Crash()
Crash simulates a leader crash for tests (crash seam): it stops the leader services immediately and abandons the lease without resigning, so other clients take over only after it expires. Run returns. Crash waits until Run has stopped; it is a no-op if Run was never started.