我有一个具有多个节点的集群/服务器来处理来自应用程序的请求。
用户正在运行的应用程序使用以下URL打开两个web客户端:
为了支持粘着性(我希望两个web客户机中的每一个都保持对节点的连接--持久性),ConnectionLeaseTimeout保持默认值,这意味着“不要关闭连接”,而且由于默认情况下DefaultPersistentConnectionLimit为2,所以我将DefaultConnectionLimit设置为1。
问题:
我做错什么了?
我的课堂输出:
CommunicationWebClient https://myprocess.myapp.com/api/json/v1
CommunicationWebClient https://myprocess.myapp.com/api/protobuf/v1
SendAsync _uri=https://myprocess.myapp.com/api/json/v1 servicePoint.Address=https://myprocess.myapp.com/api/json/v1 servicePoint.ConnectionLimit=1 servicePoint.CurrentConnections=2
..。
SendAsync _uri=https://myprocess.myapp.com/api/protobuf/v1 servicePoint.Address=https://myprocess.myapp.com/api/json/v1 servicePoint.ConnectionLimit=1 servicePoint.CurrentConnections=2
..。
public sealed class CommunicationWebClient : IDisposable
{
private HttpClient _httpClient;
private Uri _uri;
public CommunicationWebClient(Uri uri)
{
Logger.Debug($"{nameof(CommunicationWebClient)} {nameof(uri)}={uri}");
_uri = uri;
ServicePointManager.DefaultConnectionLimit = 1;
_httpClient = new HttpClient(new WebRequestHandler())
{
Timeout = 10.Minutes(),
};
}
public void Dispose()
{
_httpClient.Dispose();
}
public async Task SendAsync(
ByteArrayContent content)
{
var servicePoint = ServicePointManager.FindServicePoint(_uri);
Logger.Debug($"{nameof(SendAsync)} " +
$"{nameof(_uri)}={_uri} " +
$"{nameof(servicePoint.Address)}={servicePoint.Address} " +
$"{nameof(servicePoint.ConnectionLimit)}={servicePoint.ConnectionLimit} " +
$"{nameof(servicePoint.CurrentConnections)}={servicePoint.CurrentConnections}");
using (var httpResponseMessage = await _httpClient.PostAsync(_uri, content))
{
...
}
}
}发布于 2018-05-08 12:23:25
如果您仍然有问题,请检查您的CommunicationWebClient是否被处理得太频繁。它处理HttpClient,但它的行为并不像人们通常期望的那样。
查看本文:https://learn.microsoft.com/en-us/azure/architecture/antipatterns/improper-instantiation/
很快,当您在windows情况下释放HttpClient时,您会要求窗口关闭所有打开的套接字。但是默认情况下,窗口有4分钟的超时时间来完全关闭套接字。因此,在这4分钟内,您的HttpClient和web服务器之间将有一个连接。
发布于 2022-03-18 04:59:44
在大多数情况下,建议每个应用程序使用一个http客户端。处理http客户端不会立即抛出它使用的套接字。
https://stackoverflow.com/questions/46226287
复制相似问题