In C#, I am calling a public API, which has an API limit of 10 calls per second. API has multiple methods, different users can call different methods at a time, hence there are chances that "Rate Limit Reached" Exception may occur.
I have the following class structure:
public class MyServiceManager
{
public int Method1()
{
}
public void Method2()
{
}
public string Method3()
{
}
}
Multiple users can call different methods at a time, How can I maintain a static calling Queue or Task so that I can monitor all requests and entertain only 10 requests in a one second
You can build a TaskLimiter based on SemaphoreSlim
public class TaskLimiter
{
private readonly TimeSpan _timespan;
private readonly SemaphoreSlim _semaphore;
public TaskLimiter(int count, TimeSpan timespan)
{
_semaphore = new SemaphoreSlim(count, count);
_timespan = timespan;
}
public async Task LimitAsync(Func<Task> taskFactory)
{
await _semaphore.WaitAsync().ConfigureAwait(false);
var task = taskFactory();
task.ContinueWith(async e =>
{
await Task.Delay(_timespan);
_semaphore.Release(1);
});
await task;
}
public async Task<T> LimitAsync<T>(Func<Task<T>> taskFactory)
{
await _semaphore.WaitAsync().ConfigureAwait(false);
var task = taskFactory();
task.ContinueWith(async e =>
{
await Task.Delay(_timespan);
_semaphore.Release(1);
});
return await task;
}
}
It will
Here a sample usage
public class Program
{
public static void Main()
{
RunAsync().Wait();
}
public static async Task RunAsync()
{
var limiter = new TaskLimiter(10, TimeSpan.FromSeconds(1));
// create 100 tasks
var tasks = Enumerable.Range(1, 100)
.Select(e => limiter.LimitAsync(() => DoSomeActionAsync(e)));
// wait unitl all 100 tasks are completed
await Task.WhenAll(tasks).ConfigureAwait(false);
}
static readonly Random _rng = new Random();
public static async Task DoSomeActionAsync(int i)
{
await Task.Delay(150 + _rng.Next(150)).ConfigureAwait(false);
Console.WriteLine("Completed Action {0}", i);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With