github.com/searKing/golang/go@v1.2.117/sync/leaderelection/leader_elector.config.go (about)

     1  // Copyright 2021 The searKing Author. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package leaderelection
     6  
     7  import (
     8  	"context"
     9  	"time"
    10  )
    11  
    12  type Config struct {
    13  	// Lock is the resource that will be used for locking
    14  	Lock ResourceLocker
    15  
    16  	// LeaseDuration is the duration that non-leader candidates will
    17  	// wait to force acquire leadership. This is measured against time of
    18  	// last observed ack.
    19  	//
    20  	// A client needs to wait a full LeaseDuration without observing a change to
    21  	// the record before it can attempt to take over. When all clients are
    22  	// shutdown and a new set of clients are started with different names against
    23  	// the same leader record, they must wait the full LeaseDuration before
    24  	// attempting to acquire the lease. Thus LeaseDuration should be as short as
    25  	// possible (within your tolerance for clock skew rate) to avoid a possible
    26  	// long waits in the scenario.
    27  	//
    28  	// Core clients default this value to 15 seconds.
    29  	LeaseDuration time.Duration
    30  	// RenewTimeout is the duration that the acting master will retry
    31  	// refreshing leadership before giving up.
    32  	//
    33  	// Core clients default this value to 10 seconds.
    34  	RenewTimeout time.Duration
    35  	// RetryPeriod is the duration the LeaderElector clients should wait
    36  	// between tries of actions.
    37  	//
    38  	// Core clients default this value to 2 seconds.
    39  	RetryPeriod time.Duration
    40  
    41  	// Callbacks are callbacks that are triggered during certain lifecycle
    42  	// events of the LeaderElector
    43  	Callbacks LeaderCallbacks
    44  
    45  	// ReleaseOnCancel should be set true if the lock should be released
    46  	// when the run context is cancelled. If you set this to true, you must
    47  	// ensure all code guarded by this lease has successfully completed
    48  	// prior to cancelling the context, or you may have two processes
    49  	// simultaneously acting on the critical path.
    50  	ReleaseOnCancel bool
    51  
    52  	// Name is the name of the resource lock for debugging
    53  	Name string
    54  }
    55  
    56  // LeaderCallbacks are callbacks that are triggered during certain
    57  // lifecycle events of the LeaderElector. These are invoked asynchronously.
    58  //
    59  // possible future callbacks:
    60  //   - OnChallenge()
    61  type LeaderCallbacks struct {
    62  	// OnStartedLeading is called when a LeaderElector client starts leading
    63  	OnStartedLeading func(context.Context)
    64  	// OnStoppedLeading is called when a LeaderElector client stops leading
    65  	OnStoppedLeading func()
    66  	// OnNewLeader is called when the client observes a leader that is
    67  	// not the previously observed leader. This includes the first observed
    68  	// leader when the client starts.
    69  	OnNewLeader func(identity string)
    70  }
    71  
    72  func (c *Config) SetDefaults() {
    73  	if c != nil {
    74  		c.Lock = NewDummyLock("")
    75  		c.LeaseDuration = 15 * time.Second
    76  		c.RenewTimeout = 10 * time.Second
    77  		c.RetryPeriod = 2 * time.Second
    78  	}
    79  }
    80  
    81  func (c *Config) Complete() {
    82  	if c.LeaseDuration < 1 {
    83  		c.LeaseDuration = 15 * time.Second
    84  	}
    85  	if c.RenewTimeout < 1 {
    86  		c.RenewTimeout = 10 * time.Second
    87  	}
    88  	if c.RetryPeriod < 1 {
    89  		c.RetryPeriod = 2 * time.Second
    90  	}
    91  	if c.LeaseDuration <= c.RenewTimeout {
    92  		c.LeaseDuration = c.RenewTimeout
    93  	}
    94  	if c.RenewTimeout <= time.Duration(JitterFactor*float64(c.RetryPeriod)) {
    95  		c.RenewTimeout = time.Duration(JitterFactor * float64(c.RetryPeriod))
    96  	}
    97  }
    98  func (c *Config) New() (*LeaderElector, error) {
    99  	return NewLeaderElector(*c)
   100  }