Created
August 22, 2021 20:54
-
-
Save Qrtic/9f17092bd4815454a130c72453bdd1d4 to your computer and use it in GitHub Desktop.
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
using Polly; | |
using Polly.CircuitBreaker; | |
public class CircuitBreakerHandler | |
{ | |
private readonly IEmailService _emailService; | |
private readonly ILogger _logger; | |
private CircuitBreakerPolicy _circuitBreaker; | |
public CircuitBreakerHandler(IEmailService emailService, ILogger logger) | |
{ | |
_emailService = emailService; | |
_logger = logger; | |
_circuitBreaker = Policy | |
.Handle<Exception>() | |
.CircuitBreaker( | |
exceptionsAllowedBeforeBreaking: 1, | |
durationOfBreak: TimeSpan.FromSeconds(1) | |
); | |
} | |
public void HandleNotification(Notification notification) | |
{ | |
_circuitBreaker.Execute(() => | |
{ | |
try | |
{ | |
_emailService.SendMessage(notification.Message); | |
} | |
catch (Exception e) | |
{ | |
_logger.Log(e); | |
throw; | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment