avast/retry-go

Limit backoff delay

neta540 opened this issue · 0 comments

I think it will be nice to limit the backoff delay to a specific n in the sequence. For instance, when delay time is 30 minutes and n is 5, the delay time sequence (in hours) will be 0.5, 1, 2, 4, 8, 8, 8, 8, 8.... This way, when (as in the example with testing connection to a remote server) the remote server is down only for a short period of time, it could be detected rather quickly with the first few retries, but when the downtime of the server is long, we'd want to test the server every 8 hours but not more than that because if the server is down for a whole week for instance the delay could get way way too high. Hitting the server at constant intervals like 2 hours doesn't make sense either for when the downtime is short. Having a constant interval shorter than 2 hours will cause too many retry attempts with a server of long downtimes.

Edit: sure it's possible to call the retry twice, one with backoff delay and if fails to call it again with a constant delay of 8 hours. But it's more elegant just to limit that value instead of calling the retry function twice.

Edit 2: A closure function to set it is fine.

func delaySetter(base time.Duration, max uint) func(n uint, config *retry.Config) time.Duration {
  return func(n uint, config *retry.Config) time.Duration {
    if n < max {
      return base * (1 << n)
    }
    return base * (1 << max)
  }
}

calling it as retry.DelayType(delaySetter(30*time.Minute, 4))