Files
annotations/Azaion.Common/Extensions/ThrottleExtensions.cs
T
Alex Bezdieniezhnykh 0c66607ed7 failsafe load dlls
add user config queue offsets
throttle improvements
2025-04-17 01:19:48 +03:00

22 lines
618 B
C#

namespace Azaion.Common.Extensions;
public static class ThrottleExt
{
private static readonly Dictionary<Delegate, DateTime> LastExecution = new();
private static readonly object Lock = new();
public static async Task Throttle(this Func<Task> func, TimeSpan interval, CancellationToken ct = default)
{
ArgumentNullException.ThrowIfNull(func);
lock (Lock)
{
if (LastExecution.ContainsKey(func) && DateTime.UtcNow - LastExecution[func] < interval)
return;
func();
LastExecution[func] = DateTime.UtcNow;
}
}
}