1
0
mirror of https://github.com/chylex/Minecraft-Phantom-Panel.git synced 2024-11-24 04:42:56 +01:00
Minecraft-Phantom-Panel/Server/Phantom.Server.Web.Components/Utils/Throttler.cs

32 lines
633 B
C#

namespace Phantom.Server.Web.Components.Utils;
public sealed class Throttler {
private readonly TimeSpan interval;
private DateTime lastInvocation;
public Throttler(TimeSpan interval) {
this.interval = interval;
this.lastInvocation = DateTime.Now;
}
public bool Check() {
var now = DateTime.Now;
if (now - lastInvocation >= interval) {
lastInvocation = now;
return true;
}
return false;
}
public async Task Wait() {
var now = DateTime.Now;
var waitTime = lastInvocation + interval - now;
if (waitTime > TimeSpan.Zero) {
await Task.Delay(waitTime);
}
lastInvocation = DateTime.Now;
}
}