mirror of
https://github.com/nsnail/Ocelot.git
synced 2025-06-19 09:38:14 +08:00
Refactor qos as options
This commit is contained in:
@ -33,12 +33,11 @@ namespace Ocelot.Configuration.Builder
|
||||
private string _downstreamScheme;
|
||||
private string _downstreamHost;
|
||||
private int _dsPort;
|
||||
private int _exceptionsAllowedBeforeBreaking;
|
||||
private int _durationOfBreak;
|
||||
private int _timeoutValue;
|
||||
private string _loadBalancer;
|
||||
private string _serviceProviderHost;
|
||||
private int _serviceProviderPort;
|
||||
private bool _useQos;
|
||||
private QoSOptions _qosOptions;
|
||||
|
||||
|
||||
public ReRouteBuilder()
|
||||
@ -206,23 +205,18 @@ namespace Ocelot.Configuration.Builder
|
||||
return this;
|
||||
}
|
||||
|
||||
public ReRouteBuilder WithExceptionsAllowedBeforeBreaking(int exceptionsAllowedBeforeBreaking)
|
||||
public ReRouteBuilder WithIsQos(bool input)
|
||||
{
|
||||
_exceptionsAllowedBeforeBreaking = exceptionsAllowedBeforeBreaking;
|
||||
_useQos = input;
|
||||
return this;
|
||||
}
|
||||
|
||||
public ReRouteBuilder WithDurationOfBreak(int durationOfBreak)
|
||||
public ReRouteBuilder WithQosOptions(QoSOptions input)
|
||||
{
|
||||
_durationOfBreak = durationOfBreak;
|
||||
return this;
|
||||
}
|
||||
|
||||
public ReRouteBuilder WithTimeoutValue(int timeoutValue)
|
||||
{
|
||||
_timeoutValue = timeoutValue;
|
||||
_qosOptions = input;
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
public ReRouteBuilder WithLoadBalancerKey(string loadBalancerKey)
|
||||
{
|
||||
@ -250,7 +244,7 @@ namespace Ocelot.Configuration.Builder
|
||||
_isAuthorised, _claimToQueries, _requestIdHeaderKey, _isCached, _fileCacheOptions, _downstreamScheme, _loadBalancer,
|
||||
_downstreamHost, _dsPort, _loadBalancerKey, new ServiceProviderConfiguraion(_serviceName, _downstreamHost, _dsPort, _useServiceDiscovery,
|
||||
_serviceDiscoveryProvider, _serviceProviderHost, _serviceProviderPort),
|
||||
_exceptionsAllowedBeforeBreaking,_durationOfBreak, _timeoutValue);
|
||||
_useQos,_qosOptions);
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -96,6 +96,8 @@ namespace Ocelot.Configuration.Creator
|
||||
|
||||
var isCached = fileReRoute.FileCacheOptions.TtlSeconds > 0;
|
||||
|
||||
var isQos = fileReRoute.QoSOptions.ExceptionsAllowedBeforeBreaking > 0 && fileReRoute.QoSOptions.TimeoutValue >0;
|
||||
|
||||
var requestIdKey = globalRequestIdConfiguration
|
||||
? globalConfiguration.RequestIdKey
|
||||
: fileReRoute.RequestIdKey;
|
||||
@ -135,8 +137,8 @@ namespace Ocelot.Configuration.Creator
|
||||
requestIdKey, isCached, new CacheOptions(fileReRoute.FileCacheOptions.TtlSeconds)
|
||||
, fileReRoute.DownstreamScheme,
|
||||
fileReRoute.LoadBalancer, fileReRoute.DownstreamHost, fileReRoute.DownstreamPort, loadBalancerKey,
|
||||
serviceProviderConfiguration, fileReRoute.ExceptionsAllowedBeforeBreaking,
|
||||
fileReRoute.DurationOfBreak, fileReRoute.TimeoutValue);
|
||||
serviceProviderConfiguration, isQos,
|
||||
new QoSOptions(fileReRoute.QoSOptions.ExceptionsAllowedBeforeBreaking, fileReRoute.QoSOptions.DurationOfBreak, fileReRoute.QoSOptions.TimeoutValue));
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -148,8 +150,8 @@ namespace Ocelot.Configuration.Creator
|
||||
requestIdKey, isCached, new CacheOptions(fileReRoute.FileCacheOptions.TtlSeconds),
|
||||
fileReRoute.DownstreamScheme,
|
||||
fileReRoute.LoadBalancer, fileReRoute.DownstreamHost, fileReRoute.DownstreamPort, loadBalancerKey,
|
||||
serviceProviderConfiguration, fileReRoute.ExceptionsAllowedBeforeBreaking,
|
||||
fileReRoute.DurationOfBreak, fileReRoute.TimeoutValue);
|
||||
serviceProviderConfiguration, isQos,
|
||||
new QoSOptions(fileReRoute.QoSOptions.ExceptionsAllowedBeforeBreaking, fileReRoute.QoSOptions.DurationOfBreak, fileReRoute.QoSOptions.TimeoutValue));
|
||||
}
|
||||
|
||||
var loadBalancer = await _loadBalanceFactory.Get(reRoute);
|
||||
|
16
src/Ocelot/Configuration/File/FileQoSOptions.cs
Normal file
16
src/Ocelot/Configuration/File/FileQoSOptions.cs
Normal file
@ -0,0 +1,16 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Ocelot.Configuration.File
|
||||
{
|
||||
public class FileQoSOptions
|
||||
{
|
||||
public int ExceptionsAllowedBeforeBreaking { get; set; }
|
||||
|
||||
public int DurationOfBreak { get; set; }
|
||||
|
||||
public int TimeoutValue { get; set; }
|
||||
}
|
||||
}
|
@ -12,6 +12,7 @@ namespace Ocelot.Configuration.File
|
||||
AddQueriesToRequest = new Dictionary<string, string>();
|
||||
AuthenticationOptions = new FileAuthenticationOptions();
|
||||
FileCacheOptions = new FileCacheOptions();
|
||||
QoSOptions = new FileQoSOptions();
|
||||
}
|
||||
|
||||
public string DownstreamPathTemplate { get; set; }
|
||||
@ -29,9 +30,7 @@ namespace Ocelot.Configuration.File
|
||||
public string DownstreamScheme {get;set;}
|
||||
public string DownstreamHost {get;set;}
|
||||
public int DownstreamPort { get; set; }
|
||||
public int ExceptionsAllowedBeforeBreaking { get; set; }
|
||||
public int DurationOfBreak { get; set; }
|
||||
public int TimeoutValue { get; set; }
|
||||
public FileQoSOptions QoSOptions { get; set; }
|
||||
public string LoadBalancer {get;set;}
|
||||
}
|
||||
}
|
@ -4,17 +4,18 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Ocelot.Values
|
||||
namespace Ocelot.Configuration
|
||||
{
|
||||
public class QoS
|
||||
public class QoSOptions
|
||||
{
|
||||
public QoS(int exceptionsAllowedBeforeBreaking, int durationofBreak, int timeoutValue, TimeoutStrategy timeoutStrategy = TimeoutStrategy.Pessimistic)
|
||||
public QoSOptions(int exceptionsAllowedBeforeBreaking, int durationofBreak, int timeoutValue, TimeoutStrategy timeoutStrategy = TimeoutStrategy.Pessimistic)
|
||||
{
|
||||
ExceptionsAllowedBeforeBreaking = exceptionsAllowedBeforeBreaking;
|
||||
DurationOfBreak = TimeSpan.FromMilliseconds(durationofBreak);
|
||||
TimeoutValue = TimeSpan.FromMilliseconds(timeoutValue);
|
||||
TimeoutStrategy = timeoutStrategy;
|
||||
}
|
||||
|
||||
|
||||
public int ExceptionsAllowedBeforeBreaking { get; private set; }
|
||||
|
||||
@ -23,5 +24,6 @@ namespace Ocelot.Values
|
||||
public TimeSpan TimeoutValue { get; private set; }
|
||||
|
||||
public TimeoutStrategy TimeoutStrategy { get; private set; }
|
||||
|
||||
}
|
||||
}
|
@ -17,7 +17,7 @@ namespace Ocelot.Configuration
|
||||
string requestIdKey, bool isCached, CacheOptions fileCacheOptions,
|
||||
string downstreamScheme, string loadBalancer, string downstreamHost,
|
||||
int downstreamPort, string loadBalancerKey, ServiceProviderConfiguraion serviceProviderConfiguraion,
|
||||
int exceptionsAllowedBeforeBreaking =3, int durationofBreak =8, int timeoutValue = 5000)
|
||||
bool isQos,QoSOptions qos)
|
||||
{
|
||||
LoadBalancerKey = loadBalancerKey;
|
||||
ServiceProviderConfiguraion = serviceProviderConfiguraion;
|
||||
@ -42,9 +42,8 @@ namespace Ocelot.Configuration
|
||||
ClaimsToHeaders = configurationHeaderExtractorProperties
|
||||
?? new List<ClaimToThing>();
|
||||
DownstreamScheme = downstreamScheme;
|
||||
ExceptionsAllowedBeforeBreaking = exceptionsAllowedBeforeBreaking;
|
||||
DurationOfBreak = durationofBreak;
|
||||
TimeoutValue = timeoutValue;
|
||||
IsQos = isQos;
|
||||
QosOptions = qos;
|
||||
}
|
||||
|
||||
public string LoadBalancerKey {get;private set;}
|
||||
@ -63,9 +62,8 @@ namespace Ocelot.Configuration
|
||||
public bool IsCached { get; private set; }
|
||||
public CacheOptions FileCacheOptions { get; private set; }
|
||||
public string DownstreamScheme {get;private set;}
|
||||
public int ExceptionsAllowedBeforeBreaking { get; private set; }
|
||||
public int DurationOfBreak { get; private set; }
|
||||
public int TimeoutValue { get; private set; }
|
||||
public bool IsQos { get; private set; }
|
||||
public QoSOptions QosOptions { get; private set; }
|
||||
public string LoadBalancer {get;private set;}
|
||||
public string DownstreamHost { get; private set; }
|
||||
public int DownstreamPort { get; private set; }
|
||||
|
@ -2,6 +2,7 @@
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Ocelot.Responses;
|
||||
using Ocelot.Configuration;
|
||||
|
||||
namespace Ocelot.Request.Builder
|
||||
{
|
||||
@ -16,7 +17,8 @@ namespace Ocelot.Request.Builder
|
||||
QueryString queryString,
|
||||
string contentType,
|
||||
RequestId.RequestId requestId,
|
||||
Values.QoS qos)
|
||||
bool isQos,
|
||||
QoSOptions qos)
|
||||
{
|
||||
var request = await new RequestBuilder()
|
||||
.WithHttpMethod(httpMethod)
|
||||
@ -27,6 +29,7 @@ namespace Ocelot.Request.Builder
|
||||
.WithHeaders(headers)
|
||||
.WithRequestId(requestId)
|
||||
.WithCookies(cookies)
|
||||
.WithIsQos(isQos)
|
||||
.WithQos(qos)
|
||||
.Build();
|
||||
|
||||
|
@ -2,6 +2,7 @@
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Ocelot.Responses;
|
||||
using Ocelot.Configuration;
|
||||
|
||||
namespace Ocelot.Request.Builder
|
||||
{
|
||||
@ -13,8 +14,9 @@ namespace Ocelot.Request.Builder
|
||||
IHeaderDictionary headers,
|
||||
IRequestCookieCollection cookies,
|
||||
QueryString queryString,
|
||||
string contentType,
|
||||
string contentType,
|
||||
RequestId.RequestId requestId,
|
||||
Values.QoS qos);
|
||||
bool isQos,
|
||||
QoSOptions qos);
|
||||
}
|
||||
}
|
||||
|
@ -8,6 +8,7 @@ using System.Net.Http.Headers;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.Extensions.Primitives;
|
||||
using Ocelot.Configuration;
|
||||
|
||||
namespace Ocelot.Request.Builder
|
||||
{
|
||||
@ -22,7 +23,8 @@ namespace Ocelot.Request.Builder
|
||||
private RequestId.RequestId _requestId;
|
||||
private IRequestCookieCollection _cookies;
|
||||
private readonly string[] _unsupportedHeaders = {"host"};
|
||||
private Values.QoS _qos;
|
||||
private bool _isQos;
|
||||
private QoSOptions _qos;
|
||||
|
||||
public RequestBuilder WithHttpMethod(string httpMethod)
|
||||
{
|
||||
@ -72,7 +74,13 @@ namespace Ocelot.Request.Builder
|
||||
return this;
|
||||
}
|
||||
|
||||
public RequestBuilder WithQos(Values.QoS qos)
|
||||
public RequestBuilder WithIsQos(bool isqos)
|
||||
{
|
||||
_isQos = isqos;
|
||||
return this;
|
||||
}
|
||||
|
||||
public RequestBuilder WithQos(QoSOptions qos)
|
||||
{
|
||||
_qos = qos;
|
||||
return this;
|
||||
@ -97,7 +105,7 @@ namespace Ocelot.Request.Builder
|
||||
|
||||
var cookieContainer = CreateCookieContainer(uri);
|
||||
|
||||
return new Request(httpRequestMessage, cookieContainer, _qos);
|
||||
return new Request(httpRequestMessage, cookieContainer,_isQos,_qos);
|
||||
}
|
||||
|
||||
private Uri CreateUri()
|
||||
|
@ -33,7 +33,7 @@ namespace Ocelot.Request.Middleware
|
||||
.Build(context.Request.Method, DownstreamUrl, context.Request.Body,
|
||||
context.Request.Headers, context.Request.Cookies, context.Request.QueryString,
|
||||
context.Request.ContentType, new RequestId.RequestId(DownstreamRoute?.ReRoute?.RequestIdKey, context.TraceIdentifier),
|
||||
new Values.QoS(DownstreamRoute.ReRoute.ExceptionsAllowedBeforeBreaking, DownstreamRoute.ReRoute.DurationOfBreak, DownstreamRoute.ReRoute.TimeoutValue));
|
||||
DownstreamRoute.ReRoute.IsQos,DownstreamRoute.ReRoute.QosOptions);
|
||||
|
||||
if (buildResult.IsError)
|
||||
{
|
||||
|
@ -1,4 +1,5 @@
|
||||
using Ocelot.Values;
|
||||
using Ocelot.Configuration;
|
||||
using Ocelot.Values;
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
|
||||
@ -6,15 +7,17 @@ namespace Ocelot.Request
|
||||
{
|
||||
public class Request
|
||||
{
|
||||
public Request(HttpRequestMessage httpRequestMessage, CookieContainer cookieContainer, QoS qos)
|
||||
public Request(HttpRequestMessage httpRequestMessage, CookieContainer cookieContainer,bool isQos, QoSOptions qos)
|
||||
{
|
||||
HttpRequestMessage = httpRequestMessage;
|
||||
CookieContainer = cookieContainer;
|
||||
IsQos = isQos;
|
||||
Qos = qos;
|
||||
}
|
||||
|
||||
public HttpRequestMessage HttpRequestMessage { get; private set; }
|
||||
public CookieContainer CookieContainer { get; private set; }
|
||||
public QoS Qos { get; private set; }
|
||||
public bool IsQos { get; private set; }
|
||||
public QoSOptions Qos { get; private set; }
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,5 @@
|
||||
using Ocelot.Logging;
|
||||
using Ocelot.Configuration;
|
||||
using Ocelot.Logging;
|
||||
using Ocelot.Values;
|
||||
using Polly.Timeout;
|
||||
using System;
|
||||
@ -13,15 +14,15 @@ namespace Ocelot.Requester
|
||||
{
|
||||
private readonly Dictionary<int, Func<DelegatingHandler>> handlers = new Dictionary<int, Func<DelegatingHandler>>();
|
||||
|
||||
public HttpClientBuilder WithCircuitBreaker(QoS qos, IOcelotLogger logger, HttpMessageHandler innerHandler)
|
||||
public HttpClientBuilder WithCircuitBreaker(QoSOptions qos, IOcelotLogger logger, HttpMessageHandler innerHandler)
|
||||
{
|
||||
handlers.Add(5000, () => new CircuitBreakingDelegatingHandler(qos.ExceptionsAllowedBeforeBreaking, qos.DurationOfBreak, qos.TimeoutValue, qos.TimeoutStrategy, logger, innerHandler));
|
||||
return this;
|
||||
}
|
||||
|
||||
internal HttpClient Build()
|
||||
internal HttpClient Build(HttpMessageHandler innerHandler)
|
||||
{
|
||||
return handlers.Any() ? new HttpClient(CreateHttpMessageHandler()) : new HttpClient();
|
||||
return handlers.Any() ? new HttpClient(CreateHttpMessageHandler()) : new HttpClient(innerHandler);
|
||||
}
|
||||
|
||||
private HttpMessageHandler CreateHttpMessageHandler()
|
||||
|
@ -23,8 +23,11 @@ namespace Ocelot.Requester
|
||||
|
||||
using (var handler = new HttpClientHandler { CookieContainer = request.CookieContainer })
|
||||
{
|
||||
builder.WithCircuitBreaker(request.Qos, _logger, handler);
|
||||
using (var httpClient = builder.Build())
|
||||
if (request.IsQos)
|
||||
{
|
||||
builder.WithCircuitBreaker(request.Qos, _logger, handler);
|
||||
}
|
||||
using (var httpClient = builder.Build(handler))
|
||||
{
|
||||
try
|
||||
{
|
||||
|
Reference in New Issue
Block a user