Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var ( // ErrNonLeader - the instance is not the leader ErrNonLeader = errors.New("the elector is not leader") // ErrClosed - the elector is already closed ErrClosed = errors.New("the elector is already closed") // ErrPingRedis - ping redis server timeout ErrPingRedis = errors.New("ping redis server timeout") )
Functions ¶
This section is empty.
Types ¶
type Elector ¶
type Elector struct {
// contains filtered or unexported fields
}
Elector is a distributed leader election implementation using redis.
func NewElector ¶
NewElector creates a new Elector instance with the given redis config.
Example ¶
cfg := Config{
InitAddress: []string{"127.0.0.1:6379"},
Username: "",
Password: "woIuu1208",
SelectDB: 1,
}
_, err := NewElector(context.Background(), cfg)
if err != nil {
panic(err)
}
func NewElectorWithClient ¶
NewElectorWithClient creates a new Elector instance with the given etcd client.
func (*Elector) GetLeaderID ¶
GetLeaderID returns the current leader ID.
func (*Elector) Start ¶
Start Start the election. This method will keep trying the election. When the election is successful, set isleader to true. If it fails, the election directory will be monitored until the election is successful. The parameter electionPath is used to specify the etcd directory for the operation.
type Option ¶
type Option func(*Elector)
func WithLogger ¶
func WithLogger(logger *zap.SugaredLogger) Option
WithLogger sets the logger for the elector.
Click to show internal directories.
Click to hide internal directories.