重用二级重试

时间:2019-05-16 00:30:26

标签: rebus rebus-azureservicebus

是否可以为下面的重试定义随机化的指数补偿?

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

例如下面的例子:指数补偿加上一些抖动

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://docs.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

1 个答案:

答案 0 :(得分:0)

是的。一种方法是在延迟的邮件的额外标头中传递某种状态:

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

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

例如通过计算投放次数:

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);

加上一些额外的东西,例如如果它持续失败的时间太长,则转发到死信队列。