Skip to content

Commit cfd727d

Browse files
Makes the retry-after value in GenericRandomErrorPlugin configurable. Closes #520 (#537)
1 parent a8f4f7c commit cfd727d

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

dev-proxy-plugins/RandomErrors/GenericRandomErrorPlugin.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ internal enum GenericRandomErrorFailMode
2222
public class GenericRandomErrorConfiguration
2323
{
2424
public string? ErrorsFile { get; set; }
25+
public int RetryAfterInSeconds { get; set; } = 5;
2526
[JsonPropertyName("responses")]
2627
public IEnumerable<GenericErrorResponse> Responses { get; set; } = Array.Empty<GenericErrorResponse>();
2728
}
@@ -34,7 +35,6 @@ public class GenericRandomErrorPlugin : BaseProxyPlugin
3435

3536
public override string Name => nameof(GenericRandomErrorPlugin);
3637

37-
private const int retryAfterInSeconds = 5;
3838
private readonly Random _random;
3939

4040
public GenericRandomErrorPlugin()
@@ -55,7 +55,7 @@ private void FailResponse(ProxyRequestArgs e, GenericRandomErrorFailMode failMod
5555
private ThrottlingInfo ShouldThrottle(Request request, string throttlingKey)
5656
{
5757
var throttleKeyForRequest = BuildThrottleKey(request);
58-
return new ThrottlingInfo(throttleKeyForRequest == throttlingKey ? retryAfterInSeconds : 0, "Retry-After");
58+
return new ThrottlingInfo(throttleKeyForRequest == throttlingKey ? _configuration.RetryAfterInSeconds : 0, "Retry-After");
5959
}
6060

6161
private void UpdateProxyResponse(ProxyRequestArgs e, GenericErrorResponse error)
@@ -72,7 +72,7 @@ private void UpdateProxyResponse(ProxyRequestArgs e, GenericErrorResponse error)
7272
error.Headers is not null &&
7373
error.Headers.FirstOrDefault(h => h.Name == "Retry-After" || h.Name == "retry-after")?.Value == "@dynamic")
7474
{
75-
var retryAfterDate = DateTime.Now.AddSeconds(retryAfterInSeconds);
75+
var retryAfterDate = DateTime.Now.AddSeconds(_configuration.RetryAfterInSeconds);
7676
if (!e.GlobalData.ContainsKey(RetryAfterPlugin.ThrottledRequestsKey))
7777
{
7878
e.GlobalData.Add(RetryAfterPlugin.ThrottledRequestsKey, new List<ThrottlerInfo>());
@@ -82,7 +82,7 @@ error.Headers is not null &&
8282
// replace the header with the @dynamic value with the actual value
8383
var h = headers.First(h => h.Name == "Retry-After" || h.Name == "retry-after");
8484
headers.Remove(h);
85-
headers.Add(new("Retry-After", retryAfterInSeconds.ToString()));
85+
headers.Add(new("Retry-After", _configuration.RetryAfterInSeconds.ToString()));
8686
}
8787

8888
var statusCode = (HttpStatusCode)error.StatusCode;

0 commit comments

Comments
 (0)