limit parallel uploads to 20 per server

This commit is contained in:
rootdarkarchon
2022-12-22 23:19:46 +01:00
parent 3756fef498
commit 8f35a55bd8

View File

@@ -4,6 +4,7 @@ using System.IO;
using System.Linq; using System.Linq;
using System.Security.Claims; using System.Security.Claims;
using System.Security.Cryptography; using System.Security.Cryptography;
using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Google.Protobuf; using Google.Protobuf;
using Grpc.Core; using Grpc.Core;
@@ -18,6 +19,8 @@ namespace MareSynchronosServer.Hubs;
public partial class MareHub public partial class MareHub
{ {
private static readonly SemaphoreSlim _uploadSemaphore = new(20);
[Authorize(Policy = "Identified")] [Authorize(Policy = "Identified")]
public async Task FilesAbortUpload() public async Task FilesAbortUpload()
{ {
@@ -144,10 +147,20 @@ public partial class MareHub
{ {
_logger.LogCallInfo(MareHubLogger.Args(hash)); _logger.LogCallInfo(MareHubLogger.Args(hash));
await _uploadSemaphore.WaitAsync(Context.ConnectionAborted).ConfigureAwait(false);
var relatedFile = _dbContext.Files.SingleOrDefault(f => f.Hash == hash && f.Uploader.UID == AuthenticatedUserId && !f.Uploaded); var relatedFile = _dbContext.Files.SingleOrDefault(f => f.Hash == hash && f.Uploader.UID == AuthenticatedUserId && !f.Uploaded);
if (relatedFile == null) return; if (relatedFile == null)
{
_uploadSemaphore.Release();
return;
}
var forbiddenFile = _dbContext.ForbiddenUploadEntries.SingleOrDefault(f => f.Hash == hash); var forbiddenFile = _dbContext.ForbiddenUploadEntries.SingleOrDefault(f => f.Hash == hash);
if (forbiddenFile != null) return; if (forbiddenFile != null)
{
_uploadSemaphore.Release();
return;
}
var tempFileName = Path.GetTempFileName(); var tempFileName = Path.GetTempFileName();
using var fileStream = new FileStream(tempFileName, FileMode.OpenOrCreate); using var fileStream = new FileStream(tempFileName, FileMode.OpenOrCreate);
@@ -181,6 +194,7 @@ public partial class MareHub
File.Delete(tempFileName); File.Delete(tempFileName);
} }
_uploadSemaphore.Release();
return; return;
} }
@@ -199,6 +213,7 @@ public partial class MareHub
_dbContext.Remove(relatedFile); _dbContext.Remove(relatedFile);
await _dbContext.SaveChangesAsync().ConfigureAwait(false); await _dbContext.SaveChangesAsync().ConfigureAwait(false);
_uploadSemaphore.Release();
return; return;
} }
@@ -234,6 +249,7 @@ public partial class MareHub
} }
finally finally
{ {
_uploadSemaphore.Release();
File.Delete(tempFileName); File.Delete(tempFileName);
} }
} }