日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

ASP VNext 开源服务容错处理库Polly

發(fā)布時間:2023/12/4 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ASP VNext 开源服务容错处理库Polly 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

背景

Polly是一個.NET彈性和瞬態(tài)故障處理庫,允許開發(fā)人員以流暢和線程安全的方式表達(dá)諸如重試,斷路器,超時,隔離頭和回退之類的策略。

Polly面向.NET Standard 1.1(覆蓋范圍:.NET Core 1.0,Mono,Xamarin,UWP,WP8.1 +)和.NET Standard 2.0+(覆蓋范圍:.NET Core 2.0 + 、. NET Core 3.0和更高版本的Mono,Xamarin和UWP目標(biāo))。nuget軟件包還包括.NET Framework 4.6.1和4.7.2的直接目標(biāo)。

使用方式

通過NuGet安裝

Install-Package Polly

代碼實現(xiàn)

故障處理,響應(yīng)策略

故障處理策略處理您通過策略執(zhí)行的委托引發(fā)的特定異常或? ? ? ? ? 返回的結(jié)果。

? 步驟1:指定您希望策略處理的異常/錯誤

// Single exception type Policy.Handle<HttpRequestException>()// Single exception type with condition Policy.Handle<SqlException>(ex => ex.Number == 1205)// Multiple exception types Policy.Handle<HttpRequestException>().Or<OperationCanceledException>()// Multiple exception types with condition Policy.Handle<SqlException>(ex => ex.Number == 1205).Or<ArgumentException>(ex => ex.ParamName == "example")// Inner exceptions of ordinary exceptions or AggregateException, with or without conditions // (HandleInner matches exceptions at both the top-level and inner exceptions) Policy.HandleInner<HttpRequestException>().OrInner<OperationCanceledException>(ex => ex.CancellationToken != myToken)

步驟2:指定策略應(yīng)如何處理這些故障

2.1、重試

?

// Retry once Policy.Handle<SomeExceptionType>().Retry()// Retry multiple times Policy.Handle<SomeExceptionType>().Retry(3)// 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});// Retry multiple times, calling an action on each retry // with the current exception, retry count and context // provided to Execute() Policy.Handle<SomeExceptionType>().Retry(3, onRetry: (exception, retryCount, context) =>{// Add logic to be executed before each retry, such as logging });

2.2、永遠(yuǎn)重試(直到成功)

// Retry forever Policy.Handle<SomeExceptionType>().RetryForever()// Retry forever, calling an action on each retry with the // current exception Policy.Handle<SomeExceptionType>().RetryForever(onRetry: exception =>{// Add logic to be executed before each retry, such as logging });// Retry forever, calling an action on each retry with the // current exception and context provided to Execute() Policy.Handle<SomeExceptionType>().RetryForever(onRetry: (exception, context) =>{// Add logic to be executed before each retry, such as logging });

2.3、等待并重試

// Retry, waiting a specified duration between each retry. // (The wait is imposed on catching the failure, before making the next try.) Policy.Handle<SomeExceptionType>().WaitAndRetry(new[]{TimeSpan.FromSeconds(1),TimeSpan.FromSeconds(2),TimeSpan.FromSeconds(3)});// Retry, waiting a specified duration between each retry, // calling an action on each retry with the current exception // and duration Policy.Handle<SomeExceptionType>().WaitAndRetry(new[]{TimeSpan.FromSeconds(1),TimeSpan.FromSeconds(2),TimeSpan.FromSeconds(3)}, (exception, timeSpan) => {// Add logic to be executed before each retry, such as logging }); // Retry, waiting a specified duration between each retry, // calling an action on each retry with the current exception, // duration and context provided to Execute() Policy.Handle<SomeExceptionType>().WaitAndRetry(new[]{TimeSpan.FromSeconds(1),TimeSpan.FromSeconds(2),TimeSpan.FromSeconds(3)}, (exception, timeSpan, context) => {// Add logic to be executed before each retry, such as logging });// Retry, waiting a specified duration between each retry, // calling an action on each retry with the current exception, // duration, retry count, and context provided to Execute() Policy.Handle<SomeExceptionType>().WaitAndRetry(new[]{TimeSpan.FromSeconds(1),TimeSpan.FromSeconds(2),TimeSpan.FromSeconds(3)}, (exception, timeSpan, retryCount, context) => {// Add logic to be executed before each retry, such as logging });// Retry a specified number of times, using a function to // calculate the duration to wait between retries based on // the current retry attempt (allows for exponential backoff) // In this case will wait for // 2 ^ 1 = 2 seconds then // 2 ^ 2 = 4 seconds then // 2 ^ 3 = 8 seconds then // 2 ^ 4 = 16 seconds then // 2 ^ 5 = 32 seconds Policy.Handle<SomeExceptionType>().WaitAndRetry(5, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)) );// Retry a specified number of times, using a function to // calculate the duration to wait between retries based on // the current retry attempt, calling an action on each retry // with the current exception, duration and context provided // to Execute() Policy.Handle<SomeExceptionType>().WaitAndRetry(5, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)), (exception, timeSpan, context) => {// Add logic to be executed before each retry, such as logging});// Retry a specified number of times, using a function to // calculate the duration to wait between retries based on // the current retry attempt, calling an action on each retry // with the current exception, duration, retry count, and context // provided to Execute() Policy.Handle<SomeExceptionType>().WaitAndRetry(5, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)), (exception, timeSpan, retryCount, context) => {// Add logic to be executed before each retry, such as logging});

開源地址

https://github.com/App-vNext/Polly

總結(jié)

以上是生活随笔為你收集整理的ASP VNext 开源服务容错处理库Polly的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。