Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
PolicyRunner.cs
1using System;
2using System.Linq;
3using System.Threading;
4using System.Threading.Tasks;
6
8{
13 public static class PolicyRunner
14 {
15 public static Task RunAsync(
16 Func<CancellationToken, Task> action,
17 CancellationToken ct,
18 params IAsyncPolicy[] policies)
19 {
20 if (action is null) throw new ArgumentNullException(nameof(action));
21 Func<CancellationToken, Task> pipeline = action;
22 if (policies is { Length: > 0 })
23 {
24 policies.Reverse();
25
26 foreach (IAsyncPolicy pol in policies)
27 {
28 var next = pipeline;
29 pipeline = c => pol.ExecuteAsync(next, c);
30 }
31 }
32 return pipeline(ct);
33 }
34
35 public static async Task<T> RunAsync<T>(
36 Func<CancellationToken, Task<T>> action,
37 CancellationToken ct,
38 params IAsyncPolicy[] policies)
39 {
40 if (action is null) throw new ArgumentNullException(nameof(action));
41 // Build a non-generic pipeline by capturing the result.
42 T? result = default;
43 await RunAsync(async c => { result = await action(c).ConfigureAwait(false); }, ct, policies).ConfigureAwait(false);
44 return result!;
45 }
46 }
47}
48
Executes async operations through a pipeline of policies (timeout, retry, bulkhead,...
Definition: PolicyRunner.cs:14