Optimize timeout logic, try fix

https://github.com/2dust/v2rayN/issues/9681
This commit is contained in:
2dust
2026-07-06 11:49:54 +08:00
parent 0c570fcb38
commit e1a5c360ac
4 changed files with 44 additions and 27 deletions

View File

@@ -38,7 +38,7 @@ public static class ConnectionHandler
for (var i = 0; i < 2; i++)
{
responseTime = await GetRealPingTime(webProxy, 10);
responseTime = await GetRealPingTime(webProxy);
if (responseTime > 0)
{
break;
@@ -66,7 +66,7 @@ public static class ConnectionHandler
/// <summary>
/// Measures response time by sending HTTP requests through proxy.
/// </summary>
public static async Task<int> GetRealPingTime(IWebProxy? webProxy, int downloadTimeout)
public static async Task<int> GetRealPingTime(IWebProxy? webProxy, int downloadTimeout = 9)
{
var url = AppManager.Instance.Config.SpeedTestItem.SpeedPingTestUrl;
var responseTime = -1;
@@ -77,7 +77,8 @@ public static class ConnectionHandler
using var client = new HttpClient(new SocketsHttpHandler()
{
Proxy = webProxy,
UseProxy = webProxy != null
UseProxy = webProxy != null,
ConnectTimeout = TimeSpan.FromSeconds(3)
});
List<int> oneTime = [];

View File

@@ -14,6 +14,8 @@ public class DownloaderHelper
return null;
}
var connectTimeout = Math.Clamp(timeout / 5, 2, 5);
Uri uri = new(url);
//Authorization Header
var headers = new WebHeaderCollection();
@@ -27,12 +29,12 @@ public class DownloaderHelper
BlockTimeout = timeout * 1000,
MaxTryAgainOnFailure = 2,
RequestConfiguration =
{
Headers = headers,
UserAgent = userAgent,
ConnectTimeout = timeout * 1000,
Proxy = webProxy
}
{
Headers = headers,
UserAgent = userAgent,
ConnectTimeout = connectTimeout * 1000,
Proxy = webProxy
}
};
await using var downloader = new Downloader.DownloadService(downloadOpt);
@@ -45,7 +47,9 @@ public class DownloaderHelper
};
using var cts = new CancellationTokenSource();
await using var stream = await downloader.DownloadFileTaskAsync(address: url, cts.Token).WaitAsync(TimeSpan.FromSeconds(timeout), cts.Token);
cts.CancelAfter(TimeSpan.FromSeconds(timeout));
await using var stream = await downloader.DownloadFileTaskAsync(address: url, cts.Token);
using StreamReader reader = new(stream);
downloadOpt = null;
@@ -60,15 +64,16 @@ public class DownloaderHelper
throw new ArgumentNullException(nameof(url));
}
var connectTimeout = Math.Clamp(timeout / 5, 2, 5);
var downloadOpt = new DownloadConfiguration()
{
BlockTimeout = timeout * 1000,
MaxTryAgainOnFailure = 2,
RequestConfiguration =
{
ConnectTimeout= timeout * 1000,
Proxy = webProxy
}
{
ConnectTimeout= connectTimeout * 1000,
Proxy = webProxy
}
};
var lastUpdateTime = DateTime.Now;
@@ -116,7 +121,7 @@ public class DownloaderHelper
};
//progress.Report("......");
using var cts = new CancellationTokenSource();
cts.CancelAfter(timeout * 1000);
cts.CancelAfter(TimeSpan.FromSeconds(timeout));
await using var stream = await downloader.DownloadFileTaskAsync(address: url, cts.Token);
downloadOpt = null;
@@ -137,15 +142,16 @@ public class DownloaderHelper
File.Delete(fileName);
}
var connectTimeout = Math.Clamp(timeout / 5, 2, 5);
var downloadOpt = new DownloadConfiguration()
{
BlockTimeout = timeout * 1000,
MaxTryAgainOnFailure = 2,
RequestConfiguration =
{
ConnectTimeout= timeout * 1000,
Proxy = webProxy
}
{
ConnectTimeout= connectTimeout * 1000,
Proxy = webProxy
}
};
var progressPercentage = 0;

View File

@@ -109,9 +109,10 @@ public class DownloadService
/// </summary>
public async Task<string?> TryDownloadString(string url, IWebProxy? webProxy, string userAgent)
{
var timeout = 15;
try
{
var result1 = await DownloadStringAsync(url, webProxy, userAgent, 15);
var result1 = await DownloadStringAsync(url, webProxy, userAgent, timeout);
if (result1.IsNotEmpty())
{
return result1;
@@ -129,7 +130,7 @@ public class DownloadService
try
{
var result2 = await DownloadStringViaDownloader(url, webProxy, userAgent, 15);
var result2 = await DownloadStringViaDownloader(url, webProxy, userAgent, timeout);
if (result2.IsNotEmpty())
{
return result2;
@@ -155,11 +156,18 @@ public class DownloadService
{
try
{
var client = new HttpClient(new SocketsHttpHandler()
var connectTimeout = Math.Clamp(timeout / 5, 2, 5);
var handler = new SocketsHttpHandler
{
Proxy = webProxy,
UseProxy = webProxy != null
});
UseProxy = webProxy != null,
ConnectTimeout = TimeSpan.FromSeconds(connectTimeout)
};
using var client = new HttpClient(handler)
{
Timeout = Timeout.InfiniteTimeSpan
};
if (userAgent.IsNullOrEmpty())
{
@@ -175,8 +183,9 @@ public class DownloadService
}
using var cts = new CancellationTokenSource();
var result = await client.GetStringAsync(url, cts.Token).WaitAsync(TimeSpan.FromSeconds(timeout), cts.Token);
return result;
cts.CancelAfter(TimeSpan.FromSeconds(timeout));
return await client.GetStringAsync(url, cts.Token);
}
catch (Exception ex)
{
@@ -187,6 +196,7 @@ public class DownloadService
Error?.Invoke(this, new ErrorEventArgs(ex.InnerException));
}
}
return null;
}

View File

@@ -420,7 +420,7 @@ public class SpeedtestService(Config config, Func<SpeedTestResult, Task> updateF
private async Task<int> DoRealPing(ServerTestItem it)
{
var webProxy = new WebProxy($"socks5://{Global.Loopback}:{it.Port}");
var responseTime = await ConnectionHandler.GetRealPingTime(webProxy, 10);
var responseTime = await ConnectionHandler.GetRealPingTime(webProxy);
ProfileExManager.Instance.SetTestDelay(it.IndexId, responseTime);
await UpdateFunc(it.IndexId, responseTime.ToString());