func (le *LeaderElector) acquire() {
	stop := make(chan struct{})
	wait.JitterUntil(func() {
		succeeded := le.tryAcquireOrRenew()
		le.maybeReportTransition()
		desc := le.config.Lock.Describe()
		if !succeeded {
			glog.V(4).Infof("failed to renew lease %v", desc)
			return
		}
		le.config.Lock.RecordEvent("became leader")
		glog.Infof("successfully acquired lease %v", desc)
		close(stop)
	}, le.config.RetryPeriod, JitterFactor, true, stop)
}
acquire loops calling tryAcquireOrRenew and returns immediately when tryAcquireOrRenew succeeds.
package election
import (
	"fmt"
	"reflect"
	"time"
	"k8s.io/apimachinery/pkg/api/errors"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/apimachinery/pkg/util/runtime"
	"k8s.io/apimachinery/pkg/util/wait"
	rl "github.com/deepinsight/mxnet-operator/pkg/util/k8sutil/election/resourcelock"
	"github.com/golang/glog"
)
const (
	JitterFactor         = 1.2
	DefaultLeaseDuration = 15 * time.Second
	DefaultRenewDeadline = 10 * time.Second
	DefaultRetryPeriod   = 2 * time.Second
)
func NewLeaderElector(lec LeaderElectionConfig) (*LeaderElector, error) {
	if lec.LeaseDuration <= lec.RenewDeadline {
		return nil, fmt.Errorf("leaseDuration must be greater than renewDeadline")
	}
	if lec.RenewDeadline <= time.Duration(JitterFactor*float64(lec.RetryPeriod)) {
		return nil, fmt.Errorf("renewDeadline must be greater than retryPeriod*JitterFactor")
	}
	if lec.Lock == nil {
		return nil, fmt.Errorf("Lock must not be nil.")
	}
	return &LeaderElector{
		config: lec,
	}, nil
}
type LeaderElectionConfig struct {
	Lock rl.Interface
	LeaseDuration time.Duration
	RenewDeadline time.Duration
	RetryPeriod time.Duration
	Callbacks LeaderCallbacks
}
type LeaderCallbacks struct {
	OnStartedLeading func(stop <-chan struct{})
	OnStoppedLeading func()
	OnNewLeader func(identity string)
}
type LeaderElector struct {
	config LeaderElectionConfig
	observedRecord rl.LeaderElectionRecord
	observedTime   time.Time
	reportedLeader string
}
func (le *LeaderElector) Run() {
	defer func() {
		runtime.HandleCrash()
		le.config.Callbacks.OnStoppedLeading()
	}()
	le.acquire()
	stop := make(chan struct{})
	go le.config.Callbacks.OnStartedLeading(stop)
	le.renew()
	close(stop)
}
func RunOrDie(lec LeaderElectionConfig) {
	le, err := NewLeaderElector(lec)
	if err != nil {
		panic(err)
	}
	le.Run()
}
func (le *LeaderElector) GetLeader() string {
	return le.observedRecord.HolderIdentity
}
func (le *LeaderElector) IsLeader() bool {
	return le.observedRecord.HolderIdentity == le.config.Lock.Identity()
}