I have the following class to control access to a resource:
class Sync : IDisposable
{
private static readonly SemaphoreSlim Semaphore = new SemaphoreSlim(20);
private Sync()
{
}
public static async Task<Sync> Acquire()
{
await Semaphore.WaitAsync();
return new Sync();
}
public void Dispose()
{
Semaphore.Release();
}
}
Using:
using (await Sync.Acquire())
{
}
Now it allows you to use no more than 20 common methods of use.
How to change this class to allow no more than N total uses per unit of time (for example, no more than 20 per second)?
source
share