Documentation
¶
Overview ¶
Package network holds the retry helpers shared by services that keep a connection open to a provider for the length of a call. Such a connection drops for ordinary reasons (a network blip, a server recycling), so a service reconnects rather than failing the call. These helpers decide how long to wait between attempts, and when waiting longer has stopped being the answer.
Index ¶
Constants ¶
const ( // DefaultMinWait is the shortest wait between two retry attempts. DefaultMinWait = 4 * time.Second // DefaultMaxWait is the longest wait between two retry attempts. DefaultMaxWait = 10 * time.Second // DefaultMultiplier scales the doubling. One means the raw powers of two. DefaultMultiplier = 1.0 )
const ( // DefaultMinStableDuration is how long a connection must survive before // QuickFailureTracker counts it as stable. DefaultMinStableDuration = 5 * time.Second // DefaultMaxConsecutiveFailures is how many quick failures in a row // QuickFailureTracker tolerates before telling the caller to give up. DefaultMaxConsecutiveFailures = 3 )
Variables ¶
This section is empty.
Functions ¶
func ExponentialBackoffTime ¶
func ExponentialBackoffTime(attempt int, minWait, maxWait time.Duration, multiplier float64) time.Duration
ExponentialBackoffTime is how long to wait before a retry, doubling with each attempt. attempt counts from one, and the wait is two to the power of attempt-1 seconds scaled by multiplier, then clamped to minWait..maxWait. A multiplier that is not a number yields maxWait, the most cautious answer available.
Types ¶
type QuickFailureConfig ¶
type QuickFailureConfig struct {
// MinStableDuration is how long a connection must survive to count as
// stable rather than as a quick failure.
MinStableDuration time.Duration
// MaxConsecutiveFailures is how many quick failures in a row end the
// retrying.
MaxConsecutiveFailures int
}
QuickFailureConfig configures a QuickFailureTracker. A zero field takes its default.
type QuickFailureResult ¶
type QuickFailureResult struct {
// QuickFailure reports whether the connection lasted less than the
// tracker's MinStableDuration.
QuickFailure bool
// GiveUp reports whether MaxConsecutiveFailures quick failures have now
// happened in a row, so the caller should stop retrying rather than wait
// longer and try again.
GiveUp bool
}
QuickFailureResult is what recording one failed connection revealed.
type QuickFailureTracker ¶
type QuickFailureTracker struct {
// contains filtered or unexported fields
}
QuickFailureTracker spots a connection that keeps failing the moment it is established. Backing off cannot help there: every attempt completes the handshake and then fails straight away, which is what a server does when it rejects the credentials after the upgrade rather than before it. Waiting longer between attempts changes nothing, so the tracker is what says to stop.
Report how long each failed connection lasted with Record. Once MaxConsecutiveFailures of them in a row each lasted less than MinStableDuration, the result says to give up.
A tracker is not safe for concurrent use. Call it from the goroutine that owns the connection.
func NewQuickFailureTracker ¶
func NewQuickFailureTracker(cfg QuickFailureConfig) *QuickFailureTracker
NewQuickFailureTracker builds a tracker from cfg.
func (*QuickFailureTracker) Count ¶
func (t *QuickFailureTracker) Count() int
Count is how many quick failures have happened in a row.
func (*QuickFailureTracker) MaxConsecutiveFailures ¶
func (t *QuickFailureTracker) MaxConsecutiveFailures() int
MaxConsecutiveFailures is how many quick failures in a row end the retrying.
func (*QuickFailureTracker) Record ¶
func (t *QuickFailureTracker) Record(d time.Duration) QuickFailureResult
Record notes a failed connection that lasted d, lengthening the streak when it was a quick failure and ending the streak when it was not.
func (*QuickFailureTracker) Reset ¶
func (t *QuickFailureTracker) Reset()
Reset ends the streak, for a connection that is starting fresh.