From 039e3603ceaa5a1d3c944ad7cc3e291e27cb8b48 Mon Sep 17 00:00:00 2001 From: UrbanCMC Date: Sun, 20 Aug 2023 12:58:43 +0200 Subject: [PATCH] Use performance settings to limit maximum memory per download --- Wabbajack.Networking.Http/ResumableDownloader.cs | 7 ++++++- Wabbajack.Networking.Http/SingleThreadedDownloader.cs | 7 +++++-- Wabbajack.Networking.Http/Wabbajack.Networking.Http.csproj | 2 +- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/Wabbajack.Networking.Http/ResumableDownloader.cs b/Wabbajack.Networking.Http/ResumableDownloader.cs index ca07712c..025715e4 100644 --- a/Wabbajack.Networking.Http/ResumableDownloader.cs +++ b/Wabbajack.Networking.Http/ResumableDownloader.cs @@ -7,6 +7,7 @@ using System.Threading; using System.Threading.Tasks; using Downloader; using Microsoft.Extensions.Logging; +using Wabbajack.Configuration; using Wabbajack.Hashing.xxHash64; using Wabbajack.Paths; using Wabbajack.Paths.IO; @@ -20,17 +21,19 @@ internal class ResumableDownloader private readonly HttpRequestMessage _msg; private readonly AbsolutePath _outputPath; private readonly AbsolutePath _packagePath; + private readonly PerformanceSettings _performanceSettings; private readonly ILogger _logger; private CancellationToken _token; private Exception? _error; - public ResumableDownloader(HttpRequestMessage msg, AbsolutePath outputPath, IJob job, ILogger logger) + public ResumableDownloader(HttpRequestMessage msg, AbsolutePath outputPath, IJob job, PerformanceSettings performanceSettings, ILogger logger) { _job = job; _msg = msg; _outputPath = outputPath; _packagePath = outputPath.WithExtension(Extension.FromPath(".download_package")); + _performanceSettings = performanceSettings; _logger = logger; } @@ -99,8 +102,10 @@ internal class ResumableDownloader private DownloadConfiguration CreateConfiguration(HttpRequestMessage message) { + var maximumMemoryPerDownloadThreadMb = Math.Max(0, _performanceSettings.MaximumMemoryPerDownloadThreadMb); var configuration = new DownloadConfiguration { + MaximumMemoryBufferBytes = maximumMemoryPerDownloadThreadMb * 1024 * 1024, Timeout = (int)TimeSpan.FromSeconds(120).TotalMilliseconds, ReserveStorageSpaceBeforeStartingDownload = true, RequestConfiguration = new RequestConfiguration diff --git a/Wabbajack.Networking.Http/SingleThreadedDownloader.cs b/Wabbajack.Networking.Http/SingleThreadedDownloader.cs index 4790d4ac..ed88e3bb 100644 --- a/Wabbajack.Networking.Http/SingleThreadedDownloader.cs +++ b/Wabbajack.Networking.Http/SingleThreadedDownloader.cs @@ -7,6 +7,7 @@ using System.Net.Http.Headers; using System.Threading; using System.Threading.Tasks; using Microsoft.Extensions.Logging; +using Wabbajack.Configuration; using Wabbajack.Hashing.xxHash64; using Wabbajack.Networking.Http.Interfaces; using Wabbajack.Paths; @@ -19,18 +20,20 @@ public class SingleThreadedDownloader : IHttpDownloader { private readonly HttpClient _client; private readonly ILogger _logger; + private readonly PerformanceSettings _settings; - public SingleThreadedDownloader(ILogger logger, HttpClient client) + public SingleThreadedDownloader(ILogger logger, HttpClient client, MainSettings settings) { _logger = logger; _client = client; + _settings = settings.PerformanceSettings; } public async Task Download(HttpRequestMessage message, AbsolutePath outputPath, IJob job, CancellationToken token) { Exception downloadError = null!; - var downloader = new ResumableDownloader(message, outputPath, job, _logger); + var downloader = new ResumableDownloader(message, outputPath, job, _settings, _logger); for (var i = 0; i < 3; i++) { try diff --git a/Wabbajack.Networking.Http/Wabbajack.Networking.Http.csproj b/Wabbajack.Networking.Http/Wabbajack.Networking.Http.csproj index 6bdd0a88..191df7f3 100644 --- a/Wabbajack.Networking.Http/Wabbajack.Networking.Http.csproj +++ b/Wabbajack.Networking.Http/Wabbajack.Networking.Http.csproj @@ -8,7 +8,7 @@ - +