mirror of
https://github.com/azaion/annotations.git
synced 2026-04-22 15:56:30 +00:00
0c66607ed7
add user config queue offsets throttle improvements
22 lines
618 B
C#
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;
|
|
}
|
|
}
|
|
|
|
} |