elector

package module
v0.0.0-...-720b127 Latest Latest
Warning

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

Go to latest
Published: Feb 5, 2024 License: MIT Imports: 11 Imported by: 0

README

gocron-etcd-elector

install

go get github.com/go-co-op/gocron-etcd-elector

usage

Here is an example usage that would be deployed in multiple instances.

package main

import (
	"context"
	"fmt"
	"os"
	"os/signal"
	"syscall"
	"time"

	"github.com/go-co-op/gocron"
	elector "github.com/go-co-op/gocron-etcd-elector"
)

func main() {
	cfg := elector.Config{
		Endpoints:   []string{"http://127.0.0.1:2379"},
		DialTimeout: 3 * time.Second,
	}

	el, err := elector.NewElector(context.Background(), cfg, elector.WithTTL(10))
	if err != nil {
		panic(err)
	}

	go func() {
		for {
			err := el.Start("/gocron/elector")
			if err == elector.ErrClosed {
				return
			}

			time.Sleep(1e9)
		}
	}()

	s := gocron.NewScheduler(time.UTC)
	s.WithDistributedElector(el)

	_, _ = s.Every("1s").Do(func() {
		if el.IsLeader(context.Background()) == nil {
			fmt.Println("the current instance is leader")
		} else {
			fmt.Println("the current leader is", el.GetLeaderID())
		}

		fmt.Println("call 1s")
	})

	s.StartAsync()

	c := make(chan os.Signal, 1)
	signal.Notify(c, syscall.SIGINT, syscall.SIGQUIT, syscall.SIGTERM)
	<-c

	fmt.Println("exit")
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNonLeader = errors.New("the elector is not leader")
	ErrClosed    = errors.New("the elector is already closed")
	ErrPingEtcd  = errors.New("ping etcd server timeout")
)
View Source
var (
	// alias options
	WithTTL     = concurrency.WithTTL
	WithContext = concurrency.WithContext
	WithLease   = concurrency.WithLease
)

Functions

This section is empty.

Types

type Config

type Config = clientv3.Config

alias clientv3.config

type Elector

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

func NewElector

func NewElector(ctx context.Context, cfg clientv3.Config, options ...concurrency.SessionOption) (*Elector, error)

func NewElectorWithClient

func NewElectorWithClient(ctx context.Context, cli *clientv3.Client, options ...concurrency.SessionOption) (*Elector, error)

func (*Elector) GetID

func (e *Elector) GetID() string

func (*Elector) GetLeaderID

func (e *Elector) GetLeaderID() string

func (*Elector) IsLeader

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

func (*Elector) SetLogger

func (e *Elector) SetLogger(fn func(msg ...interface{}))

func (*Elector) Start

func (e *Elector) Start(electionPath string) 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

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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