All checks were successful
Tag and Release Lightless / tag-and-release (push) Successful in 2m27s
2.0.0 Changes: - Reworked shell finder UI with compact or list view with profile tags showing with the listing, allowing moderators to broadcast the syncshell as well to have it be used more. - Reworked user list in syncshell admin screen to have filter visible and moved away from table to its own thing, allowing to copy uid/note/alias when clicking on the name. - Reworked download bars and download box to make it look more modern, removed the jitter around, so it shouldn't vibrate around much. - Chat has been added to the top menu, working in Zone or in Syncshells to be used there. - Paired system has been revamped to make pausing and unpausing faster, and loading people should be faster as well. - Moved to the internal object table to have faster load times for users; people should load in faster - Compactor is running on a multi-threaded level instead of single-threaded; this should increase the speed of compacting files - Nameplate Service has been reworked so it wouldn't use the nameplate handler anymore. - Files can be resized when downloading to reduce load on users if they aren't compressed. (can be toggled to resize all). - Penumbra Collections are now only made when people are visible, reducing the load on boot-up when having many syncshells in your list. - Lightfinder plates have been moved away from using Nameplates, but will use an overlay. - Main UI has been changed a bit with a gradient, and on hover will glow up now. - Reworked Profile UI for Syncshell and Users to be more user-facing with more customizable items. - Reworked Settings UI to look more modern. - Performance should be better due to new systems that would dispose of the collections and better caching of items. Co-authored-by: defnotken <itsdefnotken@gmail.com> Co-authored-by: azyges <aaaaaa@aaa.aaa> Co-authored-by: choco <choco@patat.nl> Co-authored-by: cake <admin@cakeandbanana.nl> Co-authored-by: Minmoose <KennethBohr@outlook.com> Reviewed-on: #92
248 lines
6.7 KiB
C#
248 lines
6.7 KiB
C#
using LightlessSync.LightlessConfiguration;
|
|
using LightlessSync.Services.Mediator;
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
namespace LightlessSync.Services.PairProcessing;
|
|
|
|
public sealed class PairProcessingLimiter : DisposableMediatorSubscriberBase
|
|
{
|
|
private const int _hardLimit = 32;
|
|
private readonly LightlessConfigService _configService;
|
|
private readonly object _limitLock = new();
|
|
private readonly SemaphoreSlim _semaphore;
|
|
private int _currentLimit;
|
|
private int _pendingReductions;
|
|
private int _pendingIncrements;
|
|
private int _waiting;
|
|
private int _inFlight;
|
|
|
|
public PairProcessingLimiter(ILogger<PairProcessingLimiter> logger, LightlessMediator mediator, LightlessConfigService configService)
|
|
: base(logger, mediator)
|
|
{
|
|
_configService = configService;
|
|
_currentLimit = CalculateLimit();
|
|
var initialCount = _configService.Current.EnablePairProcessingLimiter ? _currentLimit : _hardLimit;
|
|
_semaphore = new SemaphoreSlim(initialCount, _hardLimit);
|
|
|
|
Mediator.Subscribe<PairProcessingLimitChangedMessage>(this, _ => UpdateSemaphoreLimit());
|
|
}
|
|
|
|
public ValueTask<IAsyncDisposable> AcquireAsync(CancellationToken cancellationToken)
|
|
{
|
|
return WaitInternalAsync(cancellationToken);
|
|
}
|
|
|
|
public PairProcessingLimiterSnapshot GetSnapshot()
|
|
{
|
|
lock (_limitLock)
|
|
{
|
|
var enabled = IsEnabled;
|
|
var limit = enabled ? _currentLimit : CalculateLimit();
|
|
var waiting = Math.Max(0, Volatile.Read(ref _waiting));
|
|
var inFlight = Math.Max(0, Volatile.Read(ref _inFlight));
|
|
return new PairProcessingLimiterSnapshot(enabled, limit, inFlight, waiting);
|
|
}
|
|
}
|
|
|
|
private bool IsEnabled => _configService.Current.EnablePairProcessingLimiter;
|
|
|
|
private async ValueTask<IAsyncDisposable> WaitInternalAsync(CancellationToken token)
|
|
{
|
|
if (!IsEnabled)
|
|
{
|
|
return NoopReleaser.Instance;
|
|
}
|
|
|
|
Interlocked.Increment(ref _waiting);
|
|
try
|
|
{
|
|
await _semaphore.WaitAsync(token).ConfigureAwait(false);
|
|
}
|
|
catch
|
|
{
|
|
Interlocked.Decrement(ref _waiting);
|
|
throw;
|
|
}
|
|
|
|
Interlocked.Decrement(ref _waiting);
|
|
|
|
if (!IsEnabled)
|
|
{
|
|
TryReleaseSemaphore();
|
|
return NoopReleaser.Instance;
|
|
}
|
|
|
|
Interlocked.Increment(ref _inFlight);
|
|
return new Releaser(this);
|
|
}
|
|
|
|
private void UpdateSemaphoreLimit()
|
|
{
|
|
lock (_limitLock)
|
|
{
|
|
var enabled = IsEnabled;
|
|
var desiredLimit = CalculateLimit();
|
|
|
|
if (!enabled)
|
|
{
|
|
var releaseAmount = _hardLimit - _semaphore.CurrentCount;
|
|
if (releaseAmount > 0)
|
|
{
|
|
TryReleaseSemaphore(releaseAmount);
|
|
}
|
|
|
|
_currentLimit = desiredLimit;
|
|
_pendingReductions = 0;
|
|
_pendingIncrements = 0;
|
|
return;
|
|
}
|
|
|
|
if (desiredLimit == _currentLimit)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (desiredLimit > _currentLimit)
|
|
{
|
|
var increment = desiredLimit - _currentLimit;
|
|
_pendingIncrements += increment;
|
|
|
|
var available = _hardLimit - _semaphore.CurrentCount;
|
|
var toRelease = Math.Min(_pendingIncrements, available);
|
|
if (toRelease > 0 && TryReleaseSemaphore(toRelease))
|
|
{
|
|
_pendingIncrements -= toRelease;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
var decrement = _currentLimit - desiredLimit;
|
|
var removed = 0;
|
|
while (removed < decrement && _semaphore.Wait(0))
|
|
{
|
|
removed++;
|
|
}
|
|
|
|
var remaining = decrement - removed;
|
|
if (remaining > 0)
|
|
{
|
|
_pendingReductions += remaining;
|
|
}
|
|
|
|
if (_pendingIncrements > 0)
|
|
{
|
|
var offset = Math.Min(_pendingIncrements, _pendingReductions);
|
|
_pendingIncrements -= offset;
|
|
_pendingReductions -= offset;
|
|
}
|
|
}
|
|
|
|
_currentLimit = desiredLimit;
|
|
Logger.LogDebug("Pair processing concurrency updated to {limit} (pending reductions: {pending})", _currentLimit, _pendingReductions);
|
|
}
|
|
}
|
|
|
|
private int CalculateLimit()
|
|
{
|
|
var configured = _configService.Current.MaxConcurrentPairApplications;
|
|
return Math.Clamp(configured, 1, _hardLimit);
|
|
}
|
|
|
|
private bool TryReleaseSemaphore(int count = 1)
|
|
{
|
|
if (count <= 0)
|
|
{
|
|
return true;
|
|
}
|
|
|
|
try
|
|
{
|
|
_semaphore.Release(count);
|
|
return true;
|
|
}
|
|
catch (SemaphoreFullException ex)
|
|
{
|
|
Logger.LogDebug(ex, "Attempted to release {count} pair processing slots but semaphore is already at the hard limit.", count);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
private void ReleaseOne()
|
|
{
|
|
var inFlight = Interlocked.Decrement(ref _inFlight);
|
|
if (inFlight < 0)
|
|
{
|
|
Interlocked.Exchange(ref _inFlight, 0);
|
|
}
|
|
|
|
if (!IsEnabled)
|
|
{
|
|
return;
|
|
}
|
|
|
|
lock (_limitLock)
|
|
{
|
|
if (_pendingReductions > 0)
|
|
{
|
|
_pendingReductions--;
|
|
return;
|
|
}
|
|
|
|
if (_pendingIncrements > 0)
|
|
{
|
|
if (!TryReleaseSemaphore())
|
|
{
|
|
return;
|
|
}
|
|
|
|
_pendingIncrements--;
|
|
return;
|
|
}
|
|
}
|
|
|
|
TryReleaseSemaphore();
|
|
}
|
|
|
|
protected override void Dispose(bool disposing)
|
|
{
|
|
base.Dispose(disposing);
|
|
if (!disposing)
|
|
{
|
|
return;
|
|
}
|
|
|
|
_semaphore.Dispose();
|
|
}
|
|
|
|
private sealed class Releaser : IAsyncDisposable
|
|
{
|
|
private PairProcessingLimiter? _owner;
|
|
|
|
public Releaser(PairProcessingLimiter owner)
|
|
{
|
|
_owner = owner;
|
|
}
|
|
|
|
public ValueTask DisposeAsync()
|
|
{
|
|
var owner = Interlocked.Exchange(ref _owner, null);
|
|
owner?.ReleaseOne();
|
|
return ValueTask.CompletedTask;
|
|
}
|
|
}
|
|
|
|
private sealed class NoopReleaser : IAsyncDisposable
|
|
{
|
|
public static readonly NoopReleaser Instance = new();
|
|
|
|
private NoopReleaser()
|
|
{
|
|
}
|
|
|
|
public ValueTask DisposeAsync()
|
|
{
|
|
return ValueTask.CompletedTask;
|
|
}
|
|
}
|
|
}
|