Files
opengenesislinkserver/addons/OGLAssetService/OGLAssetServiceAdminControls.cs
T
OpenGenesis (MPL 2.0) 9295b28179 feat(ogl-assets): integrate safe FSAssets-compatible Gen 1 addon (#237)
Distribute the opt-in OGL AssetService, isolated build/ROBUST/DB/HTTP and cache/atomic migration tests, and staged admin infrastructure. Preserve existing AssetService/HGAssetService configuration and authoritative FSAssets storage. Fix SSD cache reuse across ROBUST restarts. No production cutover; distributed caches, atomic updates and admin listener remain disabled. Real-data staging and rollback required before activation.
2026-10-23 03:16:20 +02:00

73 lines
2.5 KiB
C#

using System;
using System.Threading;
namespace OpenGenesisLINK.Addons.AssetService
{
/// <summary>
/// Process-local fixed-window request limiter, shared by admin routes.
/// Not a substitute for host-level distributed or per-client controls.
/// </summary>
public sealed class OGLAssetServiceAdminRateLimiter
{
private readonly object sync = new object();
private readonly int requestsPerMinute;
private long windowStart;
private int count;
public OGLAssetServiceAdminRateLimiter(int requestsPerMinute)
{
if (requestsPerMinute < 1 || requestsPerMinute > 10000)
throw new ArgumentOutOfRangeException(nameof(requestsPerMinute));
this.requestsPerMinute = requestsPerMinute;
windowStart = Environment.TickCount64;
}
public bool TryAcquire()
{
lock (sync)
{
long now = Environment.TickCount64;
if (now - windowStart >= 60000 || now < windowStart)
{
windowStart = now;
count = 0;
}
if (count >= requestsPerMinute) return false;
count++;
return true;
}
}
}
/// <summary>
/// Privacy-preserving process-local outcome counters. No request body,
/// remote address, identity, token or asset identifier is stored.
/// </summary>
public sealed class OGLAssetServiceAdminAudit
{
private long allowed;
private long unauthorized;
private long forbidden;
private long throttled;
private long errors;
public long Allowed => Interlocked.Read(ref allowed);
public long Unauthorized => Interlocked.Read(ref unauthorized);
public long Forbidden => Interlocked.Read(ref forbidden);
public long Throttled => Interlocked.Read(ref throttled);
public long Errors => Interlocked.Read(ref errors);
public void Record(string outcome)
{
switch (outcome)
{
case "allowed": Interlocked.Increment(ref allowed); break;
case "unauthorized": Interlocked.Increment(ref unauthorized); break;
case "forbidden": Interlocked.Increment(ref forbidden); break;
case "throttled": Interlocked.Increment(ref throttled); break;
default: Interlocked.Increment(ref errors); break;
}
}
}
}