Ejemplo n.º 1
0
        /// <summary>
        /// Configures circuit breaker.
        /// </summary>
        private void ConfigureCircuitBreaker()
        {
            void OnBreak(Exception exception, TimeSpan timespan)
            {
                //recreate secondary sender
                if (SecondaryClient == null)
                {
                    SecondaryClient = CreateClient(_secondaryServiceBusConnectionString);
                }

                //kill primary sender
                PrimaryClient.CloseAsync().ConfigureAwait(false).GetAwaiter().GetResult();
                PrimaryClient = null;

                _logger?.LogWarning(exception, "Switched from primary to secondary service bus sender");
            }

            void OnHalfOpen()
            {
                //recreate primary sender
                if (PrimaryClient == null)
                {
                    PrimaryClient = CreateClient(_primaryServiceBusConnectionString);
                }

                //kill secondary
                SecondaryClient.CloseAsync().ConfigureAwait(false).GetAwaiter().GetResult();
                SecondaryClient = null;

                _logger?.LogWarning("Switched back from secondary to primary service bus sender");
            }

            void OnReset()
            {
                _logger?.LogInformation("Circuit breaker was reset.");
            }

            _circuitBreaker = Policy
                              .Handle <Exception>()
                              .CircuitBreakerAsync(ExceptionsAllowedBeforeBreaking, TimeSpan.FromMilliseconds(TimeOutIfBreaksInMilliseconds), OnBreak, OnReset, OnHalfOpen);
        }
Ejemplo n.º 2
0
 /// <summary>
 /// Cleans up (un)managed resources.
 /// </summary>
 /// <param name="disposing"></param>
 protected virtual void Dispose(bool disposing)
 {
     PrimaryClient?.CloseAsync().ConfigureAwait(false).GetAwaiter().GetResult();
     SecondaryClient?.CloseAsync().ConfigureAwait(false).GetAwaiter().GetResult();
 }