jeroen / curl

A Modern and Flexible Web Client for R
https://jeroen.r-universe.dev/curl
Other
216 stars 71 forks source link

Support for request rate limiting? #336

Open benwhalley opened 6 months ago

benwhalley commented 6 months ago

Curl supports request rate limiting (https://everything.curl.dev/usingcurl/transfers/request-rate.html) which is really nice when calling external APIs. Sometimes you have a known rate limit you want to get close to but not exceed (e.g. calling openai).

An additional wrinkle is that even with an explicit limit per minute of N, spamming the API with N requests in a single hit is likely to trigger a rate limit exceeded exception. Retrying and backoff is one solution, but it would be much nicer if it were possible to use the n per U syntax from curl command line, e.g. n/U = 10 requests per second. This is distinct from the number of open connections per host; it's possible that >N connections may be open if requests are long running.

Is the rate limiting functionality exposed by libcurl, and would it be possible to implement it here, e.g. when defining a pool? Having a soft-start feature where traffic is ramped-up to n/U over a set period would also be neat (there are examples of this in some of the python libraries). I think this would be appreciated in other users of this package (e.g. httr2) which might otherwise have to implement similar logic.

jeroen commented 6 months ago

I don't think this is part of libcurl: https://curl.se/libcurl/c/curl_multi_setopt.html

You probably need to implement this in the application layer. It would probably more suitable to do this in httr2 for example in req_perform_parallel.