使用HttpClientFactory取代HttpClient
This commit is contained in:
parent
dfa6bc1367
commit
b6b449644b
@ -1,50 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Net.Http;
|
|
||||||
using System.Threading;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
|
|
||||||
namespace FastGithub.ReverseProxy
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// YARP的HttpClient
|
|
||||||
/// </summary>
|
|
||||||
class HttpClient : HttpMessageInvoker
|
|
||||||
{
|
|
||||||
private readonly TlsSniPattern tlsSniPattern;
|
|
||||||
private readonly bool tlsIgnoreNameMismatch;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// YARP的HttpClient
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="handler"></param>
|
|
||||||
/// <param name="tlsSniPattern"></param>
|
|
||||||
/// <param name="disposeHandler"></param>
|
|
||||||
public HttpClient(
|
|
||||||
HttpMessageHandler handler,
|
|
||||||
TlsSniPattern tlsSniPattern,
|
|
||||||
bool tlsIgnoreNameMismatch,
|
|
||||||
bool disposeHandler = false) : base(handler, disposeHandler)
|
|
||||||
{
|
|
||||||
this.tlsSniPattern = tlsSniPattern;
|
|
||||||
this.tlsIgnoreNameMismatch = tlsIgnoreNameMismatch;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 发送数据
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="request"></param>
|
|
||||||
/// <param name="cancellationToken"></param>
|
|
||||||
/// <returns></returns>
|
|
||||||
public override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
|
|
||||||
{
|
|
||||||
request.SetRequestContext(new RequestContext
|
|
||||||
{
|
|
||||||
Host = request.RequestUri?.Host,
|
|
||||||
IsHttps = request.RequestUri?.Scheme == Uri.UriSchemeHttps,
|
|
||||||
TlsSniPattern = this.tlsSniPattern,
|
|
||||||
TlsIgnoreNameMismatch = this.tlsIgnoreNameMismatch
|
|
||||||
});
|
|
||||||
return base.SendAsync(request, cancellationToken);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
73
FastGithub.ReverseProxy/HttpClientFactory.cs
Normal file
73
FastGithub.ReverseProxy/HttpClientFactory.cs
Normal file
@ -0,0 +1,73 @@
|
|||||||
|
using Microsoft.Extensions.Options;
|
||||||
|
using System;
|
||||||
|
using System.Net.Http;
|
||||||
|
using System.Threading;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace FastGithub.ReverseProxy
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// HttpClient工厂
|
||||||
|
/// </summary>
|
||||||
|
sealed class HttpClientFactory
|
||||||
|
{
|
||||||
|
private HttpClientHanlder httpClientHanlder;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// HttpClient工厂
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="domainResolver"></param>
|
||||||
|
/// <param name="options"></param>
|
||||||
|
public HttpClientFactory(
|
||||||
|
DomainResolver domainResolver,
|
||||||
|
IOptionsMonitor<FastGithubOptions> options)
|
||||||
|
{
|
||||||
|
this.httpClientHanlder = new HttpClientHanlder(domainResolver);
|
||||||
|
options.OnChange(opt => this.httpClientHanlder = new HttpClientHanlder(domainResolver));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 创建httpClient
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="domainConfig"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public HttpMessageInvoker CreateHttpClient(DomainConfig domainConfig)
|
||||||
|
{
|
||||||
|
return new HttpClient(this.httpClientHanlder, domainConfig, disposeHandler: false);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// http客户端
|
||||||
|
/// </summary>
|
||||||
|
private class HttpClient : HttpMessageInvoker
|
||||||
|
{
|
||||||
|
private readonly DomainConfig domainConfig;
|
||||||
|
|
||||||
|
public HttpClient(
|
||||||
|
HttpMessageHandler handler,
|
||||||
|
DomainConfig domainConfig,
|
||||||
|
bool disposeHandler = false) : base(handler, disposeHandler)
|
||||||
|
{
|
||||||
|
this.domainConfig = domainConfig;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 发送数据
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="request"></param>
|
||||||
|
/// <param name="cancellationToken"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
request.SetRequestContext(new RequestContext
|
||||||
|
{
|
||||||
|
Host = request.RequestUri?.Host,
|
||||||
|
IsHttps = request.RequestUri?.Scheme == Uri.UriSchemeHttps,
|
||||||
|
TlsSniPattern = this.domainConfig.GetTlsSniPattern(),
|
||||||
|
TlsIgnoreNameMismatch = this.domainConfig.TlsIgnoreNameMismatch
|
||||||
|
});
|
||||||
|
return base.SendAsync(request, cancellationToken);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -12,18 +12,18 @@ namespace FastGithub.ReverseProxy
|
|||||||
sealed class ReverseProxyMiddleware
|
sealed class ReverseProxyMiddleware
|
||||||
{
|
{
|
||||||
private readonly IHttpForwarder httpForwarder;
|
private readonly IHttpForwarder httpForwarder;
|
||||||
private readonly HttpClientHanlder httpClientHanlder;
|
private readonly HttpClientFactory httpClientFactory;
|
||||||
private readonly FastGithubConfig fastGithubConfig;
|
private readonly FastGithubConfig fastGithubConfig;
|
||||||
private readonly ILogger<ReverseProxyMiddleware> logger;
|
private readonly ILogger<ReverseProxyMiddleware> logger;
|
||||||
|
|
||||||
public ReverseProxyMiddleware(
|
public ReverseProxyMiddleware(
|
||||||
IHttpForwarder httpForwarder,
|
IHttpForwarder httpForwarder,
|
||||||
HttpClientHanlder httpClientHanlder,
|
HttpClientFactory httpClientFactory,
|
||||||
FastGithubConfig fastGithubConfig,
|
FastGithubConfig fastGithubConfig,
|
||||||
ILogger<ReverseProxyMiddleware> logger)
|
ILogger<ReverseProxyMiddleware> logger)
|
||||||
{
|
{
|
||||||
this.httpForwarder = httpForwarder;
|
this.httpForwarder = httpForwarder;
|
||||||
this.httpClientHanlder = httpClientHanlder;
|
this.httpClientFactory = httpClientFactory;
|
||||||
this.fastGithubConfig = fastGithubConfig;
|
this.fastGithubConfig = fastGithubConfig;
|
||||||
this.logger = logger;
|
this.logger = logger;
|
||||||
}
|
}
|
||||||
@ -54,11 +54,9 @@ namespace FastGithub.ReverseProxy
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
var destinationPrefix = GetDestinationPrefix(host, domainConfig.Destination);
|
var destinationPrefix = GetDestinationPrefix(host, domainConfig.Destination);
|
||||||
|
var httpClient = this.httpClientFactory.CreateHttpClient(domainConfig);
|
||||||
var requestConfig = new ForwarderRequestConfig { Timeout = domainConfig.Timeout };
|
var requestConfig = new ForwarderRequestConfig { Timeout = domainConfig.Timeout };
|
||||||
|
|
||||||
var tlsSniPattern = domainConfig.GetTlsSniPattern();
|
|
||||||
using var httpClient = new HttpClient(this.httpClientHanlder, tlsSniPattern, domainConfig.TlsIgnoreNameMismatch);
|
|
||||||
|
|
||||||
var error = await httpForwarder.SendAsync(context, destinationPrefix, httpClient, requestConfig);
|
var error = await httpForwarder.SendAsync(context, destinationPrefix, httpClient, requestConfig);
|
||||||
await HandleErrorAsync(context, error);
|
await HandleErrorAsync(context, error);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -19,7 +19,7 @@ namespace FastGithub
|
|||||||
.AddMemoryCache()
|
.AddMemoryCache()
|
||||||
.AddHttpForwarder()
|
.AddHttpForwarder()
|
||||||
.AddSingleton<DomainResolver>()
|
.AddSingleton<DomainResolver>()
|
||||||
.AddTransient<HttpClientHanlder>()
|
.AddTransient<HttpClientFactory>()
|
||||||
.AddSingleton<RequestLoggingMilldeware>()
|
.AddSingleton<RequestLoggingMilldeware>()
|
||||||
.AddSingleton<ReverseProxyMiddleware>();
|
.AddSingleton<ReverseProxyMiddleware>();
|
||||||
}
|
}
|
||||||
|
|||||||
@ -40,7 +40,8 @@
|
|||||||
"TlsSni": false
|
"TlsSni": false
|
||||||
},
|
},
|
||||||
"*github*.s3.amazonaws.com": {
|
"*github*.s3.amazonaws.com": {
|
||||||
"TlsSni": false
|
"TlsSni": false,
|
||||||
|
"TlsIgnoreNameMismatch": true
|
||||||
},
|
},
|
||||||
"ajax.googleapis.com": {
|
"ajax.googleapis.com": {
|
||||||
"TlsSni": true,
|
"TlsSni": true,
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user