clusterleader

package module
v0.0.0-...-26349b6 Latest Latest
Warning

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

Go to latest
Published: Oct 22, 2023 License: MIT Imports: 7 Imported by: 1

README

Cluster leader election using Consul distributed locks

package main

import (
	"log"
	"time"

	"github.com/hashicorp/consul/api"
	"github.com/pteich/clusterleader"
)

func main() {

	config := api.DefaultConfig()
	config.Address = "127.0.0.1:8500"
	client, err := api.NewClient(config)
 
	clusterLeader, err := clusterleader.NewClusterleader(client, "testkey", "localhost", 15*time.Second)
	if err != nil {
		log.Fatal(err)
	}

	go func() {
		for err := range clusterLeader.Errors() {
			log.Print(err)
		}
	}()

	// as long as this loop runs we try to become leader
	// an isElected event is send on every state change
	// best would be to run this loop in a go routine and
	// react to whatever we are leader or not
	for isElected := range clusterLeader.Election() {
		if isElected {
			// we are leader
		} else {
			// not leader
		}
	}

}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Clusterleader

type Clusterleader struct {
	sync.Mutex
	// contains filtered or unexported fields
}

Clusterleader uses Consul distributed locks to elect a cluster leader

func New

func New(consulClient *api.Client, key string, node string, waitTime time.Duration) (*Clusterleader, error)

New returns a new Clusterleader instance with a given consul connection

func NewWithDefaultClient

func NewWithDefaultClient(key string, node string, waitTime time.Duration) (*Clusterleader, error)

NewDefaultClient uses a default Consul client (which can also be set using Consul env variables)

func (*Clusterleader) Election

func (cl *Clusterleader) Election(ctx context.Context) <-chan bool

Election returns a channel that signals if we are leader or not

func (*Clusterleader) Errors

func (cl *Clusterleader) Errors() <-chan error

Errors returns a channel that receives all errors

func (*Clusterleader) IsLeader

func (cl *Clusterleader) IsLeader() bool

IsLeader just returns if we are leader or not

type DistributedLock

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

DistributedLock describes a lock

func NewDistributedLock

func NewDistributedLock(consulClient *api.Client, prefix string, sessionTTL time.Duration) (*DistributedLock, error)

NewDistributedLock returns an instance with a given Consul client

func NewDistributedLockWithDefaultClient

func NewDistributedLockWithDefaultClient(prefix string, sessionTTL time.Duration) (*DistributedLock, error)

NewDistributedLockWithDefaultClient uses a Consul client with default settings (or values from ENV)

func (*DistributedLock) Lock

func (dl *DistributedLock) Lock(key string, value string, lockWaitTime time.Duration) (<-chan struct{}, error)

Lock tries once to acquire the lock or return an error if it could not get it

func (*DistributedLock) Unlock

func (dl *DistributedLock) Unlock(key string) error

Unlock releases a specific lock

func (*DistributedLock) UnlockAll

func (dl *DistributedLock) UnlockAll()

UnlockAll releases all held locks

Jump to

Keyboard shortcuts

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