some fixes maybe

This commit is contained in:
rootdarkarchon
2024-02-22 12:36:37 +01:00
committed by Loporrit
parent 9d9573a510
commit 9b9f6cd2f9
2 changed files with 12 additions and 4 deletions

View File

@@ -18,7 +18,7 @@ public sealed class CachedFileProvider : IDisposable
private readonly string _basePath; private readonly string _basePath;
private readonly ConcurrentDictionary<string, Task> _currentTransfers = new(StringComparer.Ordinal); private readonly ConcurrentDictionary<string, Task> _currentTransfers = new(StringComparer.Ordinal);
private readonly HttpClient _httpClient; private readonly HttpClient _httpClient;
private readonly SemaphoreSlim _downloadSemaphore = new(1); private readonly SemaphoreSlim _downloadSemaphore = new(1, 1);
private bool _disposed; private bool _disposed;
private bool IsMainServer => _remoteCacheSourceUri == null && _isDistributionServer; private bool IsMainServer => _remoteCacheSourceUri == null && _isDistributionServer;
@@ -56,15 +56,17 @@ public sealed class CachedFileProvider : IDisposable
using var requestMessage = new HttpRequestMessage(HttpMethod.Get, downloadUrl); using var requestMessage = new HttpRequestMessage(HttpMethod.Get, downloadUrl);
requestMessage.Headers.Authorization = new AuthenticationHeaderValue("Bearer", _generator.Token); requestMessage.Headers.Authorization = new AuthenticationHeaderValue("Bearer", _generator.Token);
using var response = await _httpClient.SendAsync(requestMessage).ConfigureAwait(false); HttpResponseMessage? response = null;
try try
{ {
response = await _httpClient.SendAsync(requestMessage).ConfigureAwait(false);
response.EnsureSuccessStatusCode(); response.EnsureSuccessStatusCode();
} }
catch (Exception ex) catch (Exception ex)
{ {
_logger.LogWarning(ex, "Failed to download {url}", downloadUrl); _logger.LogWarning(ex, "Failed to download {url}", downloadUrl);
response?.Dispose();
return; return;
} }
@@ -86,6 +88,7 @@ public sealed class CachedFileProvider : IDisposable
_metrics.IncGauge(MetricsAPI.GaugeFilesTotal); _metrics.IncGauge(MetricsAPI.GaugeFilesTotal);
_metrics.IncGauge(MetricsAPI.GaugeFilesTotalSize, FilePathUtil.GetFileInfoForHash(_basePath, hash).Length); _metrics.IncGauge(MetricsAPI.GaugeFilesTotalSize, FilePathUtil.GetFileInfoForHash(_basePath, hash).Length);
response.Dispose();
} }
public async Task DownloadFileWhenRequired(string hash) public async Task DownloadFileWhenRequired(string hash)
@@ -94,7 +97,9 @@ public sealed class CachedFileProvider : IDisposable
if (fi == null && IsMainServer) return; if (fi == null && IsMainServer) return;
await _downloadSemaphore.WaitAsync().ConfigureAwait(false); 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 () => _currentTransfers[hash] = Task.Run(async () =>
{ {

View File

@@ -83,6 +83,7 @@ public class ShardFileCleanupService : IHostedService
{ {
_logger.LogInformation("Cleaning up files beyond the cache size limit of {cacheSizeLimit} GiB", sizeLimit); _logger.LogInformation("Cleaning up files beyond the cache size limit of {cacheSizeLimit} GiB", sizeLimit);
var allLocalFiles = Directory.EnumerateFiles(_cacheDir, "*", SearchOption.AllDirectories) var allLocalFiles = Directory.EnumerateFiles(_cacheDir, "*", SearchOption.AllDirectories)
.Where(f => !f.EndsWith("dl", StringComparison.OrdinalIgnoreCase))
.Select(f => new FileInfo(f)).ToList() .Select(f => new FileInfo(f)).ToList()
.OrderBy(f => f.LastAccessTimeUtc).ToList(); .OrderBy(f => f.LastAccessTimeUtc).ToList();
var totalCacheSizeInBytes = allLocalFiles.Sum(s => s.Length); var totalCacheSizeInBytes = allLocalFiles.Sum(s => s.Length);
@@ -120,7 +121,9 @@ public class ShardFileCleanupService : IHostedService
var prevTime = DateTime.Now.Subtract(TimeSpan.FromDays(unusedRetention)); var prevTime = DateTime.Now.Subtract(TimeSpan.FromDays(unusedRetention));
var prevTimeForcedDeletion = DateTime.Now.Subtract(TimeSpan.FromHours(forcedDeletionAfterHours)); var prevTimeForcedDeletion = DateTime.Now.Subtract(TimeSpan.FromHours(forcedDeletionAfterHours));
DirectoryInfo dir = new(_cacheDir); 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) foreach (var file in allFilesInDir)
{ {