我想將 100000 個 SignalR 用戶連接到 IIS(Windows Server 2016)。一切正常,直到大約 16000 個連接。然后我開始收到此錯誤:
通常每個套接字地址(協議/網路地址/埠)只允許使用一次
這是我的客戶端代碼 - 它是一個創建所有 SignalR 物件并連接到服務器的回圈:
private static void Run()
{
for (int i = 0; i < 100000 ; i )
{
Guid g = Guid.Empty;
string lG = g.ToString();
string lGS = lG.Substring(0, lG.Length - i.ToString().Length);
string lTenantIdentfier = lGS i.ToString();
bool lConnected = false;
SignalRClient sc = new SignalRClient(lTenantIdentfier);
while (!lConnected)
{
sc.Stop();
lConnected = sc.Connect();
if (lConnected)
break;
Console.WriteLine("[" lTenantIdentfier "] - Repeating connection...");
System.Threading.Thread.Sleep(5000);
}
if ((i % 1000) == 0)
Thread.Sleep(5000);
}
Console.ReadKey();
}
連接功能:
public bool Connect()
{
try
{
if (connection != null)
{
Stop();
}
if (connection == null)
{
connection = new HubConnection("my_url");
connection.Headers.Add("TenantIdentifier", TenantIdentifier);
HubProxy = connection.CreateHubProxy("notificationHub");
GetNotification = HubProxy.On("NotifyChange", () =>
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("[" TenantIdentifier "] " DateTime.Now);
});
}
connection.Start().Wait();
if (connection.State == ConnectionState.Connected)
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("[" TenantIdentifier "] - Connected");
connection.Closed -= Connection_Closed;
connection.Closed = Connection_Closed;
return true;
}
}
catch (Exception ex)
{
//Show exception
}
return false;
}
我在 <system.web> 下添加到 machine.config :
<processModel autoConfig="false" maxIoThreads="999999" maxWorkerThreads="999999" memoryLimit="999999" minIoThreads="999999" minWorkerThreads="999999" />
我添加到regedit:
MaxUserPort 65535 TcpTimedWaitDelay 30
我想 IIS 沒有更多的空閑套接字或埠。也許我錯了 - 我可以以某種方式調整它嗎?
uj5u.com熱心網友回復:
正如評論中所討論的,原因似乎是客戶端用完了可用埠。這MaxUserPort對基于Windows 5000的默認值,所以只能從1024到4999埠可用于客戶端連接到服務器。服務器本身不會以這種方式耗盡可用埠,因為所有連接都轉到同一個埠(以通常的網路服務器為例,所有客戶端都連接到埠 80 或埠 443 等,這里的情況相同)。
然后您嘗試從客戶端回圈連接,為每個連接使用新的本地埠。我懷疑您可以連接超過 (4999-1024) 次,因為您沒有明確關閉 signalR 連接,但您也沒有將它們存盤在某種全域串列中,因此它們有資格進行垃圾收集,這會呼叫終結器,從而關閉連接。或者連接已通過其他方式關閉,但速度不夠快,因此有時您會耗盡客戶端上的本地埠,但您擁有的數量可能并不代表并發連接數。
在客戶端上將 MaxUserPort 設定為 65535 和 TcpTimedWaitDelay 30應該允許您從該客戶端建立更多連接。
uj5u.com熱心網友回復:
Evk 是正確的,但這是我在查看用戶連接量時記得讀過的有趣內容。
來自最初的 SignalR 領導者 Damien Edwards,早在 2014 年......是的,這是可能的。這本來是在 ASP.NET 版本 2.x 下的。但是,如果您閱讀了一些回復,Damien 說 .NET CORE 版本應該更高。
https://twitter.com/DamianEdwards/status/486642486350061568
10GB svr 上的新 @SignalR 并發連接記錄:150,000 個連接(WebSockets),帶實驗性緩沖池更改。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/327183.html
上一篇:如何使用LINQ獲取最新號碼
