optimize for readability
This commit is contained in:
@@ -24,16 +24,7 @@ public class CachedFileProvider
|
|||||||
_basePath = configuration.GetValue<string>(nameof(StaticFilesServerConfiguration.CacheDirectory));
|
_basePath = configuration.GetValue<string>(nameof(StaticFilesServerConfiguration.CacheDirectory));
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<FileStream?> GetFileStream(string hash, string auth)
|
private async Task DownloadTask(string hash, string auth)
|
||||||
{
|
|
||||||
var fi = FilePathUtil.GetFileInfoForHash(_basePath, hash);
|
|
||||||
bool hasTransferTask = _currentTransfers.TryGetValue(hash, out var transferTask);
|
|
||||||
if (fi == null && !hasTransferTask)
|
|
||||||
{
|
|
||||||
if (IsMainServer) return null;
|
|
||||||
if (transferTask == null)
|
|
||||||
{
|
|
||||||
_currentTransfers[hash] = Task.Run(async () =>
|
|
||||||
{
|
{
|
||||||
// download file from remote
|
// download file from remote
|
||||||
var downloadUrl = new Uri(_remoteCacheSourceUri, hash);
|
var downloadUrl = new Uri(_remoteCacheSourceUri, hash);
|
||||||
@@ -65,37 +56,28 @@ public class CachedFileProvider
|
|||||||
|
|
||||||
_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);
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
transferTask = _currentTransfers[hash];
|
public async Task<FileStream?> GetFileStream(string hash, string auth)
|
||||||
}
|
|
||||||
|
|
||||||
if (transferTask != null)
|
|
||||||
{
|
{
|
||||||
await transferTask.ConfigureAwait(false);
|
var fi = FilePathUtil.GetFileInfoForHash(_basePath, hash);
|
||||||
_currentTransfers.Remove(hash, out _);
|
if (fi == null && IsMainServer) return null;
|
||||||
|
|
||||||
|
if (fi == null && !_currentTransfers.ContainsKey(hash))
|
||||||
|
{
|
||||||
|
_currentTransfers[hash] = DownloadTask(hash, auth).ContinueWith(r => _currentTransfers.Remove(hash, out _));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (fi == null && _currentTransfers.TryGetValue(hash, out var downloadTask))
|
||||||
|
{
|
||||||
|
await downloadTask.ConfigureAwait(false);
|
||||||
|
}
|
||||||
|
|
||||||
fi = FilePathUtil.GetFileInfoForHash(_basePath, hash);
|
fi = FilePathUtil.GetFileInfoForHash(_basePath, hash);
|
||||||
if (fi == null) return null;
|
if (fi == null) return null;
|
||||||
}
|
|
||||||
|
|
||||||
_fileStatisticsService.LogFile(hash, fi.Length);
|
_fileStatisticsService.LogFile(hash, fi.Length);
|
||||||
|
|
||||||
int attempts = 0;
|
|
||||||
while (attempts < 5)
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
return new FileStream(fi.FullName, FileMode.Open, FileAccess.Read, FileShare.Inheritable | FileShare.Read);
|
return new FileStream(fi.FullName, FileMode.Open, FileAccess.Read, FileShare.Inheritable | FileShare.Read);
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
attempts++;
|
|
||||||
_logger.LogWarning(ex, "Error opening file, retrying");
|
|
||||||
await Task.Delay(TimeSpan.FromSeconds(1)).ConfigureAwait(false);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
throw new IOException("Could not open file " + fi.FullName);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user