Created
October 25, 2021 06:39
-
-
Save mayankshah1607/b72b876026a84e62aa37043e7e91e9ed to your computer and use it in GitHub Desktop.
runLeaderElection
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func runLeaderElection(lock *resourcelock.LeaseLock, ctx context.Context, id string) { | |
leaderelection.RunOrDie(ctx, leaderelection.LeaderElectionConfig{ | |
Lock: lock, | |
ReleaseOnCancel: true, | |
LeaseDuration: 15 * time.Second, | |
RenewDeadline: 10 * time.Second, | |
RetryPeriod: 2 * time.Second, | |
Callbacks: leaderelection.LeaderCallbacks{ | |
OnStartedLeading: func(c context.Context) { | |
doStuff() | |
}, | |
OnStoppedLeading: func() { | |
klog.Info("no longer the leader, staying inactive.") | |
}, | |
OnNewLeader: func(current_id string) { | |
if current_id == id { | |
klog.Info("still the leader!") | |
return | |
} | |
klog.Info("new leader is %s", current_id) | |
}, | |
}, | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment