Skip to content

Commit 664e51d

Browse files
committed
Add exponential backoff delay
1 parent ce72d49 commit 664e51d

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

src/ServiceControl.Infrastructure/AsyncTimer.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,15 @@ public TimerJob(Func<CancellationToken, Task<TimerJobExecutionResult>> callback,
2424
{
2525
await Task.Delay(due, token).ConfigureAwait(false);
2626

27+
var consecutiveFailures = 0;
28+
2729
while (!token.IsCancellationRequested)
2830
{
2931
try
3032
{
3133
var result = await callback(token).ConfigureAwait(false);
34+
35+
consecutiveFailures = 0;
3236
if (result == TimerJobExecutionResult.DoNotContinueExecuting)
3337
{
3438
tokenSource.Cancel();
@@ -46,6 +50,11 @@ public TimerJob(Func<CancellationToken, Task<TimerJobExecutionResult>> callback,
4650
}
4751
catch (Exception ex)
4852
{
53+
consecutiveFailures++;
54+
var exponentialBackoffDelay = TimeSpan.FromSeconds(int.Max(60, consecutiveFailures * consecutiveFailures));
55+
56+
await Task.Delay(exponentialBackoffDelay, token).ConfigureAwait(false);
57+
4958
errorCallback(ex);
5059
}
5160
}

0 commit comments

Comments
 (0)