mirror of
https://github.com/nsnail/Ocelot.git
synced 2025-06-19 15:28:16 +08:00
Feature/timeout for http client (#319)
* #318 http client obeys Qos timeout or defaults to 90 seconds, which is think is default for http client anyway but zero docs.... * #318 updated docs to specify default timeout and make it clear how to set it on a ReRoute basis * #318 missed this * #318 missed this
This commit is contained in:
@ -48,5 +48,6 @@ namespace Ocelot.Configuration.File
|
||||
public string Key { get;set; }
|
||||
public List<string> DelegatingHandlers {get;set;}
|
||||
public int Priority { get;set; }
|
||||
public int Timeout { get; set; }
|
||||
}
|
||||
}
|
||||
|
@ -16,12 +16,12 @@ namespace Ocelot.Configuration
|
||||
TimeoutStrategy = timeoutStrategy;
|
||||
}
|
||||
|
||||
public int ExceptionsAllowedBeforeBreaking { get; private set; }
|
||||
public int ExceptionsAllowedBeforeBreaking { get; }
|
||||
|
||||
public int DurationOfBreak { get; private set; }
|
||||
public int DurationOfBreak { get; }
|
||||
|
||||
public int TimeoutValue { get; private set; }
|
||||
public int TimeoutValue { get; }
|
||||
|
||||
public TimeoutStrategy TimeoutStrategy { get; private set; }
|
||||
public TimeoutStrategy TimeoutStrategy { get; }
|
||||
}
|
||||
}
|
||||
|
@ -17,6 +17,7 @@ namespace Ocelot.Requester
|
||||
private HttpClient _httpClient;
|
||||
private IHttpClient _client;
|
||||
private HttpClientHandler _httpclientHandler;
|
||||
private readonly TimeSpan _defaultTimeout;
|
||||
|
||||
public HttpClientBuilder(
|
||||
IDelegatingHandlerHandlerFactory factory,
|
||||
@ -26,6 +27,10 @@ namespace Ocelot.Requester
|
||||
_factory = factory;
|
||||
_cacheHandlers = cacheHandlers;
|
||||
_logger = logger;
|
||||
|
||||
// This is hardcoded at the moment but can easily be added to configuration
|
||||
// if required by a user request.
|
||||
_defaultTimeout = TimeSpan.FromSeconds(90);
|
||||
}
|
||||
|
||||
public IHttpClient Create(DownstreamContext request)
|
||||
@ -46,7 +51,14 @@ namespace Ocelot.Requester
|
||||
CookieContainer = new CookieContainer()
|
||||
};
|
||||
|
||||
_httpClient = new HttpClient(CreateHttpMessageHandler(_httpclientHandler, request.DownstreamReRoute));
|
||||
var timeout = request.DownstreamReRoute.QosOptionsOptions.TimeoutValue == 0
|
||||
? _defaultTimeout
|
||||
: TimeSpan.FromMilliseconds(request.DownstreamReRoute.QosOptionsOptions.TimeoutValue);
|
||||
|
||||
_httpClient = new HttpClient(CreateHttpMessageHandler(_httpclientHandler, request.DownstreamReRoute))
|
||||
{
|
||||
Timeout = timeout
|
||||
};
|
||||
|
||||
_client = new HttpClientWrapper(_httpClient);
|
||||
|
||||
|
@ -39,6 +39,10 @@ namespace Ocelot.Requester
|
||||
{
|
||||
return new ErrorResponse<HttpResponseMessage>(new RequestTimedOutError(exception));
|
||||
}
|
||||
catch (TaskCanceledException exception)
|
||||
{
|
||||
return new ErrorResponse<HttpResponseMessage>(new RequestTimedOutError(exception));
|
||||
}
|
||||
catch (BrokenCircuitException exception)
|
||||
{
|
||||
return new ErrorResponse<HttpResponseMessage>(new RequestTimedOutError(exception));
|
||||
|
@ -1,38 +1,38 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Ocelot.Errors;
|
||||
|
||||
namespace Ocelot.Responder
|
||||
{
|
||||
public class ErrorsToHttpStatusCodeMapper : IErrorsToHttpStatusCodeMapper
|
||||
{
|
||||
public int Map(List<Error> errors)
|
||||
{
|
||||
if (errors.Any(e => e.Code == OcelotErrorCode.UnauthenticatedError))
|
||||
{
|
||||
return 401;
|
||||
}
|
||||
|
||||
if (errors.Any(e => e.Code == OcelotErrorCode.UnauthorizedError
|
||||
|| e.Code == OcelotErrorCode.ClaimValueNotAuthorisedError
|
||||
|| e.Code == OcelotErrorCode.ScopeNotAuthorisedError
|
||||
|| e.Code == OcelotErrorCode.UserDoesNotHaveClaimError
|
||||
|| e.Code == OcelotErrorCode.CannotFindClaimError))
|
||||
{
|
||||
return 403;
|
||||
}
|
||||
|
||||
if (errors.Any(e => e.Code == OcelotErrorCode.RequestTimedOutError))
|
||||
{
|
||||
return 503;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Ocelot.Errors;
|
||||
|
||||
namespace Ocelot.Responder
|
||||
{
|
||||
public class ErrorsToHttpStatusCodeMapper : IErrorsToHttpStatusCodeMapper
|
||||
{
|
||||
public int Map(List<Error> errors)
|
||||
{
|
||||
if (errors.Any(e => e.Code == OcelotErrorCode.UnauthenticatedError))
|
||||
{
|
||||
return 401;
|
||||
}
|
||||
|
||||
if (errors.Any(e => e.Code == OcelotErrorCode.UnauthorizedError
|
||||
|| e.Code == OcelotErrorCode.ClaimValueNotAuthorisedError
|
||||
|| e.Code == OcelotErrorCode.ScopeNotAuthorisedError
|
||||
|| e.Code == OcelotErrorCode.UserDoesNotHaveClaimError
|
||||
|| e.Code == OcelotErrorCode.CannotFindClaimError))
|
||||
{
|
||||
return 403;
|
||||
}
|
||||
|
||||
if (errors.Any(e => e.Code == OcelotErrorCode.RequestTimedOutError))
|
||||
{
|
||||
return 503;
|
||||
}
|
||||
|
||||
if (errors.Any(e => e.Code == OcelotErrorCode.UnableToFindDownstreamRouteError))
|
||||
{
|
||||
return 404;
|
||||
}
|
||||
|
||||
if (errors.Any(e => e.Code == OcelotErrorCode.UnableToFindDownstreamRouteError))
|
||||
{
|
||||
return 404;
|
||||
}
|
||||
|
||||
return 404;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user