闽公网安备 35020302035485号
问题:
Server endpoint是host还是url,例如:https://www.duidaima.com/a/b.js是duidaima.com还是duidaima.com/a/b.js?
HttpClientHandler.MaxConnectionsPerServer
HttpClientHandler.MaxConnectionsPerServer 的实现代码如下:
public int MaxConnectionsPerServer
{
get => _underlyingHandler.MaxConnectionsPerServer;
set => _underlyingHandler.MaxConnectionsPerServer = value;
}
实际使用的是_underlyingHandler.MaxConnectionsPerServer。而_underlyingHandler是SocketsHttpHandler类的实例:using HttpHandlerType = System.Net.Http.SocketsHttpHandler; private readonly HttpHandlerType _underlyingHandler; SocketsHttpHandler.MaxConnectionsPerServerSocketsHttpHandler.MaxConnectionsPerServer 的实现代码如下:
public int MaxConnectionsPerServer
{
get => _settings._maxConnectionsPerServer;
set
{
...
_settings._maxConnectionsPerServer = value;
}
}
实际使用的是_settings._maxConnectionsPerServer。那么,谁在使用这个设置值呢?public HttpConnectionPool(HttpConnectionPoolManager poolManager, HttpConnectionKind kind, string? host, int port, string? sslHostName, Uri? proxyUri)
{
...
_maxHttp11Connections = Settings._maxConnectionsPerServer;
...
}
而HttpConnectionPool的作用就是,提供到同一终结点的连接池。看来我们离真相越来越近了。/// <summary>Provides a pool of connections to the same endpoint.</summary> internal sealed class HttpConnectionPool : IDisposableHttpConnectionPoolManager
public ValueTask<HttpResponseMessage> SendAsyncCore(HttpRequestMessage request, Uri? proxyUri, bool async, bool doRequestAuth, bool isProxyConnect, CancellationToken cancellationToken)
{
HttpConnectionKey key = GetConnectionKey(request, proxyUri, isProxyConnect);
HttpConnectionPool? pool;
while (!_pools.TryGetValue(key, out pool))
{
pool = new HttpConnectionPool(this, key.Kind, key.Host, key.Port, key.SslHostName, key.ProxyUri);
可以看到,HttpConnectionPool是从ConcurrentDictionary _pools 中获取的,而key的值是HttpConnectionKey类型。public readonly HttpConnectionKind Kind;
public readonly string? Host;
public readonly int Port;
public readonly string? SslHostName; // null if not SSL
public readonly Uri? ProxyUri;
public readonly string Identity;
public bool Equals(HttpConnectionKey other) =>
Kind == other.Kind &&
Host == other.Host &&
Port == other.Port &&
ProxyUri == other.ProxyUri &&
SslHostName == other.SslHostName &&
Identity == other.Identity;
结论