Merge branch 'main' of https://github.com/Penumbra-Sync/server
This commit is contained in:
2
MareAPI
2
MareAPI
Submodule MareAPI updated: 5ac8a753dd...9fea4c30da
@@ -124,7 +124,7 @@ public class Startup
|
|||||||
new RedisHost(){ Host = address, Port = port },
|
new RedisHost(){ Host = address, Port = port },
|
||||||
},
|
},
|
||||||
AllowAdmin = true,
|
AllowAdmin = true,
|
||||||
ConnectTimeout = 3000,
|
ConnectTimeout = options.ConnectTimeout,
|
||||||
Database = 0,
|
Database = 0,
|
||||||
Ssl = false,
|
Ssl = false,
|
||||||
Password = options.Password,
|
Password = options.Password,
|
||||||
@@ -135,7 +135,8 @@ public class Startup
|
|||||||
UnreachableServerAction = ServerEnumerationStrategy.UnreachableServerActionOptions.Throw,
|
UnreachableServerAction = ServerEnumerationStrategy.UnreachableServerActionOptions.Throw,
|
||||||
},
|
},
|
||||||
MaxValueLength = 1024,
|
MaxValueLength = 1024,
|
||||||
PoolSize = 50,
|
PoolSize = mareConfig.GetValue(nameof(ServerConfiguration.RedisPool), 50),
|
||||||
|
SyncTimeout = options.SyncTimeout,
|
||||||
};
|
};
|
||||||
|
|
||||||
services.AddStackExchangeRedisExtensions<SystemTextJsonSerializer>(redisConfiguration);
|
services.AddStackExchangeRedisExtensions<SystemTextJsonSerializer>(redisConfiguration);
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ namespace MareSynchronosShared.Utils;
|
|||||||
public class ServerConfiguration : MareConfigurationAuthBase
|
public class ServerConfiguration : MareConfigurationAuthBase
|
||||||
{
|
{
|
||||||
public string RedisConnectionString { get; set; } = string.Empty;
|
public string RedisConnectionString { get; set; } = string.Empty;
|
||||||
|
public int RedisPool { get; set; } = 50;
|
||||||
|
|
||||||
[RemoteConfiguration]
|
[RemoteConfiguration]
|
||||||
public Uri CdnFullUrl { get; set; } = null;
|
public Uri CdnFullUrl { get; set; } = null;
|
||||||
@@ -32,6 +33,7 @@ public class ServerConfiguration : MareConfigurationAuthBase
|
|||||||
sb.AppendLine($"{nameof(CdnShardConfiguration)} => {string.Join(", ", CdnShardConfiguration.Select(c => c.ToString()))}");
|
sb.AppendLine($"{nameof(CdnShardConfiguration)} => {string.Join(", ", CdnShardConfiguration.Select(c => c.ToString()))}");
|
||||||
sb.AppendLine($"{nameof(StaticFileServiceAddress)} => {StaticFileServiceAddress}");
|
sb.AppendLine($"{nameof(StaticFileServiceAddress)} => {StaticFileServiceAddress}");
|
||||||
sb.AppendLine($"{nameof(RedisConnectionString)} => {RedisConnectionString}");
|
sb.AppendLine($"{nameof(RedisConnectionString)} => {RedisConnectionString}");
|
||||||
|
sb.AppendLine($"{nameof(RedisPool)} => {RedisPool}");
|
||||||
sb.AppendLine($"{nameof(MaxExistingGroupsByUser)} => {MaxExistingGroupsByUser}");
|
sb.AppendLine($"{nameof(MaxExistingGroupsByUser)} => {MaxExistingGroupsByUser}");
|
||||||
sb.AppendLine($"{nameof(MaxJoinedGroupsByUser)} => {MaxJoinedGroupsByUser}");
|
sb.AppendLine($"{nameof(MaxJoinedGroupsByUser)} => {MaxJoinedGroupsByUser}");
|
||||||
sb.AppendLine($"{nameof(MaxGroupUserCount)} => {MaxGroupUserCount}");
|
sb.AppendLine($"{nameof(MaxGroupUserCount)} => {MaxGroupUserCount}");
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ public class CacheController : ControllerBase
|
|||||||
private readonly RequestQueueService _requestQueue;
|
private readonly RequestQueueService _requestQueue;
|
||||||
|
|
||||||
public CacheController(ILogger<CacheController> logger, RequestFileStreamResultFactory requestFileStreamResultFactory,
|
public CacheController(ILogger<CacheController> logger, RequestFileStreamResultFactory requestFileStreamResultFactory,
|
||||||
CachedFileProvider cachedFileProvider, RequestQueueService requestQueue, ServerTokenGenerator generator) : base(logger, generator)
|
CachedFileProvider cachedFileProvider, RequestQueueService requestQueue) : base(logger)
|
||||||
{
|
{
|
||||||
_requestFileStreamResultFactory = requestFileStreamResultFactory;
|
_requestFileStreamResultFactory = requestFileStreamResultFactory;
|
||||||
_cachedFileProvider = cachedFileProvider;
|
_cachedFileProvider = cachedFileProvider;
|
||||||
@@ -30,7 +30,7 @@ public class CacheController : ControllerBase
|
|||||||
|
|
||||||
_requestQueue.ActivateRequest(requestId);
|
_requestQueue.ActivateRequest(requestId);
|
||||||
|
|
||||||
var fs = await _cachedFileProvider.GetAndDownloadFileStream(request.FileId, Authorization);
|
var fs = await _cachedFileProvider.GetAndDownloadFileStream(request.FileId);
|
||||||
if (fs == null)
|
if (fs == null)
|
||||||
{
|
{
|
||||||
_requestQueue.FinishRequest(requestId);
|
_requestQueue.FinishRequest(requestId);
|
||||||
|
|||||||
@@ -6,14 +6,11 @@ namespace MareSynchronosStaticFilesServer.Controllers;
|
|||||||
public class ControllerBase : Controller
|
public class ControllerBase : Controller
|
||||||
{
|
{
|
||||||
protected ILogger _logger;
|
protected ILogger _logger;
|
||||||
private readonly ServerTokenGenerator _generator;
|
|
||||||
|
|
||||||
public ControllerBase(ILogger logger, ServerTokenGenerator generator)
|
public ControllerBase(ILogger logger)
|
||||||
{
|
{
|
||||||
_logger = logger;
|
_logger = logger;
|
||||||
_generator = generator;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected string MareUser => HttpContext.User.Claims.First(f => string.Equals(f.Type, MareClaimTypes.Uid, StringComparison.Ordinal)).Value;
|
protected string MareUser => HttpContext.User.Claims.First(f => string.Equals(f.Type, MareClaimTypes.Uid, StringComparison.Ordinal)).Value;
|
||||||
protected string Authorization => _generator.Token;
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,10 +10,9 @@ public class RequestController : ControllerBase
|
|||||||
{
|
{
|
||||||
private readonly CachedFileProvider _cachedFileProvider;
|
private readonly CachedFileProvider _cachedFileProvider;
|
||||||
private readonly RequestQueueService _requestQueue;
|
private readonly RequestQueueService _requestQueue;
|
||||||
private static SemaphoreSlim _parallelRequestSemaphore = new(500);
|
private static readonly SemaphoreSlim _parallelRequestSemaphore = new(500);
|
||||||
|
|
||||||
public RequestController(ILogger<RequestController> logger, CachedFileProvider cachedFileProvider, RequestQueueService requestQueue,
|
public RequestController(ILogger<RequestController> logger, CachedFileProvider cachedFileProvider, RequestQueueService requestQueue) : base(logger)
|
||||||
ServerTokenGenerator generator) : base(logger, generator)
|
|
||||||
{
|
{
|
||||||
_cachedFileProvider = cachedFileProvider;
|
_cachedFileProvider = cachedFileProvider;
|
||||||
_requestQueue = requestQueue;
|
_requestQueue = requestQueue;
|
||||||
@@ -46,7 +45,7 @@ public class RequestController : ControllerBase
|
|||||||
foreach (var file in files)
|
foreach (var file in files)
|
||||||
{
|
{
|
||||||
_logger.LogDebug("Prerequested file: " + file);
|
_logger.LogDebug("Prerequested file: " + file);
|
||||||
_cachedFileProvider.DownloadFileWhenRequired(file, Authorization);
|
_cachedFileProvider.DownloadFileWhenRequired(file);
|
||||||
}
|
}
|
||||||
|
|
||||||
return Ok();
|
return Ok();
|
||||||
@@ -66,7 +65,7 @@ public class RequestController : ControllerBase
|
|||||||
{
|
{
|
||||||
await _parallelRequestSemaphore.WaitAsync(HttpContext.RequestAborted);
|
await _parallelRequestSemaphore.WaitAsync(HttpContext.RequestAborted);
|
||||||
Guid g = Guid.NewGuid();
|
Guid g = Guid.NewGuid();
|
||||||
_cachedFileProvider.DownloadFileWhenRequired(file, Authorization);
|
_cachedFileProvider.DownloadFileWhenRequired(file);
|
||||||
await _requestQueue.EnqueueUser(new(g, MareUser, file));
|
await _requestQueue.EnqueueUser(new(g, MareUser, file));
|
||||||
return Ok(g);
|
return Ok(g);
|
||||||
}
|
}
|
||||||
@@ -76,4 +75,22 @@ public class RequestController : ControllerBase
|
|||||||
_parallelRequestSemaphore.Release();
|
_parallelRequestSemaphore.Release();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[HttpGet]
|
||||||
|
[Route(MareFiles.Request_Check)]
|
||||||
|
public async Task<IActionResult> CheckQueueAsync(Guid requestId, string file)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
await _parallelRequestSemaphore.WaitAsync(HttpContext.RequestAborted);
|
||||||
|
if (!_requestQueue.StillEnqueued(requestId, MareUser))
|
||||||
|
await _requestQueue.EnqueueUser(new(requestId, MareUser, file));
|
||||||
|
return Ok();
|
||||||
|
}
|
||||||
|
catch (OperationCanceledException) { return BadRequest(); }
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
_parallelRequestSemaphore.Release();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -11,7 +11,7 @@ public class ServerFilesController : ControllerBase
|
|||||||
{
|
{
|
||||||
private readonly CachedFileProvider _cachedFileProvider;
|
private readonly CachedFileProvider _cachedFileProvider;
|
||||||
|
|
||||||
public ServerFilesController(ILogger<ServerFilesController> logger, CachedFileProvider cachedFileProvider, ServerTokenGenerator generator) : base(logger, generator)
|
public ServerFilesController(ILogger<ServerFilesController> logger, CachedFileProvider cachedFileProvider) : base(logger)
|
||||||
{
|
{
|
||||||
_cachedFileProvider = cachedFileProvider;
|
_cachedFileProvider = cachedFileProvider;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ using MareSynchronosStaticFilesServer.Utils;
|
|||||||
using System.Collections.Concurrent;
|
using System.Collections.Concurrent;
|
||||||
using System.Net.Http.Headers;
|
using System.Net.Http.Headers;
|
||||||
using MareSynchronos.API;
|
using MareSynchronos.API;
|
||||||
|
using MareSynchronosShared.Utils;
|
||||||
|
|
||||||
namespace MareSynchronosStaticFilesServer.Services;
|
namespace MareSynchronosStaticFilesServer.Services;
|
||||||
|
|
||||||
@@ -12,30 +13,32 @@ public class CachedFileProvider
|
|||||||
private readonly ILogger<CachedFileProvider> _logger;
|
private readonly ILogger<CachedFileProvider> _logger;
|
||||||
private readonly FileStatisticsService _fileStatisticsService;
|
private readonly FileStatisticsService _fileStatisticsService;
|
||||||
private readonly MareMetrics _metrics;
|
private readonly MareMetrics _metrics;
|
||||||
|
private readonly ServerTokenGenerator _generator;
|
||||||
private readonly Uri _remoteCacheSourceUri;
|
private readonly Uri _remoteCacheSourceUri;
|
||||||
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 bool IsMainServer => _remoteCacheSourceUri == null;
|
private bool IsMainServer => _remoteCacheSourceUri == null;
|
||||||
|
|
||||||
public CachedFileProvider(IConfigurationService<StaticFilesServerConfiguration> configuration, ILogger<CachedFileProvider> logger, FileStatisticsService fileStatisticsService, MareMetrics metrics)
|
public CachedFileProvider(IConfigurationService<StaticFilesServerConfiguration> configuration, ILogger<CachedFileProvider> logger, FileStatisticsService fileStatisticsService, MareMetrics metrics, ServerTokenGenerator generator)
|
||||||
{
|
{
|
||||||
_logger = logger;
|
_logger = logger;
|
||||||
_fileStatisticsService = fileStatisticsService;
|
_fileStatisticsService = fileStatisticsService;
|
||||||
_metrics = metrics;
|
_metrics = metrics;
|
||||||
|
_generator = generator;
|
||||||
_remoteCacheSourceUri = configuration.GetValueOrDefault<Uri>(nameof(StaticFilesServerConfiguration.RemoteCacheSourceUri), null);
|
_remoteCacheSourceUri = configuration.GetValueOrDefault<Uri>(nameof(StaticFilesServerConfiguration.RemoteCacheSourceUri), null);
|
||||||
_basePath = configuration.GetValue<string>(nameof(StaticFilesServerConfiguration.CacheDirectory));
|
_basePath = configuration.GetValue<string>(nameof(StaticFilesServerConfiguration.CacheDirectory));
|
||||||
_httpClient = new HttpClient();
|
_httpClient = new HttpClient();
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task DownloadTask(string hash, string auth)
|
private async Task DownloadTask(string hash)
|
||||||
{
|
{
|
||||||
// download file from remote
|
// download file from remote
|
||||||
var downloadUrl = MareFiles.ServerFilesGetFullPath(_remoteCacheSourceUri, hash);
|
var downloadUrl = MareFiles.ServerFilesGetFullPath(_remoteCacheSourceUri, hash);
|
||||||
_logger.LogInformation("Did not find {hash}, downloading from {server}", hash, downloadUrl);
|
_logger.LogInformation("Did not find {hash}, downloading from {server}", hash, downloadUrl);
|
||||||
|
|
||||||
using var requestMessage = new HttpRequestMessage(HttpMethod.Get, downloadUrl);
|
using var requestMessage = new HttpRequestMessage(HttpMethod.Get, downloadUrl);
|
||||||
requestMessage.Headers.Authorization = new AuthenticationHeaderValue("Bearer", auth);
|
requestMessage.Headers.Authorization = new AuthenticationHeaderValue("Bearer", _generator.Token);
|
||||||
var response = await _httpClient.SendAsync(requestMessage).ConfigureAwait(false);
|
var response = await _httpClient.SendAsync(requestMessage).ConfigureAwait(false);
|
||||||
|
|
||||||
try
|
try
|
||||||
@@ -63,7 +66,7 @@ public class CachedFileProvider
|
|||||||
_metrics.IncGauge(MetricsAPI.GaugeFilesTotalSize, FilePathUtil.GetFileInfoForHash(_basePath, hash).Length);
|
_metrics.IncGauge(MetricsAPI.GaugeFilesTotalSize, FilePathUtil.GetFileInfoForHash(_basePath, hash).Length);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void DownloadFileWhenRequired(string hash, string auth)
|
public void DownloadFileWhenRequired(string hash)
|
||||||
{
|
{
|
||||||
var fi = FilePathUtil.GetFileInfoForHash(_basePath, hash);
|
var fi = FilePathUtil.GetFileInfoForHash(_basePath, hash);
|
||||||
if (fi == null && IsMainServer) return;
|
if (fi == null && IsMainServer) return;
|
||||||
@@ -72,7 +75,7 @@ public class CachedFileProvider
|
|||||||
{
|
{
|
||||||
_currentTransfers[hash] = Task.Run(async () =>
|
_currentTransfers[hash] = Task.Run(async () =>
|
||||||
{
|
{
|
||||||
await DownloadTask(hash, auth).ConfigureAwait(false);
|
await DownloadTask(hash).ConfigureAwait(false);
|
||||||
_currentTransfers.Remove(hash, out _);
|
_currentTransfers.Remove(hash, out _);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -88,9 +91,9 @@ public class CachedFileProvider
|
|||||||
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<FileStream?> GetAndDownloadFileStream(string hash, string auth)
|
public async Task<FileStream?> GetAndDownloadFileStream(string hash)
|
||||||
{
|
{
|
||||||
DownloadFileWhenRequired(hash, auth);
|
DownloadFileWhenRequired(hash);
|
||||||
|
|
||||||
if (_currentTransfers.TryGetValue(hash, out var downloadTask))
|
if (_currentTransfers.TryGetValue(hash, out var downloadTask))
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user