Skip to content

Commit

Permalink
Merge pull request #68 from dodopizza/additional-break-condition
Browse files Browse the repository at this point in the history
Additional break condition
  • Loading branch information
evjenio authored Apr 5, 2023
2 parents e6e4966 + 84416cd commit 34dea9b
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 7 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,11 @@ Also you may check the [defaults](src/Dodo.HttpClient.ResiliencePolicies/Default
durationOfBreak: TimeSpan.FromSeconds(5),
samplingDuration: TimeSpan.FromSeconds(30)
),
OnRetry = (response, time) => { ... }, // Handle retry event. For example you may add logging here
OnBreak = (response, time) => { ... }, // Handle CircuitBreaker break event. For example you may add logging here
OnReset = () => {...}, // Handle CircuitBreaker reset event. For example you may add logging here
OnHalfOpen = () => {...}, // Handle CircuitBreaker reset event. For example you may add logging here
OnRetry = (response, time) => { ... }, // Handle retry event. For example you may add logging here
OnBreak = (response, time) => { ... }, // Handle CircuitBreaker break event. For example you may add logging here
OnReset = () => {...}, // Handle CircuitBreaker reset event. For example you may add logging here
OnHalfOpen = () => {...}, // Handle CircuitBreaker reset event. For example you may add logging here
ExtraBreakCondition = BreakConditions.OnTooManyRequests // Extra condition for CircuitBreaker to open (opens on TooManyRequests by default)
}
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,30 @@ await Helper.InvokeMultipleHttpRequests(wrapper.Client, taskCount,
Assert.AreEqual(minimumThroughput + taskCount, wrapper.NumberOfCalls);
}

[Test]
public void Should_not_break_on_429()
{
const int retryCount = 5;
const int minimumThroughput = 2;
var settings = new ResiliencePoliciesSettings
{
OverallTimeout = TimeSpan.FromSeconds(5),
RetryPolicySettings = RetryPolicySettings.Constant(retryCount, TimeSpan.FromMilliseconds(100)),
CircuitBreakerPolicySettings = BuildCircuitBreakerSettings(minimumThroughput),
ExtraBreakCondition = BreakConditions.None
};
var wrapper = Create.HttpClientWrapperWrapperBuilder
.WithStatusCode(HttpStatusCode.TooManyRequests)
.WithResiliencePolicySettings(settings)
.Please();

const int taskCount = 4;
Assert.DoesNotThrowAsync(async () =>
await Helper.InvokeMultipleHttpRequests(wrapper.Client, taskCount));

Assert.AreEqual(wrapper.NumberOfCalls, taskCount);
}

private static CircuitBreakerPolicySettings BuildCircuitBreakerSettings(int throughput)
{
return new CircuitBreakerPolicySettings(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using System.Net;
using System.Net.Http;

namespace Dodo.HttpClientResiliencePolicies.CircuitBreakerPolicy
{
public static class BreakConditions
{
public static readonly Func<HttpResponseMessage, bool> OnTooManyRequests = response =>
response.StatusCode == (HttpStatusCode)429; // Too Many Requests
public static readonly Func<HttpResponseMessage, bool> None = _ => false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public sealed class CircuitBreakerPolicySettings
internal Action<DelegateResult<HttpResponseMessage>, TimeSpan> OnBreak { get; set; }
internal Action OnReset { get; set; }
internal Action OnHalfOpen { get; set; }
internal Func<HttpResponseMessage, bool> ExtraBreakCondition { get; set; }

public CircuitBreakerPolicySettings()
: this(
Expand All @@ -38,6 +39,7 @@ public CircuitBreakerPolicySettings(
OnBreak = DoNothingOnBreak;
OnReset = DoNothingOnReset;
OnHalfOpen = DoNothingOnHalfOpen;
ExtraBreakCondition = BreakConditions.OnTooManyRequests;
}

private static readonly Action<DelegateResult<HttpResponseMessage>, TimeSpan> DoNothingOnBreak = (_, __) => { };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<TargetFramework Condition="'$(Framework)' == 'net5.0'">net5.0</TargetFramework>
<TargetFramework Condition="'$(Framework)' == 'netcoreapp3.1'">netcoreapp3.1</TargetFramework>
<LangVersion>8.0</LangVersion>
<VersionPrefix>2.0.2</VersionPrefix>
<VersionPrefix>2.1.0</VersionPrefix>
<Title>Dodo.HttpClient.ResiliencePolicies</Title>
<RootNamespace>Dodo.HttpClientResiliencePolicies</RootNamespace>
<WarningsAsErrors>true</WarningsAsErrors>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Net;
using System.Net.Http;
using Dodo.HttpClientResiliencePolicies.CircuitBreakerPolicy;
using Dodo.HttpClientResiliencePolicies.RetryPolicy;
Expand Down Expand Up @@ -80,7 +79,7 @@ private static IAsyncPolicy<HttpResponseMessage> BuildCircuitBreakerPolicy(
return HttpPolicyExtensions
.HandleTransientHttpError()
.Or<TimeoutRejectedException>()
.OrResult(r => r.StatusCode == (HttpStatusCode) 429) // Too Many Requests
.OrResult(settings.ExtraBreakCondition)
.AdvancedCircuitBreakerAsync(settings);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,13 @@ public CircuitBreakerPolicySettings CircuitBreakerPolicySettings
var onBreakHandler = OnBreak;
var onResetHandler = OnReset;
var onHalfOpenHandler = OnHalfOpen;
var extraBreakCondition = ExtraBreakCondition;

_circuitBreakerPolicySettings = value;
_circuitBreakerPolicySettings.OnBreak = onBreakHandler;
_circuitBreakerPolicySettings.OnReset = onResetHandler;
_circuitBreakerPolicySettings.OnHalfOpen = onHalfOpenHandler;
_circuitBreakerPolicySettings.ExtraBreakCondition = extraBreakCondition;
}
}

Expand All @@ -75,5 +77,11 @@ public Action OnHalfOpen
get => CircuitBreakerPolicySettings.OnHalfOpen;
set => CircuitBreakerPolicySettings.OnHalfOpen = value;
}

public Func<HttpResponseMessage, bool> ExtraBreakCondition
{
get => CircuitBreakerPolicySettings.ExtraBreakCondition;
set => CircuitBreakerPolicySettings.ExtraBreakCondition = value;
}
}
}

0 comments on commit 34dea9b

Please sign in to comment.