-
Notifications
You must be signed in to change notification settings - Fork 444
/
election_factory.go
50 lines (43 loc) · 1.6 KB
/
election_factory.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package leaderelector
import (
"context"
"os"
"strconv"
)
// Leader Election is a valuable feature of Gloo Edge that is enabled by default
// If you wish to disable it, set this env variable to a truthy value ("1", "t", "T", "true", "TRUE", "True")
const disableElectionEnvVar = "DISABLE_LEADER_ELECTION"
var (
disableElection = false
)
func init() {
disableElectionVal := os.Getenv(disableElectionEnvVar)
boolValue, err := strconv.ParseBool(disableElectionVal)
// in the case where a non-truthy string was provided, this will return an error
// in that case, we ignore the value altogether
if err == nil {
disableElection = boolValue
}
}
// ElectionConfig is the set of properties that can be used to configure leader elections
type ElectionConfig struct {
// The name of the component
Id string
// The namespace where the component is running
Namespace string
// Callback function that is executed when the current component becomes leader
OnStartedLeading func(c context.Context)
// Callback function that is executed when the current component stops leading
OnStoppedLeading func()
// Callback function that is executed when a new leader is elected
OnNewLeader func(leaderId string)
}
// An ElectionFactory is an implementation for running a leader election
type ElectionFactory interface {
// StartElection begins leader election and returns the Identity of the current component
StartElection(ctx context.Context, config *ElectionConfig) (Identity, error)
}
// IsDisabled returns true if leader election is disabled using an environment variable
func IsDisabled() bool {
return disableElection
}