Skip to content

Latest commit

 

History

History
28 lines (21 loc) · 1.33 KB

README.md

File metadata and controls

28 lines (21 loc) · 1.33 KB

consul-leader-election

Build Status Coverage Status

An implementation of Consul leader election based on https://www.consul.io/docs/guides/leader-election.html

Usage

import (
	"github.com/dpires/consul-leader-election"
	"github.com/dpires/consul-leader-election/client"
	"github.com/hashicorp/consul/api"
)

config := api.DefaultConfig()                                  // Create a new api client config
consulclient, _ := api.NewClient(config)                       // Create a Consul api client

leaderElection := &election.LeaderElection{
        StopElection:  make(chan bool),                        // The channel for stopping the election
        LeaderKey:     "service/my-leadership-service/leader", // The leadership key to create/aquire
        WatchWaitTime: 10,                                     // Time in seconds to check for leadership
        Client: &client.ConsulClient{Client:consulclient},     // The injected Consul api client
}

go leaderElection.ElectLeader()                                // Run the election