Skip to content

Commit

Permalink
fix: 修复下载文件时内存占用过大的 bug
Browse files Browse the repository at this point in the history
  • Loading branch information
SALTWOOD committed May 26, 2024
1 parent ea9ece9 commit 0cf9c74
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
25 changes: 15 additions & 10 deletions CSharp-OpenBMCLAPI/Modules/Cluster.cs
Original file line number Diff line number Diff line change
Expand Up @@ -453,16 +453,26 @@ protected async Task CheckFiles(bool skipCheck, FileVerificationMode mode)
{
while (true)
{
source.Token.ThrowIfCancellationRequested();
int threadsTotal = this.requiredData.maxThreadCount;
int threadsUsed = threadsTotal - this.requiredData.SemaphoreSlim.CurrentCount;
source.Token.ThrowIfCancellationRequested();
downloadProgress.Clear();
downloadProgress.Tick("Progress", count, files.Count, ConsoleColor.Blue);
downloadProgress.Tick("Threads", threadsUsed, threadsTotal, ConsoleColor.Blue);
Thread.Sleep(100);
}
}, source.Token);

_ = Task.Run(() =>
{
while (true)
{
downloadMessage.Clear();
source.Token.ThrowIfCancellationRequested();
Thread.Sleep(10000);
}
}, source.Token);

Parallel.ForEach(files, file =>
//foreach (var file in files)
{
Expand All @@ -471,12 +481,6 @@ protected async Task CheckFiles(bool skipCheck, FileVerificationMode mode)
{
count++;
}

//Logger.Instance.LogInfoNoNewLine(
// $"\r[{new string('=', (progressBarWidth * (count * 100 / files.Count) / 100)).PadRight(progressBarWidth, ' ')}]" +
// $"{count}/{files.Count}, Threads: " +
// $"{ClusterRequiredData.Config.downloadFileThreads - this.requiredData.SemaphoreSlim.CurrentCount}/" +
// $"{ClusterRequiredData.Config.downloadFileThreads}");
});

source.Cancel();
Expand All @@ -493,7 +497,7 @@ public async Task<List<ApiFileInfo>> GetFileList()
{
HttpResponseMessage resp;
resp = await this.client.GetAsync("openbmclapi/files");
Logger.Instance.LogDebug($"检查文件结果:{resp}");
Logger.Instance.LogDebug($"检查文件结果:{resp.StatusCode}");

List<ApiFileInfo> files;

Expand Down Expand Up @@ -624,12 +628,13 @@ private async Task DownloadFile(string hash, string path, bool force = false, IC
try
{
var resp = await this.client.GetAsync($"openbmclapi/download/{hash}");
this.storage.WriteFile(Utils.HashToFileName(hash), await resp.Content.ReadAsByteArrayAsync());
this.storage.WriteFileStream(Utils.HashToFileName(hash), await resp.Content.ReadAsStreamAsync());
resp = null!;
console?.WriteLine($"文件 {path} 下载成功");
}
catch (Exception ex)
{
console?.WriteLine(ConsoleColor.Red, $"文件 {path} 下载失败:{ex.ExceptionToDetail()}");
Logger.Instance.LogError($"文件 {path} 下载失败:{ex.ExceptionToDetail()}");
}
finally
{
Expand Down
1 change: 0 additions & 1 deletion CSharp-OpenBMCLAPI/Modules/ExtensionMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ public static void Tick(this IConsole console, string symbol, int value, int tot
console.WriteLine(color, $" ({value}/{total}, {value * 100 / total:0.00}%)");
}


/// <summary>
/// 最便捷的方式,保证 null 值不会被使用
/// </summary>
Expand Down

0 comments on commit 0cf9c74

Please sign in to comment.