VassasCo.Utility.RetryHelper
1.0.0
dotnet add package VassasCo.Utility.RetryHelper --version 1.0.0
NuGet\Install-Package VassasCo.Utility.RetryHelper -Version 1.0.0
<PackageReference Include="VassasCo.Utility.RetryHelper" Version="1.0.0" />
<PackageVersion Include="VassasCo.Utility.RetryHelper" Version="1.0.0" />
<PackageReference Include="VassasCo.Utility.RetryHelper" />
paket add VassasCo.Utility.RetryHelper --version 1.0.0
#r "nuget: VassasCo.Utility.RetryHelper, 1.0.0"
#:package VassasCo.Utility.RetryHelper@1.0.0
#addin nuget:?package=VassasCo.Utility.RetryHelper&version=1.0.0
#tool nuget:?package=VassasCo.Utility.RetryHelper&version=1.0.0
RetryHelper
智能重试器 — 支持退避策略(固定/线性/指数)+ 抖动 + 断路器 + 降级 + 超时。
核心功能
- 退避策略:固定间隔 / 线性增长 / 指数退避
- 抖动类型:Full(全抖动)/ Equal(等量)/ Decorrelated(去相关)/ None(无)
- 断路器:Closed → Open → HalfOpen 三态自动切换
- 超时控制:单次执行超时 + 总超时
- 降级 Fallback:重试耗尽后执行备选逻辑
- 重试回调:每次失败后可执行自定义逻辑
- 同步 + 异步:完整的同步/异步 API,支持 CancellationToken
- 扩展方法:
Func<T>.Retry()/Func<T>.RetryWith(policy)等链式调用
快速开始
基础用法
// 使用默认策略(3次,指数退避 1s→30s,全抖动)
var result = RetryPolicy.Default.Execute(() => CallRemoteService());
// 自定义策略
var policy = RetryPolicy
.Build()
.WithMaxRetries(5)
.WithExponentialBackoff(TimeSpan.FromSeconds(1), TimeSpan.FromMinutes(1))
.WithJitter(JitterType.Full)
.When<HttpRequestException>()
.When<TimeoutException>()
.Build();
var result = policy.Execute(() => CallRemoteService());
异步使用
var result = await policy.ExecuteAsync(async ct =>
{
return await httpClient.GetAsync(url, ct);
});
断路器
var policy = RetryPolicy
.Build()
.WithCircuitBreaker(failures: 3, breakDuration: TimeSpan.FromSeconds(30))
.WithOperationKey("PaymentService")
.OnCircuitStateChanged((s, e) =>
{
Console.WriteLine($"断路器: {e.OldState} → {e.NewState}");
})
.Build();
降级 Fallback
var result = policy.ExecuteWithFallback(
() => CallPrimaryService(),
report => CallBackupService()
);
扩展方法
// 使用默认策略重试
var result = (() => CallService()).Retry();
// 使用自定义策略
var result = (() => CallService()).RetryWith(policy);
// 异步
var result = await (() => CallServiceAsync()).RetryAsync();
Builder 配置选项
| 方法 | 说明 |
|---|---|
WithMaxRetries(n) |
设置最大重试次数(含首次执行),默认 3 |
WithFixedBackoff(interval) |
固定间隔退避 |
WithLinearBackoff(base, max) |
线性增长退避 |
WithExponentialBackoff(base, max) |
指数退避(默认) |
WithJitter(type) |
设置抖动类型,默认 Full |
WithCircuitBreaker(failures, duration) |
配置断路器 |
WithOperationKey(key) |
设置操作标识键 |
OnCircuitStateChanged(handler) |
订阅断路器状态变更 |
When<TException>() |
仅对指定异常类型重试 |
When(predicate) |
自定义重试判断条件 |
WithPerAttemptTimeout(timeout) |
单次执行超时 |
WithTotalTimeout(timeout) |
总重试超时 |
OnRetry(callback) |
重试回调 |
类型
| 类型 | 说明 |
|---|---|
RetryPolicy |
重试策略,不可变,线程安全 |
RetryPolicyBuilder |
Builder,链式配置 |
CircuitBreaker |
断路器,三态切换 |
RetryContext |
单次尝试上下文 |
RetryReport |
重试最终报告 |
RetryExhaustedException |
重试耗尽异常(含报告) |
CircuitBrokenException |
断路器熔断异常 |
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
| .NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
| .NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen40 was computed. tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- No dependencies.
-
net6.0
- No dependencies.
-
net8.0
- No dependencies.
NuGet packages (1)
Showing the top 1 NuGet packages that depend on VassasCo.Utility.RetryHelper:
| Package | Downloads |
|---|---|
|
VassasCo.Utility
C# 桌面开发工具库 (WinForm / WPF / Avalonia): - ConfigHelper:零代码实体类⇋JSON/XML配置双向映射(带注释、热重载、原子保存、列表展开) - LogHelper:异步高性能日志系统(建造者模式、异步队列、自动清理、14种日志类型) - CrashDumpHelper:崩溃捕获+FirstChance异常追踪+MiniDump生成 - SnowflakeIdHelper:分布式雪花ID生成器(单调时钟杜绝回拨、集群WorkerId分配、ID反解) - ScheduleHelper:全能定时任务调度器(CRON/固定速率/固定延迟、重试退避、超时取消、日历过滤、线程池、优雅关闭) - RetryHelper:智能重试器(指数退避+抖动+断路器三态+降级+超时,同步/异步) - EventBus:进程内事件总线(类型发布/订阅、特性自动注册、优先级、条件过滤、粘性事件、多通道),支持 .NET Standard 2.0 / .NET 6 / .NET 8 / .NET 9 / .NET 10 - ExcelMapper:原生对象→Excel映射(嵌套类子表头合并、数组独立Sheet、Dictionary自适应、全可配置样式) |
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 1.0.0 | 270 | 6/22/2026 |