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
+17 -7
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;
}
@@ -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());