-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathTimeoutSyntax.cs
260 lines (231 loc) · 17.7 KB
/
TimeoutSyntax.cs
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
using System;
using System.Threading.Tasks;
using Polly.Timeout;
using Polly.Utilities;
namespace Polly
{
public partial class Policy
{
/// <summary>
/// Builds a <see cref="Policy"/> that will wait for a delegate to complete for a specified period of time. A <see cref="TimeoutRejectedException"/> will be thrown if the delegate does not complete within the configured timeout.
/// </summary>
/// <param name="seconds">The number of seconds after which to timeout.</param>
/// <exception cref="System.ArgumentOutOfRangeException">seconds;Value must be greater than zero.</exception>
/// <returns>The policy instance.</returns>
public static TimeoutPolicy Timeout(int seconds)
{
if (seconds <= 0) throw new ArgumentOutOfRangeException(nameof(seconds));
Action<Context, TimeSpan, Task> doNothing = (_, __, ___) => { };
return Timeout(ctx => TimeSpan.FromSeconds(seconds), TimeoutStrategy.Optimistic, doNothing);
}
/// <summary>
/// Builds a <see cref="Policy" /> that will wait for a delegate to complete for a specified period of time. A <see cref="TimeoutRejectedException" /> will be thrown if the delegate does not complete within the configured timeout.
/// </summary>
/// <param name="seconds">The number of seconds after which to timeout.</param>
/// <param name="timeoutStrategy">The timeout strategy.</param>
/// <returns>The policy instance.</returns>
/// <exception cref="System.ArgumentOutOfRangeException">seconds;Value must be greater than zero.</exception>
public static TimeoutPolicy Timeout(int seconds, TimeoutStrategy timeoutStrategy)
{
if (seconds <= 0) throw new ArgumentOutOfRangeException(nameof(seconds));
Action<Context, TimeSpan, Task> doNothing = (_, __, ___) => { };
return Timeout(ctx => TimeSpan.FromSeconds(seconds), timeoutStrategy, doNothing);
}
/// <summary>
/// Builds a <see cref="Policy"/> that will wait for a delegate to complete for a specified period of time. A <see cref="TimeoutRejectedException"/> will be thrown if the delegate does not complete within the configured timeout.
/// </summary>
/// <param name="seconds">The number of seconds after which to timeout.</param>
/// <param name="onTimeout">An action to call on timeout, passing the execution context, the timeout applied, and a <see cref="Task"/> capturing the abandoned, timed-out action.
/// <remarks>The Task parameter will be null if the executed action responded co-operatively to cancellation before the policy timed it out.</remarks></param>
/// <returns>The policy instance.</returns>
/// <exception cref="System.ArgumentOutOfRangeException">seconds;Value must be greater than zero.</exception>
/// <exception cref="System.ArgumentNullException">onTimeout</exception>
public static TimeoutPolicy Timeout(int seconds, Action<Context, TimeSpan, Task> onTimeout)
{
if (seconds <= 0) throw new ArgumentOutOfRangeException(nameof(seconds));
return Timeout(ctx => TimeSpan.FromSeconds(seconds), TimeoutStrategy.Optimistic, onTimeout);
}
/// <summary>
/// Builds a <see cref="Policy" /> that will wait for a delegate to complete for a specified period of time. A <see cref="TimeoutRejectedException" /> will be thrown if the delegate does not complete within the configured timeout.
/// </summary>
/// <param name="seconds">The number of seconds after which to timeout.</param>
/// <param name="timeoutStrategy">The timeout strategy.</param>
/// <param name="onTimeout">An action to call on timeout, passing the execution context, the timeout applied, and a <see cref="Task" /> capturing the abandoned, timed-out action.
/// <remarks>The Task parameter will be null if the executed action responded co-operatively to cancellation before the policy timed it out.</remarks></param>
/// <returns>The policy instance.</returns>
/// <exception cref="System.ArgumentOutOfRangeException">seconds;Value must be greater than zero.</exception>
/// <exception cref="System.ArgumentNullException">onTimeout</exception>
public static TimeoutPolicy Timeout(int seconds, TimeoutStrategy timeoutStrategy, Action<Context, TimeSpan, Task> onTimeout)
{
if (seconds <= 0) throw new ArgumentOutOfRangeException(nameof(seconds));
return Timeout(ctx => TimeSpan.FromSeconds(seconds), timeoutStrategy, onTimeout);
}
/// <summary>
/// Builds a <see cref="Policy"/> that will wait for a delegate to complete for a specified period of time. A <see cref="TimeoutRejectedException"/> will be thrown if the delegate does not complete within the configured timeout.
/// </summary>
/// <param name="timeout">The timeout.</param>
/// <returns>The policy instance.</returns>
public static TimeoutPolicy Timeout(TimeSpan timeout)
{
if (timeout <= TimeSpan.Zero) throw new ArgumentOutOfRangeException(nameof(timeout));
Action<Context, TimeSpan, Task> doNothing = (_, __, ___) => { };
return Timeout(ctx => timeout, TimeoutStrategy.Optimistic, doNothing);
}
/// <summary>
/// Builds a <see cref="Policy" /> that will wait for a delegate to complete for a specified period of time. A <see cref="TimeoutRejectedException" /> will be thrown if the delegate does not complete within the configured timeout.
/// </summary>
/// <param name="timeout">The timeout.</param>
/// <param name="timeoutStrategy">The timeout strategy.</param>
/// <returns>The policy instance.</returns>
/// <exception cref="System.ArgumentOutOfRangeException"></exception>
public static TimeoutPolicy Timeout(TimeSpan timeout, TimeoutStrategy timeoutStrategy)
{
if (timeout <= TimeSpan.Zero) throw new ArgumentOutOfRangeException(nameof(timeout));
Action<Context, TimeSpan, Task> doNothing = (_, __, ___) => { };
return Timeout(ctx => timeout, timeoutStrategy, doNothing);
}
/// <summary>
/// Builds a <see cref="Policy"/> that will wait for a delegate to complete for a specified period of time. A <see cref="TimeoutRejectedException"/> will be thrown if the delegate does not complete within the configured timeout.
/// </summary>
/// <param name="timeout">The timeout.</param>
/// <param name="onTimeout">An action to call on timeout, passing the execution context, the timeout applied, and a <see cref="Task"/> capturing the abandoned, timed-out action.
/// <remarks>The Task parameter will be null if the executed action responded co-operatively to cancellation before the policy timed it out.</remarks></param>
/// <returns>The policy instance.</returns>
/// <exception cref="System.ArgumentOutOfRangeException">timeout;Value must be greater than zero.</exception>
/// <exception cref="System.ArgumentNullException">onTimeout</exception>
public static TimeoutPolicy Timeout(TimeSpan timeout, Action<Context, TimeSpan, Task> onTimeout)
{
if (timeout <= TimeSpan.Zero) throw new ArgumentOutOfRangeException(nameof(timeout));
return Timeout(ctx => timeout, TimeoutStrategy.Optimistic, onTimeout);
}
/// <summary>
/// Builds a <see cref="Policy" /> that will wait for a delegate to complete for a specified period of time. A <see cref="TimeoutRejectedException" /> will be thrown if the delegate does not complete within the configured timeout.
/// </summary>
/// <param name="timeout">The timeout.</param>
/// <param name="timeoutStrategy">The timeout strategy.</param>
/// <param name="onTimeout">An action to call on timeout, passing the execution context, the timeout applied, and a <see cref="Task" /> capturing the abandoned, timed-out action.
/// <remarks>The Task parameter will be null if the executed action responded co-operatively to cancellation before the policy timed it out.</remarks></param>
/// <returns>The policy instance.</returns>
/// <exception cref="System.ArgumentOutOfRangeException">timeout;Value must be greater than zero.</exception>
/// <exception cref="System.ArgumentNullException">onTimeout</exception>
public static TimeoutPolicy Timeout(TimeSpan timeout, TimeoutStrategy timeoutStrategy, Action<Context, TimeSpan, Task> onTimeout)
{
if (timeout <= TimeSpan.Zero) throw new ArgumentOutOfRangeException(nameof(timeout));
return Timeout(ctx => timeout, timeoutStrategy, onTimeout);
}
/// <summary>
/// Builds a <see cref="Policy"/> that will wait for a delegate to complete for a specified period of time. A <see cref="TimeoutRejectedException"/> will be thrown if the delegate does not complete within the configured timeout.
/// </summary>
/// <param name="timeoutProvider">A function to provide the timeout for this execution.</param>
/// <exception cref="System.ArgumentNullException">timeoutProvider</exception>
/// <returns>The policy instance.</returns>
public static TimeoutPolicy Timeout(Func<TimeSpan> timeoutProvider)
{
if (timeoutProvider == null) throw new ArgumentNullException(nameof(timeoutProvider));
Action<Context, TimeSpan, Task> doNothing = (_, __, ___) => { };
return Timeout(ctx => timeoutProvider(), TimeoutStrategy.Optimistic, doNothing);
}
/// <summary>
/// Builds a <see cref="Policy" /> that will wait for a delegate to complete for a specified period of time. A <see cref="TimeoutRejectedException" /> will be thrown if the delegate does not complete within the configured timeout.
/// </summary>
/// <param name="timeoutProvider">A function to provide the timeout for this execution.</param>
/// <param name="timeoutStrategy">The timeout strategy.</param>
/// <returns>The policy instance.</returns>
/// <exception cref="System.ArgumentNullException">timeoutProvider</exception>
public static TimeoutPolicy Timeout(Func<TimeSpan> timeoutProvider, TimeoutStrategy timeoutStrategy)
{
if (timeoutProvider == null) throw new ArgumentNullException(nameof(timeoutProvider));
Action<Context, TimeSpan, Task> doNothing = (_, __, ___) => { };
return Timeout(ctx => timeoutProvider(), timeoutStrategy, doNothing);
}
/// <summary>
/// Builds a <see cref="Policy" /> that will wait for a delegate to complete for a specified period of time. A <see cref="TimeoutRejectedException" /> will be thrown if the delegate does not complete within the configured timeout.
/// </summary>
/// <param name="timeoutProvider">A function to provide the timeout for this execution.</param>
/// <param name="onTimeout">An action to call on timeout, passing the execution context, the timeout applied, and a <see cref="Task" /> capturing the abandoned, timed-out action.
/// <remarks>The Task parameter will be null if the executed action responded co-operatively to cancellation before the policy timed it out.</remarks></param>
/// <returns>The policy instance.</returns>
/// <exception cref="System.ArgumentNullException">timeoutProvider</exception>
/// <exception cref="System.ArgumentNullException">onTimeout</exception>
public static TimeoutPolicy Timeout(Func<TimeSpan> timeoutProvider, Action<Context, TimeSpan, Task> onTimeout)
{
if (timeoutProvider == null) throw new ArgumentNullException(nameof(timeoutProvider));
return Timeout(ctx => timeoutProvider(), TimeoutStrategy.Optimistic, onTimeout);
}
/// <summary>
/// Builds a <see cref="Policy" /> that will wait for a delegate to complete for a specified period of time. A <see cref="TimeoutRejectedException" /> will be thrown if the delegate does not complete within the configured timeout.
/// </summary>
/// <param name="timeoutProvider">A function to provide the timeout for this execution.</param>
/// <param name="timeoutStrategy">The timeout strategy.</param>
/// <param name="onTimeout">An action to call on timeout, passing the execution context, the timeout applied, and a <see cref="Task" /> capturing the abandoned, timed-out action.
/// <remarks>The Task parameter will be null if the executed action responded co-operatively to cancellation before the policy timed it out.</remarks></param>
/// <returns>The policy instance.</returns>
/// <exception cref="System.ArgumentNullException">timeoutProvider</exception>
/// <exception cref="System.ArgumentNullException">onTimeout</exception>
public static TimeoutPolicy Timeout(Func<TimeSpan> timeoutProvider, TimeoutStrategy timeoutStrategy, Action<Context, TimeSpan, Task> onTimeout)
{
if (timeoutProvider == null) throw new ArgumentNullException(nameof(timeoutProvider));
return Timeout(ctx => timeoutProvider(), timeoutStrategy, onTimeout);
}
/// <summary>
/// Builds a <see cref="Policy"/> that will wait for a delegate to complete for a specified period of time. A <see cref="TimeoutRejectedException"/> will be thrown if the delegate does not complete within the configured timeout.
/// </summary>
/// <param name="timeoutProvider">A function to provide the timeout for this execution.</param>
/// <exception cref="System.ArgumentNullException">timeoutProvider</exception>
/// <returns>The policy instance.</returns>
public static TimeoutPolicy Timeout(Func<Context, TimeSpan> timeoutProvider)
{
Action<Context, TimeSpan, Task> doNothing = (_, __, ___) => { };
return Timeout(timeoutProvider, TimeoutStrategy.Optimistic, doNothing);
}
/// <summary>
/// Builds a <see cref="Policy" /> that will wait for a delegate to complete for a specified period of time. A <see cref="TimeoutRejectedException" /> will be thrown if the delegate does not complete within the configured timeout.
/// </summary>
/// <param name="timeoutProvider">A function to provide the timeout for this execution.</param>
/// <param name="timeoutStrategy">The timeout strategy.</param>
/// <returns>The policy instance.</returns>
/// <exception cref="System.ArgumentNullException">timeoutProvider</exception>
public static TimeoutPolicy Timeout(Func<Context, TimeSpan> timeoutProvider, TimeoutStrategy timeoutStrategy)
{
Action<Context, TimeSpan, Task> doNothing = (_, __, ___) => { };
return Timeout(timeoutProvider, timeoutStrategy, doNothing);
}
/// <summary>
/// Builds a <see cref="Policy" /> that will wait for a delegate to complete for a specified period of time. A <see cref="TimeoutRejectedException" /> will be thrown if the delegate does not complete within the configured timeout.
/// </summary>
/// <param name="timeoutProvider">A function to provide the timeout for this execution.</param>
/// <param name="onTimeout">An action to call on timeout, passing the execution context, the timeout applied, and a <see cref="Task" /> capturing the abandoned, timed-out action.
/// <remarks>The Task parameter will be null if the executed action responded co-operatively to cancellation before the policy timed it out.</remarks></param>
/// <returns>The policy instance.</returns>
/// <exception cref="System.ArgumentNullException">timeoutProvider</exception>
/// <exception cref="System.ArgumentNullException">onTimeout</exception>
public static TimeoutPolicy Timeout(Func<Context, TimeSpan> timeoutProvider, Action<Context, TimeSpan, Task> onTimeout)
{
return Timeout(timeoutProvider, TimeoutStrategy.Optimistic, onTimeout);
}
/// <summary>
/// Builds a <see cref="Policy" /> that will wait for a delegate to complete for a specified period of time. A <see cref="TimeoutRejectedException" /> will be thrown if the delegate does not complete within the configured timeout.
/// </summary>
/// <param name="timeoutProvider">A function to provide the timeout for this execution.</param>
/// <param name="timeoutStrategy">The timeout strategy.</param>
/// <param name="onTimeout">An action to call on timeout, passing the execution context, the timeout applied, and a <see cref="Task" /> capturing the abandoned, timed-out action.
/// <remarks>The Task parameter will be null if the executed action responded co-operatively to cancellation before the policy timed it out.</remarks></param>
/// <returns>The policy instance.</returns>
/// <exception cref="System.ArgumentNullException">timeoutProvider</exception>
/// <exception cref="System.ArgumentNullException">onTimeout</exception>
public static TimeoutPolicy Timeout(Func<Context, TimeSpan> timeoutProvider, TimeoutStrategy timeoutStrategy, Action<Context, TimeSpan, Task> onTimeout)
{
if (timeoutProvider == null) throw new ArgumentNullException(nameof(timeoutProvider));
if (onTimeout == null) throw new ArgumentNullException(nameof(onTimeout));
return new TimeoutPolicy(
(action, context, cancellationToken) => TimeoutEngine.Implementation(
(ctx, ct) => { action(ctx, ct); return EmptyStruct.Instance; },
context,
cancellationToken,
timeoutProvider,
timeoutStrategy,
onTimeout)
);
}
}
}