在 C# 开发中,选对工具能让效率提升 3 倍不止。这篇文章专门介绍 Polly 和 容错,都是我踩过很多坑之后筛选出来的「真香」工具。
工具 1:Polly
一句话评价:用 Polly 为 HTTP 调用添加 CircuitBreaker/Timeout/Retry 策略
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract DataStore {
mapping(address => uint[]) public userData;
function add(uint value) external {
require(value > 0, "Value must be positive");
userData[msg.sender].push(value);
}
function getSum(address user) external view returns(uint) {
uint sum = 0;
for(uint i=0; i<userData[user].length; i++) sum += userData[user][i];
return sum;
}
}
工具 2:容错
一句话评价:解决 用 Polly 为 HTTP 调用添加 CircuitBreaker/Timeout/Retry 策略 的瑞士军刀。
# Polly 进阶配置
timeout: 30s
retries: 3
cache: true
功能对比
| 功能 | Polly | 传统方案 |
|---|---|---|
| 易用性 | 5星 | 2星 |
| 性能 | 4星 | 3星 |
| 社区支持 | 活跃 | 一般 |
选对工具事半功倍。以上两个工具组合使用,基本能覆盖 C# 开发中 Polly 和 容错 的全部场景。