From fe455f098993d6b816e6b9593cf39db3c71b8983 Mon Sep 17 00:00:00 2001 From: maliming Date: Fri, 22 Aug 2025 16:21:20 +0800 Subject: [PATCH] Fix retry logic and delay calculation in InboxProcessor --- .../Abp/EventBus/Distributed/InboxProcessor.cs | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/Distributed/InboxProcessor.cs b/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/Distributed/InboxProcessor.cs index e4fa8a0b12..10251ffedb 100644 --- a/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/Distributed/InboxProcessor.cs +++ b/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/Distributed/InboxProcessor.cs @@ -133,21 +133,29 @@ public class InboxProcessor : IInboxProcessor, ITransientDependency { using (var uow = UnitOfWorkManager.Begin(isTransactional: true, requiresNew: true)) { - if (waitingEvent.RetryCount > EventBusBoxesOptions.InboxProcessorMaxRetryCount) + if (waitingEvent.NextRetryTime != null) + { + waitingEvent.RetryCount++; + } + + if (waitingEvent.RetryCount >= EventBusBoxesOptions.InboxProcessorMaxRetryCount) { Logger.LogWarning($"Event with id = {waitingEvent.Id:N} has exceeded the maximum retry count. Marking it as discarded."); + await Inbox.RetryLaterAsync(waitingEvent.Id, waitingEvent.RetryCount, null); await Inbox.MarkAsDiscardAsync(waitingEvent.Id); await uow.CompleteAsync(StoppingToken); continue; } + waitingEvent.NextRetryTime = GetNextRetryTime(waitingEvent.RetryCount); + Logger.LogInformation($"Event with id = {waitingEvent.Id:N} will retry later. " + $"Current retry count: {waitingEvent.RetryCount}, " + - $"Next retry time: {GetNextRetryTime(waitingEvent.RetryCount)}, " + + $"Next retry time: {waitingEvent.NextRetryTime}, " + $"Max retry count: {EventBusBoxesOptions.InboxProcessorMaxRetryCount}."); - await Inbox.RetryLaterAsync(waitingEvent.Id, ++waitingEvent.RetryCount, GetNextRetryTime(waitingEvent.RetryCount)); + await Inbox.RetryLaterAsync(waitingEvent.Id, waitingEvent.RetryCount, GetNextRetryTime(waitingEvent.RetryCount)); await uow.CompleteAsync(StoppingToken); } continue; @@ -182,7 +190,7 @@ public class InboxProcessor : IInboxProcessor, ITransientDependency protected virtual DateTime? GetNextRetryTime(int retryCount) { - var delaySeconds = 1 * (int)Math.Pow(2, retryCount - 1); + var delaySeconds = (int)Math.Pow(2, retryCount); return DateTime.Now.AddSeconds(delaySeconds); }