Skip to content

Commit

Permalink
add retry to leader election process
Browse files Browse the repository at this point in the history
  • Loading branch information
samos123 committed Feb 3, 2024
1 parent fd8725a commit e365dd7
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion pkg/leader/election.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/leaderelection"
"k8s.io/client-go/tools/leaderelection/resourcelock"
"k8s.io/client-go/util/flowcontrol"
)

func NewElection(clientset kubernetes.Interface, id, namespace string) *Election {
Expand Down Expand Up @@ -63,5 +64,27 @@ type Election struct {
}

func (le *Election) Start(ctx context.Context) {
leaderelection.RunOrDie(ctx, le.config)
backoff := flowcontrol.NewBackOff(1*time.Second, 15*time.Second)
const backoffID = "lingo-leader-election"
retryCount := 0
for {
select {
case <-ctx.Done():
return
default:
if retryCount > 0 {
backoff.Next(backoffID, backoff.Clock.Now())
delay := backoff.Get(backoffID)
log.Printf("Leader election failed, retrying in %v. RetryCount: %v", delay, retryCount+1)
select {
case <-time.After(delay):
case <-ctx.Done():
return
}
}
log.Printf("Starting leader election process. RetryCount: %v", retryCount+1)
leaderelection.RunOrDie(ctx, le.config)
retryCount++
}
}
}

0 comments on commit e365dd7

Please sign in to comment.