-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathOcelotBuilderExtensions.cs
114 lines (105 loc) · 6.39 KB
/
OcelotBuilderExtensions.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
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Ocelot.Configuration;
using Ocelot.DependencyInjection;
using Ocelot.Errors;
using Ocelot.Errors.QoS;
using Ocelot.Logging;
using Ocelot.Provider.Polly.Interfaces;
using Ocelot.Requester;
using Polly.CircuitBreaker;
using Polly.Registry;
using Polly.Timeout;
namespace Ocelot.Provider.Polly;
public static class OcelotBuilderExtensions
{
/// <summary>
/// Default mapping of Polly <see cref="Exception"/>s to <see cref="Error"/> objects.
/// </summary>
public static readonly IDictionary<Type, Func<Exception, Error>> DefaultErrorMapping = new Dictionary<Type, Func<Exception, Error>>
{
{typeof(TaskCanceledException), CreateRequestTimedOutError},
{typeof(TimeoutRejectedException), CreateRequestTimedOutError},
{typeof(BrokenCircuitException), CreateRequestTimedOutError},
{typeof(BrokenCircuitException<HttpResponseMessage>), CreateRequestTimedOutError},
};
private static Error CreateRequestTimedOutError(Exception e) => new RequestTimedOutError(e);
/// <summary>
/// Adds Polly QoS provider to Ocelot by custom delegate and with custom error mapping.
/// </summary>
/// <typeparam name="TProvider">QoS provider to use (by default use <see cref="PollyQoSResiliencePipelineProvider"/>).</typeparam>
/// <param name="builder">Ocelot builder to extend.</param>
/// <param name="delegatingHandler">Your customized delegating handler (to manage QoS behavior by yourself).</param>
/// <param name="errorMapping">Your customized error mapping.</param>
/// <returns>The reference to the same extended <see cref="IOcelotBuilder"/> object.</returns>
public static IOcelotBuilder AddPolly<TProvider>(this IOcelotBuilder builder, QosDelegatingHandlerDelegate delegatingHandler, IDictionary<Type, Func<Exception, Error>> errorMapping)
where TProvider : class, IPollyQoSResiliencePipelineProvider<HttpResponseMessage>
{
builder.Services
.AddSingleton<ResiliencePipelineRegistry<OcelotResiliencePipelineKey>>()
.AddSingleton(errorMapping) // Dictionary<TKey, TValue> injection used in HttpExceptionToErrorMapper
.AddSingleton<IPollyQoSResiliencePipelineProvider<HttpResponseMessage>, TProvider>()
.AddSingleton(delegatingHandler);
return builder;
}
/// <summary>
/// Adds Polly QoS provider to Ocelot with custom error mapping, but default <see cref="DelegatingHandler"/> is used.
/// </summary>
/// <typeparam name="TProvider">QoS provider to use (by default use <see cref="PollyQoSResiliencePipelineProvider"/>).</typeparam>
/// <param name="builder">Ocelot builder to extend.</param>
/// <param name="errorMapping">Your customized error mapping.</param>
/// <returns>The reference to the same extended <see cref="IOcelotBuilder"/> object.</returns>
public static IOcelotBuilder AddPolly<TProvider>(this IOcelotBuilder builder, IDictionary<Type, Func<Exception, Error>> errorMapping)
where TProvider : class, IPollyQoSResiliencePipelineProvider<HttpResponseMessage>
=> AddPolly<TProvider>(builder, GetDelegatingHandler, errorMapping);
/// <summary>
/// Adds Polly QoS provider to Ocelot with custom <see cref="DelegatingHandler"/> delegate, but default error mapping is used.
/// </summary>
/// <typeparam name="TProvider">QoS provider to use (by default use <see cref="PollyQoSResiliencePipelineProvider"/>).</typeparam>
/// <param name="builder">Ocelot builder to extend.</param>
/// <param name="delegatingHandler">Your customized delegating handler (to manage QoS behavior by yourself).</param>
/// <returns>The reference to the same extended <see cref="IOcelotBuilder"/> object.</returns>
public static IOcelotBuilder AddPolly<TProvider>(this IOcelotBuilder builder, QosDelegatingHandlerDelegate delegatingHandler)
where TProvider : class, IPollyQoSResiliencePipelineProvider<HttpResponseMessage>
=> AddPolly<TProvider>(builder, delegatingHandler, DefaultErrorMapping);
/// <summary>
/// Adds Polly QoS provider to Ocelot by defaults.
/// </summary>
/// <remarks>
/// Defaults:
/// <list type="bullet">
/// <item><see cref="GetDelegatingHandler"/></item>
/// <item><see cref="DefaultErrorMapping"/></item>
/// </list>
/// </remarks>
/// <typeparam name="TProvider">QoS provider to use (by default use <see cref="PollyQoSResiliencePipelineProvider"/>).</typeparam>
/// <param name="builder">Ocelot builder to extend.</param>
/// <returns>The reference to the same extended <see cref="IOcelotBuilder"/> object.</returns>
public static IOcelotBuilder AddPolly<TProvider>(this IOcelotBuilder builder)
where TProvider : class, IPollyQoSResiliencePipelineProvider<HttpResponseMessage>
=> AddPolly<TProvider>(builder, GetDelegatingHandler, DefaultErrorMapping);
/// <summary>
/// Adds Polly QoS provider to Ocelot by defaults with default QoS provider.
/// </summary>
/// <remarks>
/// Defaults:
/// <list type="bullet">
/// <item><see cref="PollyQoSResiliencePipelineProvider"/></item>
/// <item><see cref="GetDelegatingHandler"/></item>
/// <item><see cref="DefaultErrorMapping"/></item>
/// </list>
/// </remarks>
/// <param name="builder">Ocelot builder to extend.</param>
/// <returns>The reference to the same extended <see cref="IOcelotBuilder"/> object.</returns>
public static IOcelotBuilder AddPolly(this IOcelotBuilder builder)
=> AddPolly<PollyQoSResiliencePipelineProvider>(builder, GetDelegatingHandler, DefaultErrorMapping);
/// <summary>
/// Creates default delegating handler based on the <see cref="PollyResiliencePipelineDelegatingHandler"/> type.
/// </summary>
/// <param name="route">The downstream route to apply the handler for.</param>
/// <param name="contextAccessor">The context accessor of the route.</param>
/// <param name="loggerFactory">The factory of logger.</param>
/// <returns>A <see cref="DelegatingHandler"/> object, but concrete type is the <see cref="PollyResiliencePipelineDelegatingHandler"/> class.</returns>
private static DelegatingHandler GetDelegatingHandler(DownstreamRoute route, IHttpContextAccessor contextAccessor, IOcelotLoggerFactory loggerFactory)
=> new PollyResiliencePipelineDelegatingHandler(route, contextAccessor, loggerFactory);
}