wait for all incoming calls in CachedFileProvider
This commit is contained in:
@@ -27,10 +27,11 @@ public class CachedFileProvider
|
|||||||
public async Task<FileStream?> GetFileStream(string hash, string auth)
|
public async Task<FileStream?> GetFileStream(string hash, string auth)
|
||||||
{
|
{
|
||||||
var fi = FilePathUtil.GetFileInfoForHash(_basePath, hash);
|
var fi = FilePathUtil.GetFileInfoForHash(_basePath, hash);
|
||||||
if (fi == null)
|
bool hasTransferTask = _currentTransfers.TryGetValue(hash, out var transferTask);
|
||||||
|
if (fi == null && !hasTransferTask)
|
||||||
{
|
{
|
||||||
if (IsMainServer) return null;
|
if (IsMainServer) return null;
|
||||||
if (!_currentTransfers.ContainsKey(hash))
|
if (transferTask == null)
|
||||||
{
|
{
|
||||||
_currentTransfers[hash] = Task.Run(async () =>
|
_currentTransfers[hash] = Task.Run(async () =>
|
||||||
{
|
{
|
||||||
@@ -52,9 +53,7 @@ public class CachedFileProvider
|
|||||||
}
|
}
|
||||||
|
|
||||||
var fileName = FilePathUtil.GetFilePath(_basePath, hash);
|
var fileName = FilePathUtil.GetFilePath(_basePath, hash);
|
||||||
var fileStream = File.Create(fileName);
|
using var fileStream = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.ReadWrite);
|
||||||
await using (fileStream.ConfigureAwait(false))
|
|
||||||
{
|
|
||||||
var bufferSize = response.Content.Headers.ContentLength > 1024 * 1024 ? 4096 : 1024;
|
var bufferSize = response.Content.Headers.ContentLength > 1024 * 1024 ? 4096 : 1024;
|
||||||
var buffer = new byte[bufferSize];
|
var buffer = new byte[bufferSize];
|
||||||
|
|
||||||
@@ -63,18 +62,20 @@ public class CachedFileProvider
|
|||||||
{
|
{
|
||||||
await fileStream.WriteAsync(buffer.AsMemory(0, bytesRead)).ConfigureAwait(false);
|
await fileStream.WriteAsync(buffer.AsMemory(0, bytesRead)).ConfigureAwait(false);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
_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);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
await _currentTransfers[hash].ConfigureAwait(false);
|
transferTask = _currentTransfers[hash];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (transferTask != null)
|
||||||
|
{
|
||||||
|
await transferTask.ConfigureAwait(false);
|
||||||
_currentTransfers.Remove(hash, out _);
|
_currentTransfers.Remove(hash, out _);
|
||||||
|
|
||||||
fi = FilePathUtil.GetFileInfoForHash(_basePath, hash);
|
fi = FilePathUtil.GetFileInfoForHash(_basePath, hash);
|
||||||
|
|
||||||
if (fi == null) return null;
|
if (fi == null) return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -85,7 +86,7 @@ public class CachedFileProvider
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
return new FileStream(fi.FullName, FileMode.Open, FileAccess.Read, FileShare.Read);
|
return new FileStream(fi.FullName, FileMode.Open, FileAccess.Read, FileShare.Inheritable | FileShare.Read);
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user