Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GenerateNodeID ¶
func GenerateNodeID() string
GenerateNodeID creates a unique node ID from hostname + random bytes.
Types ¶
type Elector ¶
type Elector struct {
// OnElected is called when this node becomes the leader.
OnElected func()
// OnDemoted is called when this node loses leadership.
OnDemoted func()
// contains filtered or unexported fields
}
Elector implements leader election using Redis SET NX EX + Lua refresh.
Algorithm:
- Attempt SET leader {nodeID} NX EX {ttl} — atomic, fails if key exists.
- If elected: refresh the key every TTL/3 using a Lua script.
- If not elected: retry step 1 at TTL/3 intervals.
- On graceful shutdown: delete the key for fast failover.
func NewElector ¶
func NewElector(cfg ElectorConfig) *Elector
NewElector creates a new Redis-based elector.
func (*Elector) Epoch ¶ added in v0.1.3
Epoch returns the current leader epoch (monotonically increasing fencing token).
func (*Elector) IsLeader ¶
IsLeader returns nil if this node is the leader, or ErrNotLeader otherwise.
type ElectorConfig ¶
type ElectorConfig struct {
RDB rueidis.Client
Prefix string
NodeID string
TTL time.Duration // Default: 10s
Logger gochainedlog.Logger
}
ElectorConfig holds configuration for the elector.
Click to show internal directories.
Click to hide internal directories.