Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In C#, I am calling a public API, which has a API limit of 10 calls per second [closed]

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

like image 304
Asad Khan Avatar asked Jun 19 '17 11:06

Asad Khan


1 Answers

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

  • wait for a semaphore "slot"
  • start the real task
  • release the semaphore slot after a given timespan when the real task has finished

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);
    }

}
like image 51
Sir Rufo Avatar answered Oct 02 '22 23:10

Sir Rufo