election

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Mar 11, 2024 License: MIT Imports: 6 Imported by: 0

README

Consul leader election Build Status codecov GoDoc

This package provides leader election through consul

https://www.consul.io/docs/guides/leader-election.html

How to use

package main

   import(
       "github.com/hashicorp/consul/api"
       "github.com/dmitriyGarden/consul-leader-election"
       "fmt"
   )
   type notify struct {
      T  string
   }
   func (n *notify)EventLeader(f bool)  {
       if f {
           fmt.Println(n.T, "I'm the leader!")
       } else {
           fmt.Println(n.T, "I'm no longer the leader!")
       }
   }
   func main(){
       conf := api.DefaultConfig()
   	consul, _ := api.NewClient(conf)
       n := &notify{
       	T: "My service",
       }

   	elconf := &ElectionConfig{
                 	CheckTimeout: 5 * time.Second,
                 	Client: consul,
                 	Checks: []string{"healthID"},
                 	Key: "service/test-election/leader",
                 	LogLevel: election.LogDebug
                 	Event: n,
                }
   	e := election.NewElection(elconf)
   	// start election
   	go  e.Init()
   	if e.IsLeader() {
           fmt.Println("I'm a leader!")
       }
   	// to do something ....
   	if e.IsLeader() {
   		fmt.Println("I'm a leader!")
   	}
   	// re-election
   	e.ReElection()
   	// to do something ....
   	if e.IsLeader() {
   		fmt.Println("I'm a leader!")
   	}
   	e.Stop()
   }

Documentation

Index

Constants

View Source
const (
	LogDisable = iota
	LogError
	LogInfo
	LogDebug
)

Log levels

Variables

This section is empty.

Functions

This section is empty.

Types

type Election

type Election struct {
	Client *api.Client // Consul client
	Checks []string    // Slice of associated health checks

	Kv string // Key in Consul kv

	CheckTimeout     time.Duration
	SessionLockDelay time.Duration
	LogPrefix        string // Prefix for a log

	Event Notifier
	sync.RWMutex
	// contains filtered or unexported fields
}

Election implements to detect a leader in a cluster of services

func NewElection

func NewElection(c *ElectionConfig) *Election

NewElection create new elector

func (*Election) Init

func (e *Election) Init()

Init starting election process

func (*Election) InitContext

func (e *Election) InitContext(ctx context.Context)

InitContext starting election process with context

func (*Election) IsLeader

func (e *Election) IsLeader() bool

IsLeader check a leader

func (*Election) ReElection

func (e *Election) ReElection() error

ReElection start re-election

func (*Election) SetLogLevel

func (e *Election) SetLogLevel(level uint8)

SetLogLevel is setting level according constants LogDisable|LogError|LogInfo|LogDebug

func (*Election) Stop

func (e *Election) Stop()

Stop election process

type ElectionConfig

type ElectionConfig struct {
	Client           *api.Client // Consul client
	Checks           []string    // Slice of associated health checks
	Key              string      // Key in Consul KV
	LogLevel         uint8       // Log level LogDisable|LogError|LogInfo|LogDebug
	LogPrefix        string      // Prefix for a log
	Event            Notifier
	CheckTimeout     time.Duration
	SessionLockDelay time.Duration
}

ElectionConfig config for Election

type Notifier

type Notifier interface {
	EventLeader(e bool) // The method will be called when the leader status is changed
}

Notifier can tell your code the event of the leader's status change

Jump to

Keyboard shortcuts

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