mirror of
https://github.com/2dust/v2rayN.git
synced 2025-11-29 03:02:53 +00:00
Fix: High latency in tcping test due to thread blocking
This commit is contained in:
parent
693afe3560
commit
6d15ddc0a4
1 changed files with 6 additions and 7 deletions
|
|
@ -335,15 +335,14 @@ public class SpeedtestService(Config config, Func<SpeedTestResult, Task> updateF
|
|||
using Socket clientSocket = new(endPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
|
||||
|
||||
var timer = Stopwatch.StartNew();
|
||||
var result = clientSocket.BeginConnect(endPoint, null, null);
|
||||
if (!result.AsyncWaitHandle.WaitOne(TimeSpan.FromSeconds(5)))
|
||||
{
|
||||
throw new TimeoutException("connect timeout (5s): " + url);
|
||||
}
|
||||
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(5));
|
||||
await clientSocket.ConnectAsync(endPoint, cts.Token).ConfigureAwait(false);
|
||||
timer.Stop();
|
||||
responseTime = (int)timer.Elapsed.TotalMilliseconds;
|
||||
|
||||
clientSocket.EndConnect(result);
|
||||
}
|
||||
catch (OperationCanceledException)
|
||||
{
|
||||
// 超时情况,responseTime保持为-1
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
|
|
|||
Loading…
Reference in a new issue