Merge pull request #3756 from MohammadHadi2031/dev/ma/perf

use WaitAsync for Timeout
pull/3773/head
2dust 2023-04-25 10:03:40 +08:00 committed by GitHub
commit f747416d3b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 17 deletions

View File

@ -16,9 +16,6 @@ namespace v2rayN.Base
return null; return null;
} }
var cancellationToken = new CancellationTokenSource();
cancellationToken.CancelAfter(timeout * 1000);
Uri uri = new(url); Uri uri = new(url);
//Authorization Header //Authorization Header
var headers = new WebHeaderCollection(); var headers = new WebHeaderCollection();
@ -48,7 +45,9 @@ namespace v2rayN.Base
throw value.Error; throw value.Error;
} }
}; };
using var stream = await downloader.DownloadFileTaskAsync(address: url, cancellationToken: cancellationToken.Token);
using var cts = new CancellationTokenSource();
using var stream = await downloader.DownloadFileTaskAsync(address: url, cts.Token).WaitAsync(TimeSpan.FromSeconds(timeout), cts.Token);
using StreamReader reader = new(stream); using StreamReader reader = new(stream);
downloadOpt = null; downloadOpt = null;
@ -63,9 +62,6 @@ namespace v2rayN.Base
throw new ArgumentNullException(nameof(url)); throw new ArgumentNullException(nameof(url));
} }
var cancellationToken = new CancellationTokenSource();
cancellationToken.CancelAfter(timeout * 1000);
var downloadOpt = new DownloadConfiguration() var downloadOpt = new DownloadConfiguration()
{ {
Timeout = timeout * 1000, Timeout = timeout * 1000,
@ -115,8 +111,8 @@ namespace v2rayN.Base
} }
}; };
//progress.Report("......"); //progress.Report("......");
using var cts = new CancellationTokenSource();
using var stream = await downloader.DownloadFileTaskAsync(address: url, cancellationToken: cancellationToken.Token); using var stream = await downloader.DownloadFileTaskAsync(address: url, cts.Token).WaitAsync(TimeSpan.FromSeconds(timeout), cts.Token);
downloadOpt = null; downloadOpt = null;
} }
@ -136,9 +132,6 @@ namespace v2rayN.Base
File.Delete(fileName); File.Delete(fileName);
} }
var cancellationToken = new CancellationTokenSource();
cancellationToken.CancelAfter(timeout * 1000);
var downloadOpt = new DownloadConfiguration() var downloadOpt = new DownloadConfiguration()
{ {
Timeout = timeout * 1000, Timeout = timeout * 1000,
@ -178,7 +171,8 @@ namespace v2rayN.Base
} }
}; };
await downloader.DownloadFileTaskAsync(url, fileName, cancellationToken: cancellationToken.Token); using var cts = new CancellationTokenSource();
await downloader.DownloadFileTaskAsync(url, fileName, cts.Token).WaitAsync(TimeSpan.FromSeconds(timeout), cts.Token);
downloadOpt = null; downloadOpt = null;
} }

View File

@ -4,6 +4,7 @@ using System.Net;
using System.Net.Http; using System.Net.Http;
using System.Net.Http.Headers; using System.Net.Http.Headers;
using System.Net.Sockets; using System.Net.Sockets;
using System.Threading;
using v2rayN.Base; using v2rayN.Base;
using v2rayN.Resx; using v2rayN.Resx;
@ -206,10 +207,8 @@ namespace v2rayN.Handler
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Utils.Base64Encode(uri.UserInfo)); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Utils.Base64Encode(uri.UserInfo));
} }
var cts = new CancellationTokenSource(); using var cts = new CancellationTokenSource();
cts.CancelAfter(1000 * 30); var result = await HttpClientHelper.Instance.GetAsync(client, url, cts.Token).WaitAsync(TimeSpan.FromSeconds(30), cts.Token);
var result = await HttpClientHelper.Instance.GetAsync(client, url, cts.Token);
return result; return result;
} }
catch (Exception ex) catch (Exception ex)