0

I'm writing my own retry logic with exponential backoff based on Microsoft's sample code on following page: https://learn.microsoft.com/en-us/dotnet/standard/microservices-architecture/implement-resilient-applications/explore-custom-http-call-retries-exponential-backoff

In the following line of code there is a division by 2 that I can't understand:

int delay = Math.Min(m_delayMilliseconds * (m_pow - 1) / 2,
        m_maxDelayMilliseconds);

Assume I defined int m_delayMilliseconds = 200, so we get following delays:

200 * 1 / 2 --> 100 ms

200 * 2 / 2 --> 200 ms

200 * 4 / 2 --> 400 ms

200 * 8 / 2 --> 800 ms

200 * 16 / 2 --> 1600 ms

. . . etc.

What disturbs me is that I get 100 ms for the first delay, but I want the minimum delay to be 200 ms, as defined. Can someone explain this to me?

bigb055
  • 198
  • 3
  • 14
  • 3
    Just remove the divide by two. – Robert Harvey Jan 08 '19 at 21:28
  • @RobertHarvey Still can't understand the purpose of dividing by 2. – bigb055 Jan 08 '19 at 21:39
  • 2
    This is an example of exponential backoff where the first step is only half of the delay? The purpose of it is to not delay too much for the very first step, that's all. – zaitsman Jan 08 '19 at 23:10
  • 1
    The link is broken but I think you were referring to [this calculation](https://github.com/jongio/docs-1/blob/master/docs/architecture/microservices/implement-resilient-applications/explore-custom-http-call-retries-exponential-backoff.md) – Peter Csala Oct 19 '22 at 13:38

0 Answers0