-
Notifications
You must be signed in to change notification settings - Fork 432
/
factory.go
32 lines (25 loc) · 1.14 KB
/
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
package singlereplica
import (
"context"
"github.com/solo-io/gloo/pkg/bootstrap/leaderelector"
"github.com/solo-io/go-utils/contextutils"
)
var _ leaderelector.ElectionFactory = new(singleReplicaElectionFactory)
// singleReplicaElectionFactory runs leader election for components that do not support true leader election
// The election is a no-op and returns an Identity that is always considered the "leader" since there is only one
type singleReplicaElectionFactory struct {
}
func NewElectionFactory() *singleReplicaElectionFactory {
return &singleReplicaElectionFactory{}
}
func (f *singleReplicaElectionFactory) StartElection(ctx context.Context, _ *leaderelector.ElectionConfig) (leaderelector.Identity, error) {
contextutils.LoggerFrom(ctx).Debugf("Starting Single Replica Leader Election")
return Identity(), nil
}
// Identity returns the Identity used in single replica elections
// Since there is only 1 replica, the identity is always considered the "leader"
func Identity() leaderelector.Identity {
elected := make(chan struct{})
close(elected) // immediately signal the identity as the leader
return leaderelector.NewIdentity(elected)
}