首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CurrentConnections在ServicePoint中超过ConnectionLimit

CurrentConnections在ServicePoint中超过ConnectionLimit
EN

Stack Overflow用户
提问于 2017-09-14 18:46:52
回答 2查看 1.2K关注 0票数 1

我有一个具有多个节点的集群/服务器来处理来自应用程序的请求。

用户正在运行的应用程序使用以下URL打开两个web客户端:

为了支持粘着性(我希望两个web客户机中的每一个都保持对节点的连接--持久性),ConnectionLeaseTimeout保持默认值,这意味着“不要关闭连接”,而且由于默认情况下DefaultPersistentConnectionLimit为2,所以我将DefaultConnectionLimit设置为1。

问题:

  1. servicePoint.CurrentConnections为2,而servicePoint.ConnectionLimit为1。
  2. 在服务器中,我看到远程主机端口正在改变,也就是说,我看到了两个以上的端口(每个客户端超过一个端口)。

我做错什么了?

我的课堂输出:

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

..。

代码语言:javascript
复制
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))
        {
            ...
        }
    }
}
EN

回答 2

Stack Overflow用户

发布于 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服务器之间将有一个连接。

票数 0
EN

Stack Overflow用户

发布于 2022-03-18 04:59:44

在大多数情况下,建议每个应用程序使用一个http客户端。处理http客户端不会立即抛出它使用的套接字。

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46226287

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档