forked from actions/runner
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Backoff to avoid excessive retries to Run Service in a duration (acti…
- Loading branch information
1 parent
ecb732e
commit 054fc2e
Showing
6 changed files
with
318 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using GitHub.Runner.Common; | ||
using GitHub.Services.Common; | ||
|
||
namespace GitHub.Runner.Listener | ||
{ | ||
[ServiceLocator(Default = typeof(ErrorThrottler))] | ||
public interface IErrorThrottler : IRunnerService | ||
{ | ||
void Reset(); | ||
Task IncrementAndWaitAsync(CancellationToken token); | ||
} | ||
|
||
public sealed class ErrorThrottler : RunnerService, IErrorThrottler | ||
{ | ||
internal static readonly TimeSpan MinBackoff = TimeSpan.FromSeconds(1); | ||
internal static readonly TimeSpan MaxBackoff = TimeSpan.FromMinutes(1); | ||
internal static readonly TimeSpan BackoffCoefficient = TimeSpan.FromSeconds(1); | ||
private int _count = 0; | ||
|
||
public void Reset() | ||
{ | ||
_count = 0; | ||
} | ||
|
||
public async Task IncrementAndWaitAsync(CancellationToken token) | ||
{ | ||
if (++_count <= 1) | ||
{ | ||
return; | ||
} | ||
|
||
TimeSpan backoff = BackoffTimerHelper.GetExponentialBackoff( | ||
attempt: _count - 2, // 0-based attempt | ||
minBackoff: MinBackoff, | ||
maxBackoff: MaxBackoff, | ||
deltaBackoff: BackoffCoefficient); | ||
Trace.Warning($"Back off {backoff.TotalSeconds} seconds before next attempt. Current consecutive error count: {_count}"); | ||
await HostContext.Delay(backoff, token); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,213 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using GitHub.DistributedTask.WebApi; | ||
using GitHub.Runner.Listener; | ||
using GitHub.Runner.Listener.Configuration; | ||
using GitHub.Runner.Common.Tests; | ||
using System.Runtime.CompilerServices; | ||
using GitHub.Services.WebApi; | ||
using Moq; | ||
using Xunit; | ||
|
||
namespace GitHub.Runner.Common.Tests.Listener | ||
{ | ||
public sealed class ErrorThrottlerL0 | ||
{ | ||
[Theory] | ||
[InlineData(1)] | ||
[InlineData(2)] | ||
[InlineData(3)] | ||
[InlineData(4)] | ||
[InlineData(5)] | ||
[InlineData(6)] | ||
[InlineData(7)] | ||
[InlineData(8)] | ||
public async void TestIncrementAndWait(int totalAttempts) | ||
{ | ||
using (TestHostContext hc = CreateTestContext()) | ||
{ | ||
// Arrange | ||
var errorThrottler = new ErrorThrottler(); | ||
errorThrottler.Initialize(hc); | ||
var eventArgs = new List<DelayEventArgs>(); | ||
hc.Delaying += (sender, args) => | ||
{ | ||
eventArgs.Add(args); | ||
}; | ||
|
||
// Act | ||
for (int attempt = 1; attempt <= totalAttempts; attempt++) | ||
{ | ||
await errorThrottler.IncrementAndWaitAsync(CancellationToken.None); | ||
} | ||
|
||
// Assert | ||
Assert.Equal(totalAttempts - 1, eventArgs.Count); | ||
for (int i = 0; i < eventArgs.Count; i++) | ||
{ | ||
// Expected milliseconds | ||
int expectedMin; | ||
int expectedMax; | ||
|
||
switch (i) | ||
{ | ||
case 0: | ||
expectedMin = 1000; // Min backoff | ||
expectedMax = 1000; | ||
break; | ||
case 1: | ||
expectedMin = 1800; // Min + 0.8 * Coefficient | ||
expectedMax = 2200; // Min + 1.2 * Coefficient | ||
break; | ||
case 2: | ||
expectedMin = 3400; // Min + 0.8 * Coefficient * 3 | ||
expectedMax = 4600; // Min + 1.2 * Coefficient * 3 | ||
break; | ||
case 3: | ||
expectedMin = 6600; // Min + 0.8 * Coefficient * 7 | ||
expectedMax = 9400; // Min + 1.2 * Coefficient * 7 | ||
break; | ||
case 4: | ||
expectedMin = 13000; // Min + 0.8 * Coefficient * 15 | ||
expectedMax = 19000; // Min + 1.2 * Coefficient * 15 | ||
break; | ||
case 5: | ||
expectedMin = 25800; // Min + 0.8 * Coefficient * 31 | ||
expectedMax = 38200; // Min + 1.2 * Coefficient * 31 | ||
break; | ||
case 6: | ||
expectedMin = 51400; // Min + 0.8 * Coefficient * 63 | ||
expectedMax = 60000; // Max backoff | ||
break; | ||
case 7: | ||
expectedMin = 60000; | ||
expectedMax = 60000; | ||
break; | ||
default: | ||
throw new NotSupportedException("Unexpected eventArgs count"); | ||
} | ||
|
||
var actualMilliseconds = eventArgs[i].Delay.TotalMilliseconds; | ||
Assert.True(expectedMin <= actualMilliseconds, $"Unexpected min delay for eventArgs[{i}]. Expected min {expectedMin}, actual {actualMilliseconds}"); | ||
Assert.True(expectedMax >= actualMilliseconds, $"Unexpected max delay for eventArgs[{i}]. Expected max {expectedMax}, actual {actualMilliseconds}"); | ||
} | ||
} | ||
} | ||
|
||
[Fact] | ||
public async void TestReset() | ||
{ | ||
using (TestHostContext hc = CreateTestContext()) | ||
{ | ||
// Arrange | ||
var errorThrottler = new ErrorThrottler(); | ||
errorThrottler.Initialize(hc); | ||
var eventArgs = new List<DelayEventArgs>(); | ||
hc.Delaying += (sender, args) => | ||
{ | ||
eventArgs.Add(args); | ||
}; | ||
|
||
// Act | ||
await errorThrottler.IncrementAndWaitAsync(CancellationToken.None); | ||
await errorThrottler.IncrementAndWaitAsync(CancellationToken.None); | ||
await errorThrottler.IncrementAndWaitAsync(CancellationToken.None); | ||
errorThrottler.Reset(); | ||
await errorThrottler.IncrementAndWaitAsync(CancellationToken.None); | ||
await errorThrottler.IncrementAndWaitAsync(CancellationToken.None); | ||
await errorThrottler.IncrementAndWaitAsync(CancellationToken.None); | ||
|
||
// Assert | ||
Assert.Equal(4, eventArgs.Count); | ||
for (int i = 0; i < eventArgs.Count; i++) | ||
{ | ||
// Expected milliseconds | ||
int expectedMin; | ||
int expectedMax; | ||
|
||
switch (i) | ||
{ | ||
case 0: | ||
case 2: | ||
expectedMin = 1000; // Min backoff | ||
expectedMax = 1000; | ||
break; | ||
case 1: | ||
case 3: | ||
expectedMin = 1800; // Min + 0.8 * Coefficient | ||
expectedMax = 2200; // Min + 1.2 * Coefficient | ||
break; | ||
default: | ||
throw new NotSupportedException("Unexpected eventArgs count"); | ||
} | ||
|
||
var actualMilliseconds = eventArgs[i].Delay.TotalMilliseconds; | ||
Assert.True(expectedMin <= actualMilliseconds, $"Unexpected min delay for eventArgs[{i}]. Expected min {expectedMin}, actual {actualMilliseconds}"); | ||
Assert.True(expectedMax >= actualMilliseconds, $"Unexpected max delay for eventArgs[{i}]. Expected max {expectedMax}, actual {actualMilliseconds}"); | ||
} | ||
} | ||
} | ||
|
||
[Fact] | ||
public async void TestReceivesCancellationToken() | ||
{ | ||
using (TestHostContext hc = CreateTestContext()) | ||
{ | ||
// Arrange | ||
var errorThrottler = new ErrorThrottler(); | ||
errorThrottler.Initialize(hc); | ||
var eventArgs = new List<DelayEventArgs>(); | ||
hc.Delaying += (sender, args) => | ||
{ | ||
eventArgs.Add(args); | ||
}; | ||
var cancellationTokenSource1 = new CancellationTokenSource(); | ||
var cancellationTokenSource2 = new CancellationTokenSource(); | ||
var cancellationTokenSource3 = new CancellationTokenSource(); | ||
|
||
// Act | ||
await errorThrottler.IncrementAndWaitAsync(cancellationTokenSource1.Token); | ||
await errorThrottler.IncrementAndWaitAsync(cancellationTokenSource2.Token); | ||
await errorThrottler.IncrementAndWaitAsync(cancellationTokenSource3.Token); | ||
|
||
// Assert | ||
Assert.Equal(2, eventArgs.Count); | ||
Assert.Equal(cancellationTokenSource2.Token, eventArgs[0].Token); | ||
Assert.Equal(cancellationTokenSource3.Token, eventArgs[1].Token); | ||
} | ||
} | ||
|
||
[Fact] | ||
public async void TestReceivesSender() | ||
{ | ||
using (TestHostContext hc = CreateTestContext()) | ||
{ | ||
// Arrange | ||
var errorThrottler = new ErrorThrottler(); | ||
errorThrottler.Initialize(hc); | ||
var senders = new List<object>(); | ||
hc.Delaying += (sender, args) => | ||
{ | ||
senders.Add(sender); | ||
}; | ||
|
||
// Act | ||
await errorThrottler.IncrementAndWaitAsync(CancellationToken.None); | ||
await errorThrottler.IncrementAndWaitAsync(CancellationToken.None); | ||
await errorThrottler.IncrementAndWaitAsync(CancellationToken.None); | ||
|
||
// Assert | ||
Assert.Equal(2, senders.Count); | ||
Assert.Equal(hc, senders[0]); | ||
Assert.Equal(hc, senders[1]); | ||
} | ||
} | ||
|
||
private TestHostContext CreateTestContext([CallerMemberName] String testName = "") | ||
{ | ||
return new TestHostContext(this, testName); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.