From 9b9f6cd2f91685444c82be3264c4930c8a306dcf Mon Sep 17 00:00:00 2001 From: rootdarkarchon Date: Thu, 22 Feb 2024 12:36:37 +0100 Subject: [PATCH] some fixes maybe --- .../Services/CachedFileProvider.cs | 11 ++++++++--- .../Services/ShardFileCleanupService.cs | 5 ++++- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/MareSynchronosServer/MareSynchronosStaticFilesServer/Services/CachedFileProvider.cs b/MareSynchronosServer/MareSynchronosStaticFilesServer/Services/CachedFileProvider.cs index 289411e..b7cf52e 100644 --- a/MareSynchronosServer/MareSynchronosStaticFilesServer/Services/CachedFileProvider.cs +++ b/MareSynchronosServer/MareSynchronosStaticFilesServer/Services/CachedFileProvider.cs @@ -18,7 +18,7 @@ public sealed class CachedFileProvider : IDisposable private readonly string _basePath; private readonly ConcurrentDictionary _currentTransfers = new(StringComparer.Ordinal); private readonly HttpClient _httpClient; - private readonly SemaphoreSlim _downloadSemaphore = new(1); + private readonly SemaphoreSlim _downloadSemaphore = new(1, 1); private bool _disposed; private bool IsMainServer => _remoteCacheSourceUri == null && _isDistributionServer; @@ -56,15 +56,17 @@ public sealed class CachedFileProvider : IDisposable using var requestMessage = new HttpRequestMessage(HttpMethod.Get, downloadUrl); requestMessage.Headers.Authorization = new AuthenticationHeaderValue("Bearer", _generator.Token); - using var response = await _httpClient.SendAsync(requestMessage).ConfigureAwait(false); + HttpResponseMessage? response = null; try { + response = await _httpClient.SendAsync(requestMessage).ConfigureAwait(false); response.EnsureSuccessStatusCode(); } catch (Exception ex) { _logger.LogWarning(ex, "Failed to download {url}", downloadUrl); + response?.Dispose(); return; } @@ -86,6 +88,7 @@ public sealed class CachedFileProvider : IDisposable _metrics.IncGauge(MetricsAPI.GaugeFilesTotal); _metrics.IncGauge(MetricsAPI.GaugeFilesTotalSize, FilePathUtil.GetFileInfoForHash(_basePath, hash).Length); + response.Dispose(); } public async Task DownloadFileWhenRequired(string hash) @@ -94,7 +97,9 @@ public sealed class CachedFileProvider : IDisposable if (fi == null && IsMainServer) return; await _downloadSemaphore.WaitAsync().ConfigureAwait(false); - if ((fi == null || (fi?.Length ?? 0) == 0) && !_currentTransfers.ContainsKey(hash)) + if ((fi == null || (fi?.Length ?? 0) == 0) + && (!_currentTransfers.TryGetValue(hash, out var downloadTask) + || (downloadTask?.IsCompleted ?? true))) { _currentTransfers[hash] = Task.Run(async () => { diff --git a/MareSynchronosServer/MareSynchronosStaticFilesServer/Services/ShardFileCleanupService.cs b/MareSynchronosServer/MareSynchronosStaticFilesServer/Services/ShardFileCleanupService.cs index 549ff38..8f2d475 100644 --- a/MareSynchronosServer/MareSynchronosStaticFilesServer/Services/ShardFileCleanupService.cs +++ b/MareSynchronosServer/MareSynchronosStaticFilesServer/Services/ShardFileCleanupService.cs @@ -83,6 +83,7 @@ public class ShardFileCleanupService : IHostedService { _logger.LogInformation("Cleaning up files beyond the cache size limit of {cacheSizeLimit} GiB", sizeLimit); var allLocalFiles = Directory.EnumerateFiles(_cacheDir, "*", SearchOption.AllDirectories) + .Where(f => !f.EndsWith("dl", StringComparison.OrdinalIgnoreCase)) .Select(f => new FileInfo(f)).ToList() .OrderBy(f => f.LastAccessTimeUtc).ToList(); var totalCacheSizeInBytes = allLocalFiles.Sum(s => s.Length); @@ -120,7 +121,9 @@ public class ShardFileCleanupService : IHostedService var prevTime = DateTime.Now.Subtract(TimeSpan.FromDays(unusedRetention)); var prevTimeForcedDeletion = DateTime.Now.Subtract(TimeSpan.FromHours(forcedDeletionAfterHours)); DirectoryInfo dir = new(_cacheDir); - var allFilesInDir = dir.GetFiles("*", SearchOption.AllDirectories); + var allFilesInDir = dir.GetFiles("*", SearchOption.AllDirectories) + .Where(f => !f.Name.EndsWith("dl", StringComparison.OrdinalIgnoreCase)) + .ToList(); foreach (var file in allFilesInDir) {