-
Notifications
You must be signed in to change notification settings - Fork 444
/
identity.go
39 lines (31 loc) · 944 Bytes
/
identity.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
package leaderelector
var _ Identity = new(identityImpl)
// Identity contains leader election information about the current component
type Identity interface {
// IsLeader returns true if the current component is the leader, false otherwise
IsLeader() bool
// Elected returns the channel that will be signaled when the current component is elected the leader
Elected() <-chan struct{}
}
type identityImpl struct {
elected <-chan struct{}
}
func NewIdentity(elected <-chan struct{}) *identityImpl {
return &identityImpl{
elected: elected,
}
}
func (i identityImpl) IsLeader() bool {
channelOpen := true
select {
case _, channelOpen = <-i.Elected():
default:
// https://go.dev/tour/concurrency/6
// Ensure that receiving from elected channel does not block
}
// Leadership is designated by the closing of the election channel
return !channelOpen
}
func (i identityImpl) Elected() <-chan struct{} {
return i.elected
}