mirror of
https://github.com/Caraxi/mare.server.git
synced 2025-12-12 20:57:22 +01:00
fix upload getting stuck
This commit is contained in:
parent
df87e45e9c
commit
a1573ad2c3
1 changed files with 63 additions and 28 deletions
|
|
@ -165,14 +165,26 @@ public class ServerFilesController : ControllerBase
|
||||||
var existingFile = await _mareDbContext.Files.SingleOrDefaultAsync(f => f.Hash == hash);
|
var existingFile = await _mareDbContext.Files.SingleOrDefaultAsync(f => f.Hash == hash);
|
||||||
if (existingFile != null) return Ok();
|
if (existingFile != null) return Ok();
|
||||||
|
|
||||||
SemaphoreSlim fileLock;
|
SemaphoreSlim? fileLock = null;
|
||||||
lock (_fileUploadLocks)
|
bool successfullyWaited = false;
|
||||||
|
while (!successfullyWaited && !requestAborted.IsCancellationRequested)
|
||||||
{
|
{
|
||||||
if (!_fileUploadLocks.TryGetValue(hash, out fileLock))
|
lock (_fileUploadLocks)
|
||||||
_fileUploadLocks[hash] = fileLock = new SemaphoreSlim(1);
|
{
|
||||||
}
|
if (!_fileUploadLocks.TryGetValue(hash, out fileLock))
|
||||||
|
_fileUploadLocks[hash] = fileLock = new SemaphoreSlim(1);
|
||||||
|
}
|
||||||
|
|
||||||
await fileLock.WaitAsync(requestAborted).ConfigureAwait(false);
|
try
|
||||||
|
{
|
||||||
|
await fileLock.WaitAsync(requestAborted).ConfigureAwait(false);
|
||||||
|
successfullyWaited = true;
|
||||||
|
}
|
||||||
|
catch (ObjectDisposedException)
|
||||||
|
{
|
||||||
|
_logger.LogWarning("Semaphore disposed for {hash}, recreating", hash);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
|
@ -228,8 +240,9 @@ public class ServerFilesController : ControllerBase
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
fileLock.Release();
|
fileLock?.Release();
|
||||||
fileLock.Dispose();
|
fileLock?.Dispose();
|
||||||
|
_fileUploadLocks.TryRemove(hash, out _);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -242,14 +255,26 @@ public class ServerFilesController : ControllerBase
|
||||||
var existingFile = await _mareDbContext.Files.SingleOrDefaultAsync(f => f.Hash == hash);
|
var existingFile = await _mareDbContext.Files.SingleOrDefaultAsync(f => f.Hash == hash);
|
||||||
if (existingFile != null) return Ok();
|
if (existingFile != null) return Ok();
|
||||||
|
|
||||||
SemaphoreSlim fileLock;
|
SemaphoreSlim? fileLock = null;
|
||||||
lock (_fileUploadLocks)
|
bool successfullyWaited = false;
|
||||||
|
while (!successfullyWaited && !requestAborted.IsCancellationRequested)
|
||||||
{
|
{
|
||||||
if (!_fileUploadLocks.TryGetValue(hash, out fileLock))
|
lock (_fileUploadLocks)
|
||||||
_fileUploadLocks[hash] = fileLock = new SemaphoreSlim(1);
|
{
|
||||||
}
|
if (!_fileUploadLocks.TryGetValue(hash, out fileLock))
|
||||||
|
_fileUploadLocks[hash] = fileLock = new SemaphoreSlim(1);
|
||||||
|
}
|
||||||
|
|
||||||
await fileLock.WaitAsync(requestAborted).ConfigureAwait(false);
|
try
|
||||||
|
{
|
||||||
|
await fileLock.WaitAsync(requestAborted).ConfigureAwait(false);
|
||||||
|
successfullyWaited = true;
|
||||||
|
}
|
||||||
|
catch (ObjectDisposedException)
|
||||||
|
{
|
||||||
|
_logger.LogWarning("Semaphore disposed for {hash}, recreating", hash);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
|
@ -293,8 +318,6 @@ public class ServerFilesController : ControllerBase
|
||||||
_metricsClient.IncGauge(MetricsAPI.GaugeFilesTotal, 1);
|
_metricsClient.IncGauge(MetricsAPI.GaugeFilesTotal, 1);
|
||||||
_metricsClient.IncGauge(MetricsAPI.GaugeFilesTotalSize, compressedMungedStream.Length);
|
_metricsClient.IncGauge(MetricsAPI.GaugeFilesTotalSize, compressedMungedStream.Length);
|
||||||
|
|
||||||
_fileUploadLocks.TryRemove(hash, out _);
|
|
||||||
|
|
||||||
return Ok();
|
return Ok();
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
|
|
@ -304,8 +327,9 @@ public class ServerFilesController : ControllerBase
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
fileLock.Release();
|
fileLock?.Release();
|
||||||
fileLock.Dispose();
|
fileLock?.Dispose();
|
||||||
|
_fileUploadLocks.TryRemove(hash, out _);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -326,14 +350,26 @@ public class ServerFilesController : ControllerBase
|
||||||
var existingFile = await _mareDbContext.Files.SingleOrDefaultAsync(f => f.Hash == hash);
|
var existingFile = await _mareDbContext.Files.SingleOrDefaultAsync(f => f.Hash == hash);
|
||||||
if (existingFile != null) return Ok();
|
if (existingFile != null) return Ok();
|
||||||
|
|
||||||
SemaphoreSlim fileLock;
|
SemaphoreSlim? fileLock = null;
|
||||||
lock (_fileUploadLocks)
|
bool successfullyWaited = false;
|
||||||
|
while (!successfullyWaited && !requestAborted.IsCancellationRequested)
|
||||||
{
|
{
|
||||||
if (!_fileUploadLocks.TryGetValue(hash, out fileLock))
|
lock (_fileUploadLocks)
|
||||||
_fileUploadLocks[hash] = fileLock = new SemaphoreSlim(1);
|
{
|
||||||
}
|
if (!_fileUploadLocks.TryGetValue(hash, out fileLock))
|
||||||
|
_fileUploadLocks[hash] = fileLock = new SemaphoreSlim(1);
|
||||||
|
}
|
||||||
|
|
||||||
await fileLock.WaitAsync(requestAborted).ConfigureAwait(false);
|
try
|
||||||
|
{
|
||||||
|
await fileLock.WaitAsync(requestAborted).ConfigureAwait(false);
|
||||||
|
successfullyWaited = true;
|
||||||
|
}
|
||||||
|
catch (ObjectDisposedException)
|
||||||
|
{
|
||||||
|
_logger.LogWarning("Semaphore disposed for {hash}, recreating", hash);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
|
@ -377,8 +413,6 @@ public class ServerFilesController : ControllerBase
|
||||||
_metricsClient.IncGauge(MetricsAPI.GaugeFilesTotal, 1);
|
_metricsClient.IncGauge(MetricsAPI.GaugeFilesTotal, 1);
|
||||||
_metricsClient.IncGauge(MetricsAPI.GaugeFilesTotalSize, rawFileStream.Length);
|
_metricsClient.IncGauge(MetricsAPI.GaugeFilesTotalSize, rawFileStream.Length);
|
||||||
|
|
||||||
_fileUploadLocks.TryRemove(hash, out _);
|
|
||||||
|
|
||||||
return Ok();
|
return Ok();
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
|
|
@ -388,8 +422,9 @@ public class ServerFilesController : ControllerBase
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
fileLock.Release();
|
fileLock?.Release();
|
||||||
fileLock.Dispose();
|
fileLock?.Dispose();
|
||||||
|
_fileUploadLocks.TryRemove(hash, out _);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue