mirror of
https://github.com/azaion/annotations.git
synced 2026-04-22 21:46:30 +00:00
19 lines
562 B
C#
19 lines
562 B
C#
namespace Azaion.Common.Extensions;
|
|
|
|
public static class ThrottleExt
|
|
{
|
|
private static bool _throttleOn;
|
|
public static async Task Throttle(this Func<Task> func, TimeSpan? throttleTime = null, CancellationToken cancellationToken = default)
|
|
{
|
|
if (_throttleOn)
|
|
return;
|
|
|
|
_throttleOn = true;
|
|
await func();
|
|
_ = Task.Run(async () =>
|
|
{
|
|
await Task.Delay(throttleTime ?? TimeSpan.FromMilliseconds(500), cancellationToken);
|
|
_throttleOn = false;
|
|
}, cancellationToken);
|
|
}
|
|
} |