-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Opt-in to set Connection:Close on downstream requests #1441
Open
bjartekh
wants to merge
4
commits into
ThreeMammals:develop
Choose a base branch
from
bjartekh:feature/connection-close-opt-in
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
src/Ocelot/Configuration/Creator/ConnectionCloseCreator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using Ocelot.Configuration.File; | ||
|
||
namespace Ocelot.Configuration.Creator | ||
{ | ||
public class ConnectionCloseCreator : IConnectionCloseCreator | ||
{ | ||
public bool Create(bool fileRouteConnectionClose, FileGlobalConfiguration globalConfiguration) | ||
{ | ||
var globalConnectionClose = globalConfiguration.ConnectionClose; | ||
|
||
return fileRouteConnectionClose || globalConnectionClose; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
using Ocelot.Configuration.File; | ||
|
||
namespace Ocelot.Configuration.Creator | ||
{ | ||
public interface IConnectionCloseCreator | ||
{ | ||
bool Create(bool fileRouteConnectionClose, FileGlobalConfiguration globalConfiguration); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
using System; | ||
using System.Linq; | ||
using System.Net; | ||
using System.Net.Http; | ||
|
||
using Ocelot.Configuration; | ||
|
||
using Ocelot.Logging; | ||
|
||
namespace Ocelot.Requester | ||
{ | ||
public interface IHttpClientBuilder { } | ||
public interface IHttpClientCache | ||
{ | ||
IHttpClient Get(DownstreamRoute cacheKey); | ||
void Set(DownstreamRoute cacheKey, IHttpClient client, TimeSpan span); | ||
} | ||
|
||
public class HttpClientBuilder : IHttpClientBuilder | ||
{ | ||
private readonly IDelegatingHandlerHandlerFactory _factory; | ||
private readonly IHttpClientCache _cacheHandlers; | ||
private readonly IOcelotLogger _logger; | ||
private DownstreamRoute _cacheKey; | ||
private HttpClient _httpClient; | ||
private IHttpClient _client; | ||
private readonly TimeSpan _defaultTimeout; | ||
|
||
public HttpClientBuilder( | ||
IDelegatingHandlerHandlerFactory factory, | ||
IHttpClientCache cacheHandlers, | ||
IOcelotLogger logger) | ||
{ | ||
_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(DownstreamRoute downstreamRoute) | ||
{ | ||
_cacheKey = downstreamRoute; | ||
|
||
var httpClient = _cacheHandlers.Get(_cacheKey); | ||
|
||
if (httpClient != null) | ||
{ | ||
_client = httpClient; | ||
return httpClient; | ||
} | ||
|
||
var handler = CreateHandler(downstreamRoute); | ||
|
||
if (downstreamRoute.DangerousAcceptAnyServerCertificateValidator) | ||
{ | ||
handler.ServerCertificateCustomValidationCallback = | ||
HttpClientHandler.DangerousAcceptAnyServerCertificateValidator; | ||
|
||
_logger | ||
.LogWarning($"You have ignored all SSL warnings by using DangerousAcceptAnyServerCertificateValidator for this DownstreamRoute, UpstreamPathTemplate: {downstreamRoute.UpstreamPathTemplate}, DownstreamPathTemplate: {downstreamRoute.DownstreamPathTemplate}"); | ||
} | ||
|
||
var timeout = downstreamRoute.QosOptions.TimeoutValue == 0 | ||
? _defaultTimeout | ||
: TimeSpan.FromMilliseconds(downstreamRoute.QosOptions.TimeoutValue); | ||
|
||
_httpClient = new HttpClient(CreateHttpMessageHandler(handler, downstreamRoute)) | ||
{ | ||
Timeout = timeout, | ||
}; | ||
|
||
_client = new HttpClientWrapper(_httpClient, downstreamRoute.ConnectionClose); // TODO | ||
|
||
return _client; | ||
} | ||
|
||
private static HttpClientHandler CreateHandler(DownstreamRoute downstreamRoute) | ||
{ | ||
// Dont' create the CookieContainer if UseCookies is not set or the HttpClient will complain | ||
// under .Net Full Framework | ||
var useCookies = downstreamRoute.HttpHandlerOptions.UseCookieContainer; | ||
|
||
return useCookies ? UseCookiesHandler(downstreamRoute) : UseNonCookiesHandler(downstreamRoute); | ||
} | ||
|
||
private static HttpClientHandler UseNonCookiesHandler(DownstreamRoute downstreamRoute) | ||
{ | ||
return new HttpClientHandler | ||
{ | ||
AllowAutoRedirect = downstreamRoute.HttpHandlerOptions.AllowAutoRedirect, | ||
UseCookies = downstreamRoute.HttpHandlerOptions.UseCookieContainer, | ||
UseProxy = downstreamRoute.HttpHandlerOptions.UseProxy, | ||
MaxConnectionsPerServer = downstreamRoute.HttpHandlerOptions.MaxConnectionsPerServer, | ||
|
||
}; | ||
} | ||
|
||
private static HttpClientHandler UseCookiesHandler(DownstreamRoute downstreamRoute) | ||
{ | ||
return new HttpClientHandler | ||
{ | ||
AllowAutoRedirect = downstreamRoute.HttpHandlerOptions.AllowAutoRedirect, | ||
UseCookies = downstreamRoute.HttpHandlerOptions.UseCookieContainer, | ||
UseProxy = downstreamRoute.HttpHandlerOptions.UseProxy, | ||
MaxConnectionsPerServer = downstreamRoute.HttpHandlerOptions.MaxConnectionsPerServer, | ||
CookieContainer = new CookieContainer(), | ||
}; | ||
} | ||
|
||
public void Save() | ||
{ | ||
_cacheHandlers.Set(_cacheKey, _client, TimeSpan.FromHours(24)); | ||
} | ||
|
||
private HttpMessageHandler CreateHttpMessageHandler(HttpMessageHandler httpMessageHandler, DownstreamRoute request) | ||
{ | ||
//todo handle error | ||
var handlers = _factory.Get(request).Data; | ||
|
||
handlers | ||
.Select(handler => handler) | ||
.Reverse() | ||
.ToList() | ||
.ForEach(handler => | ||
{ | ||
var delegatingHandler = handler(); | ||
delegatingHandler.InnerHandler = httpMessageHandler; | ||
httpMessageHandler = delegatingHandler; | ||
}); | ||
return httpMessageHandler; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
using System.Net.Http; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace Ocelot.Requester | ||
{ | ||
public interface IHttpClient { } | ||
|
||
/// <summary> | ||
/// This class was made to make unit testing easier when HttpClient is used. | ||
/// </summary> | ||
public class HttpClientWrapper : IHttpClient | ||
{ | ||
public HttpClient Client { get; } | ||
|
||
public bool ConnectionClose { get; } // TODO | ||
|
||
public HttpClientWrapper(HttpClient client, bool connectionClose = false) // TODO | ||
{ | ||
Client = client; | ||
ConnectionClose = connectionClose; | ||
} | ||
|
||
public Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken = default) | ||
{ | ||
request.Headers.ConnectionClose = ConnectionClose; // TODO | ||
return Client.SendAsync(request, cancellationToken); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeap!
ToList
was a bit overhead to get access toForEach
helper.Aggregate
helper is good, for sure because it is applicable for this case.There is another coding life hack: calling
All
to process all elements of collection. 😃But!... Ray, this file is redundant one! I left this file during rebasing just to keep the author's changes: see
// TODO
😉Both files are just container of new changes
You may check, there are no these files in develop! 🙃
This PR is not ready for reviews: it must be developed more ❗