Skip to content

Instantly share code, notes, and snippets.

@marraia
Created July 27, 2018 15:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marraia/304d2baf4116a4166d1fecb6cc43e094 to your computer and use it in GitHub Desktop.
Save marraia/304d2baf4116a4166d1fecb6cc43e094 to your computer and use it in GitHub Desktop.
private CircuitBreakerPolicy CircuitBreakerPolicy()
{
var circuitBreaker= Policy<ApiResponse>
.Handle<HttpRequestException>()
.CircuitBreakerAsync(
durationOfBreak: TimeSpan.FromSeconds(30),
handledEventsAllowedBeforeBreaking: 1,
onBreak: async (result, timeSpan) =>
{
await CircuitOpen(result,timeSpan);
},
onReset: async () =>
{
var msg ="CLOSING CIRCUIT! (CIRCUIT RESETED)";
_log.LogWarning(msg);
});
return circuitBreaker;
}
private async Task CircuitOpen(DelegateResult result, TimeSpan timeSpan)
{
var msg =
$" OPENED CIRCUIT (CIRCUIT BREAKED): {timeSpan} |" +
$" WAITING: {timeSpan} |" +
$" STATUSCODE: {result.Result.StatusCode} |" +
$" REASONPHRASE: {result.Result.ReasonPhrase} |";
_log.LogWarning(msg);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment