Polly是一個C#實現的彈性瞬時錯誤處理庫
它能夠幫助咱們作一些容錯模式處理,好比:html
使用也是很是簡單的,好比:git
// Retry multiple times, calling an action on each retry // with the current exception and retry count Policy .Handle<SomeExceptionType>() .Retry(3, onRetry: (exception, retryCount) => { // Add logic to be executed before each retry, such as logging });
可是每一個地方咱們都得這樣寫,我的仍是不喜,
那麼怎麼簡化呢?
固然是使用 Norns.Urd 這些AOP框架封裝咱們經常使用的東西作成 Attribute
啦github
咱們來嘗試將 Retry功能 作成 RetryAttribute
吧框架
dotnet add package Norns.Urd
public class RetryAttribute : AbstractInterceptorAttribute { private readonly int retryCount; public RetryAttribute(int retryCount) { this.retryCount = retryCount; } public override async Task InvokeAsync(AspectContext context, AsyncAspectDelegate next) { await Policy.Handle<Exception>() .RetryAsync(retryCount) .ExecuteAsync(() => next(context)); } }
public class RetryAttribute : AbstractInterceptorAttribute { private readonly int retryCount; public RetryAttribute(int retryCount) { this.retryCount = retryCount; } public override void Invoke(AspectContext context, AspectDelegate next) { Policy.Handle<Exception>() .Retry(retryCount) .Execute(() => next(context)); } public override async Task InvokeAsync(AspectContext context, AsyncAspectDelegate next) { await Policy.Handle<Exception>() .RetryAsync(retryCount) .ExecuteAsync(() => next(context)); } }
public class RetryTest { public class DoRetryTest { public int Count { get; set; } [Retry(2)] // 使用 Retry public virtual void Do() { if (Count < 50) { Count++; // 每調用一次就加1 throw new FieldAccessException(); } } } public DoRetryTest Mock() { return new ServiceCollection() .AddTransient<DoRetryTest>() .ConfigureAop() .BuildServiceProvider() .GetRequiredService<DoRetryTest>(); } [Fact] public void RetryWhenSync() { var sut = Mock(); Assert.Throws<FieldAccessException>(() => sut.Do()); Assert.Equal(3, sut.Count); //咱們指望調用總共 3 次 } }
是的,就是這樣,咱們能夠在任何地方使用 RetryAttribute
async
Norns.Urd.Extensions.Polly
這裏經過Norns.Urd將Polly的各類功能集成爲更加方便使用的功能ide
EnablePolly()
如:測試
new ServiceCollection() .AddTransient<DoTimeoutTest>() .ConfigureAop(i => i.EnablePolly())
[Timeout(seconds: 1)] // timeout 1 seconds, when timeout will throw TimeoutRejectedException double Wait(double seconds); [Timeout(timeSpan: "00:00:00.100")] // timeout 100 milliseconds, only work on async method when no CancellationToken async Task<double> WaitAsync(double seconds, CancellationToken cancellationToken = default); [Timeout(timeSpan: "00:00:01")] // timeout 1 seconds, but no work on async method when no CancellationToken async Task<double> NoCancellationTokenWaitAsync(double seconds);
[Retry(retryCount: 2, ExceptionType = typeof(AccessViolationException))] // retry 2 times when if throw Exception void Do()
[CircuitBreaker(exceptionsAllowedBeforeBreaking: 3, durationOfBreak: "00:00:01")] //or [AdvancedCircuitBreaker(failureThreshold: 0.1, samplingDuration: "00:00:01", minimumThroughput: 3, durationOfBreak: "00:00:01")] void Do()
[Bulkhead(maxParallelization: 5, maxQueuingActions: 10)] void Do()
有關 Norns.Urd, 你們能夠查看 https://fs7744.github.io/Norns.Urd/zh-cn/index.htmlui