gitlab.com/SkynetLabs/skyd@v1.6.9/skymodules/renter/workercooldown.go (about)

     1  package renter
     2  
     3  import (
     4  	"time"
     5  
     6  	"gitlab.com/NebulousLabs/fastrand"
     7  )
     8  
     9  const (
    10  	// cooldownMaxConsecutiveFailures defines the maximum number of consecutive
    11  	// failures that will be considered when determining how long a worker
    12  	// should be on cooldown.
    13  	cooldownMaxConsecutiveFailures = 10
    14  
    15  	// cooldownBaseMaxMilliseconds defines the maximum number of milliseconds
    16  	// that a worker will go on cooldown for if they have 0 consecutive
    17  	// failures. We use thousands of milliseconds instead of full seconds
    18  	// because we use a random number generator to pick a random number of
    19  	// milliseconds between 0 and max, and we want to have more granularity.
    20  	cooldownBaseMaxMilliseconds = 10e3
    21  
    22  	// cooldownBaseMinMilliseconds sets a minimum amount of time that a worker
    23  	// will go on cooldown.
    24  	cooldownBaseMinMilliseconds = 1e3
    25  )
    26  
    27  // cooldownUntil returns the next time a job should be attempted given the
    28  // number of consecutive failures in attempting this type of job.
    29  func cooldownUntil(consecutiveFailures uint64) time.Time {
    30  	// Cap the number of consecutive failures to 10.
    31  	if consecutiveFailures > cooldownMaxConsecutiveFailures {
    32  		consecutiveFailures = cooldownMaxConsecutiveFailures
    33  	}
    34  
    35  	// Get a random cooldown time between 1e3 and 10e3 milliseconds.
    36  	randMs := fastrand.Intn(cooldownBaseMaxMilliseconds - cooldownBaseMinMilliseconds)
    37  	randMs += cooldownBaseMinMilliseconds
    38  	randCooldown := time.Duration(randMs) * time.Millisecond
    39  	// Double the cooldown time for each consecutive failure, max possible
    40  	// cooldown time of ~3 hours.
    41  	for i := uint64(0); i < consecutiveFailures; i++ {
    42  		randCooldown *= 2
    43  	}
    44  	return time.Now().Add(randCooldown)
    45  }