Files
ClubPenguinServer/MareSynchronosServer/MareSynchronosStaticFilesServer/Services/FileStatisticsService.cs
rootdarkarchon 42b15cb6b7 Add Server-Side Download Queue (#21)
* test add queueing to file service

* further adjustments to download queueing

* add check for whether the request is still in the queue to CheckQueue

* forcefully release slot if download didn't finish in 15s

* actually cancel the delay task

* add metrics and refactor some of the request queue service

* refactor pathing

* reuse httpclient

* add queue request dto to requestfile, enqueue users immediately if a slot is available

* change startup to include all controllers

* update server pathing

* update pathing, again

* several adjustments to auth, banning, jwt server tokens, renaming, authorization

* update api I guess

* adjust automated banning of charaident and reg

* generate jwt on servers for internal authentication

* remove mvcextensions

Co-authored-by: rootdarkarchon <root.darkarchon@outlook.com>
2023-01-11 12:22:22 +01:00

90 lines
3.2 KiB
C#

using MareSynchronosShared.Metrics;
using System.Collections.Concurrent;
namespace MareSynchronosStaticFilesServer.Services;
public class FileStatisticsService : IHostedService
{
private readonly MareMetrics _metrics;
private readonly ILogger<FileStatisticsService> _logger;
private CancellationTokenSource _resetCancellationTokenSource;
private ConcurrentDictionary<string, long> _pastHourFiles = new(StringComparer.Ordinal);
private ConcurrentDictionary<string, long> _pastDayFiles = new(StringComparer.Ordinal);
public FileStatisticsService(MareMetrics metrics, ILogger<FileStatisticsService> logger)
{
_metrics = metrics;
_logger = logger;
}
public void LogFile(string fileHash, long length)
{
if (!_pastHourFiles.ContainsKey(fileHash))
{
_pastHourFiles[fileHash] = length;
_metrics.IncGauge(MetricsAPI.GaugeFilesUniquePastHour);
_metrics.IncGauge(MetricsAPI.GaugeFilesUniquePastHourSize, length);
}
if (!_pastDayFiles.ContainsKey(fileHash))
{
_pastDayFiles[fileHash] = length;
_metrics.IncGauge(MetricsAPI.GaugeFilesUniquePastDay);
_metrics.IncGauge(MetricsAPI.GaugeFilesUniquePastDaySize, length);
}
}
public Task StartAsync(CancellationToken cancellationToken)
{
_logger.LogInformation("Starting FileStatisticsService");
_resetCancellationTokenSource = new();
_ = ResetHourlyFileData();
_ = ResetDailyFileData();
return Task.CompletedTask;
}
public async Task ResetHourlyFileData()
{
while (!_resetCancellationTokenSource.Token.IsCancellationRequested)
{
_logger.LogInformation("Resetting 1h Data");
_pastHourFiles = new(StringComparer.Ordinal);
_metrics.SetGaugeTo(MetricsAPI.GaugeFilesUniquePastHour, 0);
_metrics.SetGaugeTo(MetricsAPI.GaugeFilesUniquePastHourSize, 0);
var now = DateTime.UtcNow;
TimeOnly currentTime = new(now.Hour, now.Minute, now.Second);
TimeOnly futureTime = new(now.Hour, 0, 0);
var span = futureTime.AddHours(1) - currentTime;
await Task.Delay(span, _resetCancellationTokenSource.Token).ConfigureAwait(false);
}
}
public async Task ResetDailyFileData()
{
while (!_resetCancellationTokenSource.Token.IsCancellationRequested)
{
_logger.LogInformation("Resetting 24h Data");
_pastDayFiles = new(StringComparer.Ordinal);
_metrics.SetGaugeTo(MetricsAPI.GaugeFilesUniquePastDay, 0);
_metrics.SetGaugeTo(MetricsAPI.GaugeFilesUniquePastDaySize, 0);
var now = DateTime.UtcNow;
TimeOnly currentTime = new(now.Hour, now.Minute, now.Second);
TimeOnly futureTime = new(0, 0, 0);
var span = futureTime - currentTime;
await Task.Delay(span, _resetCancellationTokenSource.Token).ConfigureAwait(false);
}
}
public Task StopAsync(CancellationToken cancellationToken)
{
_resetCancellationTokenSource.Cancel();
_logger.LogInformation("Stopping FileStatisticsService");
return Task.CompletedTask;
}
}