-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathRetryConfiguration.cs
More file actions
128 lines (112 loc) · 4.72 KB
/
RetryConfiguration.cs
File metadata and controls
128 lines (112 loc) · 4.72 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
using System;
using System.Net;
using Contentstack.Management.Core;
namespace Contentstack.Management.Core.Runtime.Pipeline.RetryHandler
{
/// <summary>
/// Configuration for retry behavior, supporting both network and HTTP error retries.
/// </summary>
public class RetryConfiguration
{
/// <summary>
/// When set to true, the client will retry requests.
/// When set to false, the client will not retry request.
/// The default value is true.
/// </summary>
public bool RetryOnError { get; set; } = true;
/// <summary>
/// Returns the flag indicating how many retry HTTP requests an SDK should
/// make for a single SDK operation invocation before giving up.
/// The default value is 5.
/// </summary>
public int RetryLimit { get; set; } = 5;
/// <summary>
/// Returns the flag indicating delay in retrying HTTP requests.
/// The default value is 300ms.
/// </summary>
public TimeSpan RetryDelay { get; set; } = TimeSpan.FromMilliseconds(300);
/// <summary>
/// When set to true, the client will retry on network failures.
/// The default value is true.
/// </summary>
public bool RetryOnNetworkFailure { get; set; } = true;
/// <summary>
/// When set to true, the client will retry on DNS failures.
/// The default value is true.
/// </summary>
public bool RetryOnDnsFailure { get; set; } = true;
/// <summary>
/// When set to true, the client will retry on socket failures.
/// The default value is true.
/// </summary>
public bool RetryOnSocketFailure { get; set; } = true;
/// <summary>
/// When set to true, the client will retry on HTTP server errors (5xx).
/// The default value is true.
/// </summary>
public bool RetryOnHttpServerError { get; set; } = true;
/// <summary>
/// Maximum number of network retry attempts.
/// The default value is 3.
/// </summary>
public int MaxNetworkRetries { get; set; } = 3;
/// <summary>
/// Base delay for network retries.
/// The default value is 100ms.
/// </summary>
public TimeSpan NetworkRetryDelay { get; set; } = TimeSpan.FromMilliseconds(100);
/// <summary>
/// Backoff strategy for network retries.
/// The default value is Exponential.
/// </summary>
public BackoffStrategy NetworkBackoffStrategy { get; set; } = BackoffStrategy.Exponential;
/// <summary>
/// Custom function to determine if a status code should be retried.
/// If null, default retry condition is used (429, 500, 502, 503, 504).
/// </summary>
public Func<HttpStatusCode, bool>? RetryCondition { get; set; }
/// <summary>
/// Options for retry delay calculation.
/// </summary>
public RetryDelayOptions RetryDelayOptions { get; set; } = new RetryDelayOptions();
/// <summary>
/// Creates a RetryConfiguration from ContentstackClientOptions.
/// </summary>
public static RetryConfiguration FromOptions(ContentstackClientOptions options)
{
return new RetryConfiguration
{
RetryOnError = options.RetryOnError,
RetryLimit = options.RetryLimit,
RetryDelay = options.RetryDelay,
RetryOnNetworkFailure = options.RetryOnNetworkFailure,
RetryOnDnsFailure = options.RetryOnDnsFailure,
RetryOnSocketFailure = options.RetryOnSocketFailure,
RetryOnHttpServerError = options.RetryOnHttpServerError,
MaxNetworkRetries = options.MaxNetworkRetries,
NetworkRetryDelay = options.NetworkRetryDelay,
NetworkBackoffStrategy = options.NetworkBackoffStrategy,
RetryCondition = options.RetryCondition,
RetryDelayOptions = options.RetryDelayOptions ?? new RetryDelayOptions
{
Base = options.RetryDelay
}
};
}
}
/// <summary>
/// Options for retry delay calculation.
/// </summary>
public class RetryDelayOptions
{
/// <summary>
/// Base delay for retries.
/// </summary>
public TimeSpan Base { get; set; } = TimeSpan.FromMilliseconds(300);
/// <summary>
/// Custom backoff function. Parameters: retryCount, exception.
/// Return TimeSpan.Zero or negative TimeSpan to disable retry for that attempt.
/// </summary>
public Func<int, Exception?, TimeSpan>? CustomBackoff { get; set; }
}
}