Skip to content

Commit

Permalink
wtclient: add backoff in negotiation when createSession() fails
Browse files Browse the repository at this point in the history
Currently if the tower hangs up during session negotiation there is no
backoff applied. We add backoff here to avoid excessive CPU/network
utilization during unexpected failures.
  • Loading branch information
cfromknecht authored and matheusd committed Jan 21, 2021
1 parent 56e38e5 commit 5cc0a05
Showing 1 changed file with 20 additions and 11 deletions.
31 changes: 20 additions & 11 deletions watchtower/wtclient/session_negotiator.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,19 @@ func (n *sessionNegotiator) negotiate() {
// backoff.
var backoff time.Duration

// Create a closure to update the backoff upon failure such that it
// stays within our min and max backoff parameters.
updateBackoff := func() {
if backoff == 0 {
backoff = n.cfg.MinBackoff
} else {
backoff *= 2
if backoff > n.cfg.MaxBackoff {
backoff = n.cfg.MaxBackoff
}
}
}

retryWithBackoff:
// If we are retrying, wait out the delay before continuing.
if backoff > 0 {
Expand All @@ -251,16 +264,8 @@ retryWithBackoff:
// Pull the next candidate from our list of addresses.
tower, err := n.cfg.Candidates.Next()
if err != nil {
if backoff == 0 {
backoff = n.cfg.MinBackoff
} else {
// We've run out of addresses, double and clamp
// backoff.
backoff *= 2
if backoff > n.cfg.MaxBackoff {
backoff = n.cfg.MaxBackoff
}
}
// We've run out of addresses, update our backoff.
updateBackoff()

log.Debugf("Unable to get new tower candidate, "+
"retrying after %v -- reason: %v", backoff, err)
Expand Down Expand Up @@ -293,10 +298,14 @@ retryWithBackoff:
// get a new session, trying all addresses if necessary.
err = n.createSession(tower, keyIndex)
if err != nil {
// An unexpected error occurred, updpate our backoff.
updateBackoff()

log.Debugf("Session negotiation with tower=%x "+
"failed, trying again -- reason: %v",
tower.IdentityKey.SerializeCompressed(), err)
continue

goto retryWithBackoff
}

// Success.
Expand Down

0 comments on commit 5cc0a05

Please sign in to comment.