diff --git a/src/NServiceBus.Transport.Msmq/DelayedDelivery/DelayedDeliveryPump.cs b/src/NServiceBus.Transport.Msmq/DelayedDelivery/DelayedDeliveryPump.cs index c21e185c..871a4a51 100644 --- a/src/NServiceBus.Transport.Msmq/DelayedDelivery/DelayedDeliveryPump.cs +++ b/src/NServiceBus.Transport.Msmq/DelayedDelivery/DelayedDeliveryPump.cs @@ -9,7 +9,7 @@ namespace NServiceBus.Transport.Msmq.DelayedDelivery using Logging; using Routing; - class DelayedDeliveryPump + class DelayedDeliveryPump : IDisposable { public DelayedDeliveryPump(MsmqMessageDispatcher dispatcher, DueDelayedMessagePoller poller, @@ -137,6 +137,12 @@ async Task OnError(ErrorContext errorContext, CancellationTok return ErrorHandleResult.Handled; } + public void Dispose() + { + poller.Dispose(); + storeCircuitBreaker.Dispose(); + } + readonly MsmqMessageDispatcher dispatcher; readonly DueDelayedMessagePoller poller; readonly IDelayedMessageStore storage; diff --git a/src/NServiceBus.Transport.Msmq/DelayedDelivery/DueDelayedMessagePoller.cs b/src/NServiceBus.Transport.Msmq/DelayedDelivery/DueDelayedMessagePoller.cs index 3e03d962..d4997236 100644 --- a/src/NServiceBus.Transport.Msmq/DelayedDelivery/DueDelayedMessagePoller.cs +++ b/src/NServiceBus.Transport.Msmq/DelayedDelivery/DueDelayedMessagePoller.cs @@ -11,7 +11,7 @@ namespace NServiceBus.Transport.Msmq.DelayedDelivery using Routing; using Unicast.Queuing; - class DueDelayedMessagePoller + class DueDelayedMessagePoller : IDisposable { public DueDelayedMessagePoller(MsmqMessageDispatcher dispatcher, IDelayedMessageStore delayedMessageStore, @@ -334,6 +334,14 @@ await dispatcher.Dispatch(new TransportOperations(transportOperation), transport } } + public void Dispose() + { + fetchCircuitBreaker.Dispose(); + dispatchCircuitBreaker.Dispose(); + failureHandlingCircuitBreaker.Dispose(); + tokenSource?.Dispose(); + } + static readonly ILog Log = LogManager.GetLogger(); static readonly TimeSpan MaxSleepDuration = TimeSpan.FromMinutes(1); diff --git a/src/NServiceBus.Transport.Msmq/FailureRateCircuitBreaker.cs b/src/NServiceBus.Transport.Msmq/FailureRateCircuitBreaker.cs index c13bd19e..9596a7db 100644 --- a/src/NServiceBus.Transport.Msmq/FailureRateCircuitBreaker.cs +++ b/src/NServiceBus.Transport.Msmq/FailureRateCircuitBreaker.cs @@ -15,21 +15,20 @@ public FailureRateCircuitBreaker(string name, int maximumFailuresPerSecond, Acti timer = new Timer(_ => FlushHistory(), null, TimeSpan.FromSeconds(0), TimeSpan.FromSeconds(30)); } - public void Dispose() - { - timer?.Dispose(); - } + public void Dispose() => timer.Dispose(); void FlushHistory() { - Interlocked.Exchange(ref failureCount, 0); - Logger.InfoFormat("The circuit breaker for {0} is now disarmed", name); + if (Interlocked.Exchange(ref failureCount, 0) > 0) + { + Logger.InfoFormat("The circuit breaker for {0} is now disarmed", name); + } } public void Failure(Exception lastException) { - var result = Interlocked.Increment(ref failureCount); - if (result > maximumFailuresPerThirtySeconds) + var failures = Interlocked.Increment(ref failureCount); + if (failures > maximumFailuresPerThirtySeconds) { _ = Task.Run(() => { @@ -37,7 +36,7 @@ public void Failure(Exception lastException) triggerAction(lastException); }); } - else if (result == 1) + else if (failures == 1) { Logger.WarnFormat("The circuit breaker for {0} is now in the armed state", name); } diff --git a/src/NServiceBus.Transport.Msmq/MsmqTransportInfrastructure.cs b/src/NServiceBus.Transport.Msmq/MsmqTransportInfrastructure.cs index 1fb08dd8..30d9bf2b 100644 --- a/src/NServiceBus.Transport.Msmq/MsmqTransportInfrastructure.cs +++ b/src/NServiceBus.Transport.Msmq/MsmqTransportInfrastructure.cs @@ -33,6 +33,7 @@ public override async Task Shutdown(CancellationToken cancellationToken = defaul if (delayedDeliveryPump != null) { await delayedDeliveryPump.Stop(cancellationToken).ConfigureAwait(false); + delayedDeliveryPump.Dispose(); } } diff --git a/src/NServiceBus.Transport.Msmq/RepeatedFailuresOverTimeCircuitBreaker.cs b/src/NServiceBus.Transport.Msmq/RepeatedFailuresOverTimeCircuitBreaker.cs index 113bd9e3..a33e3393 100644 --- a/src/NServiceBus.Transport.Msmq/RepeatedFailuresOverTimeCircuitBreaker.cs +++ b/src/NServiceBus.Transport.Msmq/RepeatedFailuresOverTimeCircuitBreaker.cs @@ -43,10 +43,7 @@ public Task Failure(Exception exception, CancellationToken cancellationToken = d return Task.Delay(TimeSpan.FromSeconds(1), CancellationToken.None); } - public void Dispose() - { - //Injected - } + public void Dispose() => timer.Dispose(); void CircuitBreakerTriggered(object state) {