优化0速度重试逻辑 降低资源占用
This commit is contained in:
parent
b46db566a5
commit
0da2c1424d
|
@ -11,6 +11,7 @@ using System.Collections.Generic;
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace N_m3u8DL_RE.Downloader
|
namespace N_m3u8DL_RE.Downloader
|
||||||
|
@ -55,16 +56,38 @@ namespace N_m3u8DL_RE.Downloader
|
||||||
|
|
||||||
private async Task<DownloadResult?> DownClipAsync(string url, string path, SpeedContainer speedContainer, long? fromPosition, long? toPosition, Dictionary<string, string>? headers = null, int retryCount = 3)
|
private async Task<DownloadResult?> DownClipAsync(string url, string path, SpeedContainer speedContainer, long? fromPosition, long? toPosition, Dictionary<string, string>? headers = null, int retryCount = 3)
|
||||||
{
|
{
|
||||||
|
CancellationTokenSource? cancellationTokenSource = null;
|
||||||
retry:
|
retry:
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
cancellationTokenSource = new();
|
||||||
var des = Path.ChangeExtension(path, null);
|
var des = Path.ChangeExtension(path, null);
|
||||||
|
|
||||||
//已下载过跳过
|
//已下载过跳过
|
||||||
if (File.Exists(des))
|
if (File.Exists(des))
|
||||||
{
|
{
|
||||||
return new DownloadResult() { ActualContentLength = 0, ActualFilePath = des };
|
return new DownloadResult() { ActualContentLength = 0, ActualFilePath = des };
|
||||||
}
|
}
|
||||||
var result = await DownloadUtil.DownloadToFileAsync(url, path, speedContainer, headers, fromPosition, toPosition);
|
|
||||||
|
//另起线程进行监控
|
||||||
|
using var watcher = Task.Factory.StartNew(async () =>
|
||||||
|
{
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
if (cancellationTokenSource == null || cancellationTokenSource.IsCancellationRequested) break;
|
||||||
|
if (speedContainer.ShouldStop)
|
||||||
|
{
|
||||||
|
cancellationTokenSource.Cancel();
|
||||||
|
Logger.DebugMarkUp("Cancel...");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
await Task.Delay(500);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
//调用下载
|
||||||
|
var result = await DownloadUtil.DownloadToFileAsync(url, path, speedContainer, cancellationTokenSource, headers, fromPosition, toPosition);
|
||||||
|
|
||||||
//下载完成后改名
|
//下载完成后改名
|
||||||
if (result.Success || !DownloaderConfig.CheckContentLength)
|
if (result.Success || !DownloaderConfig.CheckContentLength)
|
||||||
{
|
{
|
||||||
|
@ -86,6 +109,15 @@ namespace N_m3u8DL_RE.Downloader
|
||||||
//throw new Exception("download failed", ex);
|
//throw new Exception("download failed", ex);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
if (cancellationTokenSource != null)
|
||||||
|
{
|
||||||
|
//调用后销毁
|
||||||
|
cancellationTokenSource.Dispose();
|
||||||
|
cancellationTokenSource = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -36,7 +36,7 @@ namespace N_m3u8DL_RE.Util
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
public static async Task<DownloadResult> DownloadToFileAsync(string url, string path, SpeedContainer speedContainer, Dictionary<string, string>? headers = null, long? fromPosition = null, long? toPosition = null)
|
public static async Task<DownloadResult> DownloadToFileAsync(string url, string path, SpeedContainer speedContainer, CancellationTokenSource cancellationTokenSource, Dictionary<string, string>? headers = null, long? fromPosition = null, long? toPosition = null)
|
||||||
{
|
{
|
||||||
Logger.Debug(ResString.fetch + url);
|
Logger.Debug(ResString.fetch + url);
|
||||||
if (url.StartsWith("file:"))
|
if (url.StartsWith("file:"))
|
||||||
|
@ -55,21 +55,6 @@ namespace N_m3u8DL_RE.Util
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Logger.Debug(request.Headers.ToString());
|
Logger.Debug(request.Headers.ToString());
|
||||||
CancellationTokenSource cancellationTokenSource = new(); //取消下载
|
|
||||||
using var watcher = Task.Factory.StartNew(async () =>
|
|
||||||
{
|
|
||||||
while (true)
|
|
||||||
{
|
|
||||||
if (speedContainer == null) break;
|
|
||||||
if (speedContainer.ShouldStop)
|
|
||||||
{
|
|
||||||
cancellationTokenSource.Cancel();
|
|
||||||
Logger.DebugMarkUp("Cancel...");
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
await Task.Delay(500);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
using var response = await AppHttpClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, cancellationTokenSource.Token);
|
using var response = await AppHttpClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, cancellationTokenSource.Token);
|
||||||
|
@ -90,7 +75,7 @@ namespace N_m3u8DL_RE.Util
|
||||||
{
|
{
|
||||||
redirectedUrl = respHeaders.Location.AbsoluteUri;
|
redirectedUrl = respHeaders.Location.AbsoluteUri;
|
||||||
}
|
}
|
||||||
return await DownloadToFileAsync(redirectedUrl, path, speedContainer, headers, fromPosition, toPosition);
|
return await DownloadToFileAsync(redirectedUrl, path, speedContainer, cancellationTokenSource, headers, fromPosition, toPosition);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
response.EnsureSuccessStatusCode();
|
response.EnsureSuccessStatusCode();
|
||||||
|
@ -104,7 +89,7 @@ namespace N_m3u8DL_RE.Util
|
||||||
while ((size = await responseStream.ReadAsync(buffer, cancellationTokenSource.Token)) > 0)
|
while ((size = await responseStream.ReadAsync(buffer, cancellationTokenSource.Token)) > 0)
|
||||||
{
|
{
|
||||||
speedContainer.Add(size);
|
speedContainer.Add(size);
|
||||||
await stream.WriteAsync(buffer, 0, size, cancellationTokenSource.Token);
|
await stream.WriteAsync(buffer, 0, size);
|
||||||
}
|
}
|
||||||
|
|
||||||
return new DownloadResult()
|
return new DownloadResult()
|
||||||
|
|
Loading…
Reference in New Issue