This repository has been archived by the owner on Jan 8, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from ThreeMammals/feature/moveing-polly-code-fr…
…om-ocelot Feature/moveing polly code from ocelot
- Loading branch information
Showing
21 changed files
with
690 additions
and
19 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
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,17 @@ | ||
using Polly.CircuitBreaker; | ||
using Polly.Timeout; | ||
|
||
namespace Ocelot.Provider.Polly | ||
{ | ||
public class CircuitBreaker | ||
{ | ||
public CircuitBreaker(CircuitBreakerPolicy circuitBreakerPolicy, TimeoutPolicy timeoutPolicy) | ||
{ | ||
CircuitBreakerPolicy = circuitBreakerPolicy; | ||
TimeoutPolicy = timeoutPolicy; | ||
} | ||
|
||
public CircuitBreakerPolicy CircuitBreakerPolicy { get; private set; } | ||
public TimeoutPolicy TimeoutPolicy { get; private set; } | ||
} | ||
} |
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
43 changes: 43 additions & 0 deletions
43
src/Ocelot.Provider.Polly/PollyCircuitBreakingDelegatingHandler.cs
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,43 @@ | ||
using System.Net.Http; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Ocelot.Logging; | ||
using Polly; | ||
using Polly.CircuitBreaker; | ||
|
||
namespace Ocelot.Provider.Polly | ||
{ | ||
public class PollyCircuitBreakingDelegatingHandler : DelegatingHandler | ||
{ | ||
private readonly PollyQoSProvider _qoSProvider; | ||
private readonly IOcelotLogger _logger; | ||
|
||
public PollyCircuitBreakingDelegatingHandler( | ||
PollyQoSProvider qoSProvider, | ||
IOcelotLoggerFactory loggerFactory) | ||
{ | ||
_qoSProvider = qoSProvider; | ||
_logger = loggerFactory.CreateLogger<PollyCircuitBreakingDelegatingHandler>(); | ||
} | ||
|
||
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) | ||
{ | ||
try | ||
{ | ||
return await Policy | ||
.WrapAsync(_qoSProvider.CircuitBreaker.CircuitBreakerPolicy, _qoSProvider.CircuitBreaker.TimeoutPolicy) | ||
.ExecuteAsync(() => base.SendAsync(request,cancellationToken)); | ||
} | ||
catch (BrokenCircuitException ex) | ||
{ | ||
_logger.LogError($"Reached to allowed number of exceptions. Circuit is open",ex); | ||
throw; | ||
} | ||
catch (HttpRequestException ex) | ||
{ | ||
_logger.LogError($"Error in CircuitBreakingDelegatingHandler.SendAync", ex); | ||
throw; | ||
} | ||
} | ||
} | ||
} |
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,52 @@ | ||
namespace Ocelot.Provider.Polly | ||
{ | ||
using System; | ||
using System.Net.Http; | ||
using global::Polly; | ||
using global::Polly.CircuitBreaker; | ||
using global::Polly.Timeout; | ||
using Ocelot.Configuration; | ||
using Ocelot.Logging; | ||
|
||
public class PollyQoSProvider | ||
{ | ||
private readonly CircuitBreakerPolicy _circuitBreakerPolicy; | ||
private readonly TimeoutPolicy _timeoutPolicy; | ||
private readonly IOcelotLogger _logger; | ||
|
||
public PollyQoSProvider(DownstreamReRoute reRoute, IOcelotLoggerFactory loggerFactory) | ||
{ | ||
_logger = loggerFactory.CreateLogger<PollyQoSProvider>(); | ||
|
||
Enum.TryParse(reRoute.QosOptions.TimeoutStrategy, out TimeoutStrategy strategy); | ||
|
||
_timeoutPolicy = Policy.TimeoutAsync(TimeSpan.FromMilliseconds(reRoute.QosOptions.TimeoutValue), strategy); | ||
|
||
_circuitBreakerPolicy = Policy | ||
.Handle<HttpRequestException>() | ||
.Or<TimeoutRejectedException>() | ||
.Or<TimeoutException>() | ||
.CircuitBreakerAsync( | ||
exceptionsAllowedBeforeBreaking: reRoute.QosOptions.ExceptionsAllowedBeforeBreaking, | ||
durationOfBreak: TimeSpan.FromMilliseconds(reRoute.QosOptions.DurationOfBreak), | ||
onBreak: (ex, breakDelay) => | ||
{ | ||
_logger.LogError( | ||
".Breaker logging: Breaking the circuit for " + breakDelay.TotalMilliseconds + "ms!", ex); | ||
}, | ||
onReset: () => | ||
{ | ||
_logger.LogDebug(".Breaker logging: Call ok! Closed the circuit again."); | ||
}, | ||
onHalfOpen: () => | ||
{ | ||
_logger.LogDebug(".Breaker logging: Half-open; next call is a trial."); | ||
} | ||
); | ||
|
||
CircuitBreaker = new CircuitBreaker(_circuitBreakerPolicy, _timeoutPolicy); | ||
} | ||
|
||
public CircuitBreaker CircuitBreaker { get; } | ||
} | ||
} |
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,13 @@ | ||
namespace Ocelot.Provider.Polly | ||
{ | ||
using System; | ||
using Errors; | ||
|
||
public class RequestTimedOutError : Error | ||
{ | ||
public RequestTimedOutError(Exception exception) | ||
: base($"Timeout making http request, exception: {exception}", OcelotErrorCode.RequestTimedOutError) | ||
{ | ||
} | ||
} | ||
} |
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.