1

Is it possible to define exponential backoff with randomisation for the retry below?

public class SomeHandler : IHandleMessages<DoStuff>, IHandleMessages<IFailed<DoStuff>>
{
    readonly IBus _bus;

    public SomeHandle(IBus bus)
    {
        _bus = bus;
    }

    public async Task Handle(DoStuff message)
    {
        // do stuff that can fail here...
    }

    public async Task Handle(IFailed<DoStuff> failedMessage)
    {
        await _bus.Advanced.TransportMessage.Defer(TimeSpan.FromSeconds(30));
    }
}

https://github.com/rebus-org/Rebus/wiki/Automatic-retries-and-error-handling

For example like below: exponential back-off plus some jitter

Random jitterer = new Random(); 
Policy
  .Handle<HttpResponseException>() // etc
  .WaitAndRetry(5,    // exponential back-off plus some jitter
      retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt))  
                    + TimeSpan.FromMilliseconds(jitterer.Next(0, 100)) 
  );

https://learn.microsoft.com/en-us/dotnet/standard/microservices-architecture/implement-resilient-applications/implement-http-call-retries-exponential-backoff-polly

https://github.com/App-vNext/Polly/wiki/Retry#overall-number-of-attempts

Pingpong
  • 7,681
  • 21
  • 83
  • 209

1 Answers1

0

It is. A way to do it would be by passing some kind of state in an extra header on the deferred message:

var headers = new Dictionary<string, string>();

await _bus.Advanced.TransportMessage.Defer(TimeSpan.FromSeconds(30), headers);

e.g. by counting delivery attempts:

var attempt = failedMessage.Headers.TryGetValue("delivery-attempt", out var x)
    ? x
    : 0;

var delay = FigureOutDelay(attempt);

var headers = new Dictionary<string, string> {
    {"delivery-attempt", (attempt+1).ToString()}
};

await _bus.Advanced.TransportMessage.Defer(delay, headers);

plus a little extra to e.g. forward to the dead-letter queue, if it keeps failing for too long.

mookid8000
  • 18,258
  • 2
  • 39
  • 63