Simple way to rate limit HttpClient requests

后端 未结 3 1358
终归单人心
终归单人心 2020-12-28 21:26

I am using the HTTPClient in System.Net.Http to make requests against an API. The API is limited to 10 requests per second.

My code is roughly like so:



        
3条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-28 22:17

    The answer is similar to this one.

    Instead of using a list of tasks and WhenAll, use Parallel.ForEach and use ParallelOptions to limit the number of concurrent tasks to 10, and make sure each one takes at least 1 second:

    Parallel.ForEach(
        items,
        new ParallelOptions { MaxDegreeOfParallelism = 10 },
        async item => {
          ProcessItems(item);
          await Task.Delay(1000);
        }
    );
    

    Or if you want to make sure each item takes as close to 1 second as possible:

    Parallel.ForEach(
        searches,
        new ParallelOptions { MaxDegreeOfParallelism = 10 },
        async item => {
            var watch = new Stopwatch();
            watch.Start();
            ProcessItems(item);
            watch.Stop();
            if (watch.ElapsedMilliseconds < 1000) await Task.Delay((int)(1000 - watch.ElapsedMilliseconds));
        }
    );
    

    Or:

    Parallel.ForEach(
        searches,
        new ParallelOptions { MaxDegreeOfParallelism = 10 },
        async item => {
            await Task.WhenAll(
                    Task.Delay(1000),
                    Task.Run(() => { ProcessItems(item); })
                );
        }
    );
    

提交回复
热议问题