github.com/mattermost/mattermost-plugin-api@v0.1.4/cluster/wait.go (about)

     1  package cluster
     2  
     3  import (
     4  	"math/rand"
     5  	"time"
     6  )
     7  
     8  const (
     9  	// minWaitInterval is the minimum amount of time to wait between locking attempts
    10  	minWaitInterval = 1 * time.Second
    11  
    12  	// maxWaitInterval is the maximum amount of time to wait between locking attempts
    13  	maxWaitInterval = 5 * time.Minute
    14  
    15  	// pollWaitInterval is the usual time to wait between unsuccessful locking attempts
    16  	pollWaitInterval = 1 * time.Second
    17  
    18  	// jitterWaitInterval is the amount of jitter to add when waiting to avoid thundering herds
    19  	jitterWaitInterval = minWaitInterval / 2
    20  )
    21  
    22  // nextWaitInterval determines how long to wait until the next lock retry.
    23  func nextWaitInterval(lastWaitInterval time.Duration, err error) time.Duration {
    24  	nextWaitInterval := lastWaitInterval
    25  
    26  	if nextWaitInterval <= 0 {
    27  		nextWaitInterval = minWaitInterval
    28  	}
    29  
    30  	if err != nil {
    31  		nextWaitInterval *= 2
    32  		if nextWaitInterval > maxWaitInterval {
    33  			nextWaitInterval = maxWaitInterval
    34  		}
    35  	} else {
    36  		nextWaitInterval = pollWaitInterval
    37  	}
    38  
    39  	// Add some jitter to avoid unnecessary collision between competing plugin instances.
    40  	nextWaitInterval += time.Duration(rand.Int63n(int64(jitterWaitInterval)) - int64(jitterWaitInterval)/2)
    41  
    42  	return nextWaitInterval
    43  }