From f2a4f2a403d065e8a3d5c76e3dbe2d2b23a0b13b Mon Sep 17 00:00:00 2001 From: "Alex \"mcmonkey\" Goodwin" Date: Wed, 3 Jul 2024 19:43:16 -0700 Subject: [PATCH] chunking to avoid bad drive calls for #44 --- src/Utils/Utilities.cs | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/src/Utils/Utilities.cs b/src/Utils/Utilities.cs index bd3d927c..d2c3bb72 100644 --- a/src/Utils/Utilities.cs +++ b/src/Utils/Utilities.cs @@ -519,11 +519,7 @@ public static async Task DownloadFile(string url, string filepath, Action chunks = new(); - long progress = 0; - long startTime = Environment.TickCount64; - long lastUpdate = startTime; if (response.StatusCode != HttpStatusCode.OK) { throw new InvalidOperationException($"Failed to download {altUrl}: got response code {(int)response.StatusCode} {response.StatusCode}"); @@ -532,19 +528,36 @@ public static async Task DownloadFile(string url, string filepath, Action { + byte[] buffer = new byte[Math.Min(length + 1024, 1024 * 1024 * 64)]; // up to 64 megabytes, just grab as big a chunk as we can at a time + int nextOffset = 0; while (true) { - int read = await dlStream.ReadAsync(buffer, Program.GlobalProgramCancel); + int read = await dlStream.ReadAsync(buffer.AsMemory(nextOffset), Program.GlobalProgramCancel); if (read <= 0) { + if (nextOffset > 0) + { + chunks.Enqueue(buffer[..nextOffset]); + } chunks.Enqueue(null); return; } - chunks.Enqueue(buffer[..read]); + if (nextOffset + read < 1024 * 1024 * 5) + { + nextOffset += read; + } + else + { + chunks.Enqueue(buffer[..(nextOffset + read)]); + nextOffset = 0; + } } }); Task saveChunks = Task.Run(async () => { + long progress = 0; + long startTime = Environment.TickCount64; + long lastUpdate = startTime; while (true) { if (chunks.TryDequeue(out byte[] chunk))