Repository navigation
Expand file tree
/
Copy pathRetryAttribute.cs
More file actions
68 lines (62 loc) · 2.93 KB
/
Copy pathRetryAttribute.cs
File metadata and controls
68 lines (62 loc) · 2.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
namespace Mud.HttpUtils.Attributes;
/// <summary>
/// 为接口方法声明方法级重试策略。
/// </summary>
/// <remarks>
/// <para>
/// 默认仅幂等 HTTP 方法(GET/HEAD/OPTIONS/PUT/DELETE/TRACE)会实际重试;
/// POST/PATCH 等非幂等方法将静默跳过重试(保留超时与熔断),
/// 防止重复提交。需为非幂等方法启用重试时显式设置 <see cref="AllowNonIdempotent"/>。
/// </para>
/// <para>
/// <b>方法级覆盖面是「子集」,不是「整体替换」</b> ——
/// 本特性只覆盖 <see cref="MaxRetries"/>、<see cref="DelayMilliseconds"/>、
/// <see cref="UseExponentialBackoff"/> 三项;
/// <c>RetryStatusCodes</c>、<c>OnRetry</c>、<c>UseJitter</c> <b>恒取自全局</b>
/// <c>RetryOptions</c>(无法在方法级配置)。
/// 另:声明本特性的方法会<b>跳过全局重试策略</b>(方法级优先,只应用一次),
/// 详见 <c>Mud.HttpUtils.Resilience/README.md</c> 的「重试叠加关系()」。
/// </para>
/// <para>
/// <b>赋值口径(/ I-9)</b>:同一参数同时以位置参数与命名参数赋值时,
/// <b>命名参数优先</b>(还原 C# 特性赋值语义,命名参数在构造函数之后赋值)。
/// 例如 <c>[Retry(5, 250, DelayMilliseconds = 700)]</c> 的生效延迟是 700ms。
/// </para>
/// </remarks>
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
public sealed class RetryAttribute : Attribute
{
/// <summary>
/// 初始化 <see cref="RetryAttribute"/>,仅指定最大重试次数(延迟由全局配置决定)。
/// </summary>
/// <param name="maxRetries">最大重试次数(不含首次调用)。</param>
public RetryAttribute(int maxRetries = 3)
{
MaxRetries = maxRetries;
}
/// <summary>
/// 初始化 <see cref="RetryAttribute"/>,同时指定最大重试次数与重试延迟。
/// </summary>
/// <param name="maxRetries">最大重试次数。</param>
/// <param name="delayMilliseconds">重试延迟(毫秒)。</param>
public RetryAttribute(int maxRetries, int delayMilliseconds)
{
MaxRetries = maxRetries;
DelayMilliseconds = delayMilliseconds;
}
/// <summary>最大重试次数。</summary>
public int MaxRetries { get; set; }
/// <summary>重试延迟(毫秒)。</summary>
public int DelayMilliseconds { get; set; } = 1000;
/// <summary>是否使用指数退避策略。</summary>
public bool UseExponentialBackoff { get; set; } = true;
/// <summary>
/// 是否允许对非幂等 HTTP 方法(POST/PATCH 等)重试。默认 false。
/// </summary>
/// <remarks>
/// 为 true 时生成代码会向请求写入 <c>AllowNonIdempotentRetry</c> 标记,
/// 重试决策层(全局与方法级)据此放行重试。
/// 仅对确认服务端幂等(如带幂等键)的接口开启。
/// </remarks>
public bool AllowNonIdempotent { get; set; }
}