-
Notifications
You must be signed in to change notification settings - Fork 32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Outbox cleanup circuit breaker #242
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
namespace NServiceBus | ||
{ | ||
using System; | ||
using System.Threading; | ||
using Logging; | ||
|
||
class RepeatedFailuresOverTimeCircuitBreaker : IDisposable | ||
{ | ||
public RepeatedFailuresOverTimeCircuitBreaker(string name, TimeSpan timeToWaitBeforeTriggering, Action<Exception> triggerAction) | ||
{ | ||
this.name = name; | ||
this.triggerAction = triggerAction; | ||
this.timeToWaitBeforeTriggering = timeToWaitBeforeTriggering; | ||
|
||
timer = new Timer(CircuitBreakerTriggered); | ||
} | ||
|
||
public void Success() | ||
{ | ||
var oldValue = Interlocked.Exchange(ref failureCount, 0); | ||
|
||
if (oldValue == 0) | ||
{ | ||
return; | ||
} | ||
|
||
timer.Change(System.Threading.Timeout.Infinite, System.Threading.Timeout.Infinite); | ||
Logger.InfoFormat("The circuit breaker for {0} is now disarmed", name); | ||
} | ||
|
||
public void Failure(Exception exception) | ||
{ | ||
lastException = exception; | ||
var newValue = Interlocked.Increment(ref failureCount); | ||
|
||
if (newValue == 1) | ||
{ | ||
timer.Change(timeToWaitBeforeTriggering, NoPeriodicTriggering); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as the above. |
||
Logger.WarnFormat("The circuit breaker for {0} is now in the armed state", name); | ||
} | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
//Injected | ||
} | ||
|
||
void CircuitBreakerTriggered(object state) | ||
{ | ||
if (Interlocked.Read(ref failureCount) > 0) | ||
{ | ||
Logger.WarnFormat("The circuit breaker for {0} will now be triggered", name); | ||
triggerAction(lastException); | ||
} | ||
} | ||
|
||
long failureCount; | ||
Exception lastException; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is the last exception enough? or should we go with an AggregateException instead? That'd require a bit more code to ensure exceptions are aggregated correctly and cleared when successful. |
||
|
||
string name; | ||
Timer timer; | ||
TimeSpan timeToWaitBeforeTriggering; | ||
Action<Exception> triggerAction; | ||
|
||
static TimeSpan NoPeriodicTriggering = TimeSpan.FromMilliseconds(-1); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Isn't this the same as Timeout.Infinite? |
||
static ILog Logger = LogManager.GetLogger<RepeatedFailuresOverTimeCircuitBreaker>(); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a bit hard to read and capture the intent. Would it be a good idea to have a properly named/intent capturing private method? This is subjective of course so feel free to ignore if you disagree.