Fix logic fail

This commit is contained in:
Neil Alexander 2020-08-07 17:32:12 +01:00
parent b4d157c6cf
commit 12f2388777
No known key found for this signature in database
GPG key ID: A02A2019A2BB0944
2 changed files with 5 additions and 24 deletions

View file

@ -125,14 +125,8 @@ func (s *ServerStatistics) BackoffIfRequired(backingOff atomic.Bool, interrupt <
return 0 return 0
} }
// Work out how many times we've backed off so far. If we // Work out how many times we've backed off so far.
// have passed the failure counter then we can stop backing
// off - we've done our time.
count := s.backoffCount.Inc() count := s.backoffCount.Inc()
if count >= s.failCounter.Load() {
s.backoffStarted.Store(false)
return 0
}
// Notify the destination queue that we're backing off now. // Notify the destination queue that we're backing off now.
backingOff.Store(true) backingOff.Store(true)
@ -140,7 +134,7 @@ func (s *ServerStatistics) BackoffIfRequired(backingOff atomic.Bool, interrupt <
// Work out how long we should be backing off for. // Work out how long we should be backing off for.
duration := time.Second * time.Duration(math.Exp2(float64(count))) duration := time.Second * time.Duration(math.Exp2(float64(count)))
logrus.Debugf("Backing off %q for %d", s.serverName, duration) logrus.Infof("Backing off %q for %s", s.serverName, duration)
// Wait for either an interruption or for the backoff to // Wait for either an interruption or for the backoff to
// complete. // complete.

View file

@ -50,22 +50,9 @@ func TestBackoff(t *testing.T) {
duration := server.BackoffIfRequired(backingOff, interrupt) duration := server.BackoffIfRequired(backingOff, interrupt)
t.Logf("Backoff %d is for %s", i, duration) t.Logf("Backoff %d is for %s", i, duration)
// If we were below the number of failures that we simulated // Check if the duration is what we expect.
// then we should expect a backoff that is exponentially
// related. Otherwise, if we've gone beyond the number of
// failures then we should expect no backoff at all.
if i < failures {
// We're expecting backoff here because we're under the
// failure count.
if wanted := time.Second * time.Duration(math.Exp2(float64(i))); duration != wanted { if wanted := time.Second * time.Duration(math.Exp2(float64(i))); duration != wanted {
t.Fatalf("Backoff should have been %s but was %s", wanted, duration) t.Fatalf("Backoff should have been %s but was %s", wanted, duration)
} }
} else {
// We aren't expecting backoff here because we've exceeded
// the failure count, so our backoff is "complete".
if duration != 0 {
t.Fatalf("Backoff should have been zero but was %s", duration)
}
}
} }
} }