gpt4 book ai didi

c# - 在多个线程中重复使用单个HttpClient时,套接字用完了-仍然有1000个TIMED_WAIT连接

转载 作者:行者123 更新时间:2023-12-03 11:59:16 27 4
gpt4 key购买 nike

环境:Windows Server 2012 R2 64位。
C#.NET Framework版本4.5.1。

我正在尝试使用此程序从SharePoint 2013网站下载一堆文件:https://github.com/nddipiazza/Sharepoint-Exporter

在此项目中,有一个[FileDownloader.cs] [1]文件,该文件提取从BlockingCollection中下载文件的请求并将其下载到文件中。

运行此命令时,我很快会遇到套接字错误:

System.AggregateException: One or more errors occurred. ---> System.Net.Http.HttpRequestException: An error occurred while sending the request. ---> System.Net.WebException: Unable to connect to the remote server ---> System.Net.Sockets.SocketException: Only one usage of each socket address (protocol/network address/port) is normally permitted 10.5.50.2:443
at System.Net.Sockets.Socket.EndConnect(IAsyncResult asyncResult)
at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Exception& exception)
--- End of inner exception stack trace ---
at System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
at System.Net.Http.HttpClientHandler.GetResponseCallback(IAsyncResult ar)
--- End of inner exception stack trace ---
--- End of inner exception stack trace ---
at System.Threading.Tasks.Task`1.GetResultCore(Boolean waitCompletionNotification)
at SpPrefetchIndexBuilder.FileDownloader.attemptToDownload(FileToDownload toDownload, Int32 numRetry)
---> (Inner Exception #0) System.Net.Http.HttpRequestException: An error occurred while sending the request. ---> System.Net.WebException: Unable to connect to the remote server ---> System.Net.Sockets.SocketException: Only one usage of each socket address (protocol/network address/port) is normally permitted 10.5.50.2:443
at System.Net.Sockets.Socket.EndConnect(IAsyncResult asyncResult)
at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Exception& exception)
--- End of inner exception stack trace ---
at System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
at System.Net.Http.HttpClientHandler.GetResponseCallback(IAsyncResult ar)
--- End of inner exception stack trace ---<---

基本上,我似乎很难按一下此代码:
public void AttemptToDownload(FileToDownload toDownload, int numRetry)
{
try
{
var responseResult = client.GetAsync(SpPrefetchIndexBuilder.topParentSite + toDownload.serverRelativeUrl);
if (responseResult.Result != null && responseResult.Result.StatusCode == System.Net.HttpStatusCode.OK)
{
using (var memStream = responseResult.Result.Content.ReadAsStreamAsync().GetAwaiter().GetResult())
{
using (var fileStream = File.Create(toDownload.saveToPath))
{
memStream.CopyTo(fileStream);
}
}
Console.WriteLine("Thread {0} - Successfully downloaded {1} to {2}", Thread.CurrentThread.ManagedThreadId, toDownload.serverRelativeUrl, toDownload.saveToPath);
}
else
{
Console.WriteLine("Got non-OK status {0} when trying to download url {1}", responseResult.Result.StatusCode, SpPrefetchIndexBuilder.topParentSite + toDownload.serverRelativeUrl);
}
}
catch (Exception e)
{
if (numRetry >= NUM_RETRIES)
{
Console.WriteLine("Gave up trying to download url {0} to file {1} after {2} retries due to error: {3}", SpPrefetchIndexBuilder.topParentSite + toDownload.serverRelativeUrl, toDownload.saveToPath, NUM_RETRIES, e);
}
else
{
AttemptToDownload(toDownload, numRetry + 1);
}
}
}

我使用HttpClient的方式有问题吗?我阅读了所有论坛,并说要重复使用静态引用,而我正在这样做。我所有的线程共享相同的静态HttpClient引用。

这是下载运行了几分钟后的 netstat -a -o -n。那里坐着很多TIMED_WAIT。 https://pastebin.com/GTYmqwue在此测试中,我在端口80上使用SharePoint。为什么?

几分钟后再次运行时,TIMED_WAIT的数量增加了数百倍。一定存在某种泄漏吗?

为什么HttpClient离开那里坐着1000个TIMED_WAIT连接?如何关闭它们?

我尝试设置 ServiePointManager.DefaultConnectionLimit = numThreads,但它仍然增长到1000个连接。

最佳答案

我想我终于想通了

我不小心遇到了一个攻击性很强的client.Timeout = TimeSpan.FromMinutes(5);
我将其更改为client.Timeout = TimeSpan.FromSeconds(15);
现在,我认为连接不再泄漏。

关于c# - 在多个线程中重复使用单个HttpClient时,套接字用完了-仍然有1000个TIMED_WAIT连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47682497/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com