polly是乙個.net彈性和瞬態故障處理庫,允許開發人員以流暢和執行緒安全的方式表達諸如重試,斷路器,超時,隔離頭和回退之類的策略。
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目標)。nuget軟體包還包括.net framework 4.6.1和4.7.2的直接目標。
install-package polly
故障處理策略處理您通過策略執行的委託引發的特定異常或 返回的結果。
步驟1:指定您希望策略處理的異常/錯誤
// single exception type
policy
.handle()
// single exception type with condition
policy
.handle(ex => ex.number == 1205)
// multiple exception types
policy
.handle()
.or()
// multiple exception types with condition
policy
.handle(ex => ex.number == 1205)
.or(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()
.orinner(ex => ex.cancellationtoken != mytoken)
// retry once
policy
.handle()
.retry()
// retry multiple times
policy
.handle()
.retry(3)
// retry multiple times, calling an action on each retry
// with the current exception and retry count
policy
.handle()
.retry(3, onretry: (exception, retrycount) =>
);// retry multiple times, calling an action on each retry
// with the current exception, retry count and context
// provided to execute()
policy
.handle()
.retry(3, onretry: (exception, retrycount, context) =>
);
// retry forever
policy
.handle()
.retryforever()
// retry forever, calling an action on each retry with the
// current exception
policy
.handle()
.retryforever(onretry: exception =>
);// retry forever, calling an action on each retry with the
// current exception and context provided to execute()
policy
.handle()
.retryforever(onretry: (exception, context) =>
);
// retry, waiting a specified duration between each retry.
// (the wait is imposed on catching the failure, before ****** the next try.)
policy
.handle()
.waitandretry(new
);// retry, waiting a specified duration between each retry,
// calling an action on each retry with the current exception
// and duration
policy
.handle()
.waitandretry(new
, (exception, timespan) => );
// 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()
.waitandretry(new
, (exception, timespan, context) => );
// 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()
.waitandretry(new
, (exception, timespan, retrycount, context) => );
// 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()
.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()
.waitandretry(
5, retryattempt => timespan.fromseconds(math.pow(2, retryattempt)),
(exception, timespan, context) =>
);// 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()
.waitandretry(
5, retryattempt => timespan.fromseconds(math.pow(2, retryattempt)),
(exception, timespan, retrycount, context) =>
);
mysql 容錯處理 詞典及容錯處理
在前面幾篇文章中都是在講倒排索引的結構,及合併優化方法.這篇博文裡更多談下怎麼根據輸輸入查詢引數來定位到倒排記錄表的指標.其實這跟mysql中對varchar型別加索引後,然後基於該欄位查詢的原理一樣,都是可以基於b tree的經典資料結構來快速定位.mysql中基於記憶體表還可以用hash索引,同...
spring cloud實現微服務容錯處理
至此,已用eureka實現微服務的註冊發現,ribbon實現客戶端的負載均衡。本節頭論一下hystrix實現微服務的容錯 hystrix是乙個實現了超時機制和斷路器模式的工具類庫,在正常情況下,斷路器關閉,可正常請求依賴的服務。當一段時間內,請求失敗率達到一定閾值,斷路器就會開啟,此時,不會再去請求...
SparkRDD容錯處理
rdd不同的依賴關係 寬依賴 窄依賴 是spark rdd有不同的容錯處理方式 對spark框架層面容錯機制的三大層面 排程層 rdd血統層 checkpoint層 和spark rdd容錯四大核心要點。1 spark rdd容錯原理 rdd的不同依賴關係導致spark對不同的依賴關係有不同的處理方...