elector

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Jan 10, 2026 License: MIT Imports: 10 Imported by: 0

README

gocron-redis-elector

install

go get github.com/qindj/gocron-redis-elector

FORKED from https://github.com/go-co-op/gocron-etcd-elector

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 Config

type Config = redis.ClientOption

Config is an alias redis.ClientOption

type Elector

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

Elector is a distributed leader election implementation using redis.

func NewElector

func NewElector(ctx context.Context, cfg redis.ClientOption, opts ...Option) (*Elector, error)

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

func NewElectorWithClient(ctx context.Context, cli redis.Client, opts ...Option) (*Elector, error)

NewElectorWithClient creates a new Elector instance with the given etcd client.

func (*Elector) GetID

func (e *Elector) GetID() string

GetID returns the elector ID.

func (*Elector) GetLeaderID

func (e *Elector) GetLeaderID() string

GetLeaderID returns the current leader ID.

func (*Elector) IsLeader

func (e *Elector) IsLeader(_ context.Context) error

IsLeader checks if the current instance is the leader.

func (*Elector) Start

func (e *Elector) Start() error

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.

func (*Elector) Stop

func (e *Elector) Stop() error

Stop stops the elector and closes the etcd client.

type Option

type Option func(*Elector)

func WithKey

func WithKey(key string) Option

WithKey sets the key for the elector.

func WithLogger

func WithLogger(logger *zap.SugaredLogger) Option

WithLogger sets the logger for the elector.

func WithTTL

func WithTTL(ttl int) Option

WithTTL sets the TTL for the elector.

Jump to

Keyboard shortcuts

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