Files
ucvl-zonghengjia-open-source/windows/LearningProbeBudget.cs
T

24 lines
898 B
C#
Raw Normal View History

namespace UCVL.VPNPlus;
// One budget per core/network epoch. No unbounded queues, browsing history or disk cache.
internal sealed class LearningProbeBudget
{
private readonly Dictionary<string, long> _recent = new();
private long _window;
private int _count;
private int _active;
internal bool TryEnter(string target, long now)
{
lock (_recent)
{
if (_active >= 4) return false;
if (now - _window >= 60_000) { _count=0; _window=now; }
if (_count >= 60 || _recent.TryGetValue(target, out var previous) && now-previous < 30_000) return false;
foreach (var key in _recent.Where(p => now-p.Value >= 60_000).Select(p=>p.Key).ToArray()) _recent.Remove(key);
_recent[target]=now; _count++; _active++;
return true;
}
}
internal void Exit() { lock (_recent) { _active--; } }
}