Allows to limit the rate at which one or more functions can be called.
This package is available on CRAN. To install:
install.packages("ratelimitr")
Use ratelimitr to limit the rate at which functions are called. A rate-limited function that allows n
calls per period
will never have a window of time of length period
that includes more than n
calls.
library(ratelimitr)f <- function() NULLf_lim <- limit_rate(f, rate(n = 10, period = 1))# time without limitingsystem.time(replicate(11, f()))#> user system elapsed#> 0 0 0# time with limitingsystem.time(replicate(11, f_lim()))#> user system elapsed#> 0.00 0.00 1.05
Published rate limits often have multiple types of limits. Here is an example of limiting a function so that it never evaluates more than 10 times per .1 seconds, but additionally never evaluates more than 50 times per 1 second.
f_lim <- limit_rate(f,rate(n = 10, period = .1),rate(n = 50, period = 1))# 10 calls do not trigger the rate limitsystem.time(replicate(10, f_lim()))#> user system elapsed#> 0 0 0# sleeping in between tests to re-set the rate limit timerSys.sleep(1)# 11 function calls do trigger the rate limitsystem.time(replicate(11, f_lim())); Sys.sleep(1)#> user system elapsed#> 0.00 0.00 0.14# similarly, 50 calls don't trigger the second rate limitsystem.time(replicate(50, f_lim())); Sys.sleep(1)#> user system elapsed#> 0.00 0.00 0.56# but 51 calls do:system.time(replicate(51, f_lim())); Sys.sleep(1)#> user system elapsed#> 0.00 0.00 1.05
To limit a group of functions together, just pass limit_rate
a list of functions instead of a single function. Make sure the list is named, the names will be how you access the rate-limited versions of the functions:
f <- function() 1g <- function() 2h <- function() 3# passing a named list to limit_ratelimited <- limit_rate(list(f = f, g = g, h = h), rate(n = 3, period = 1))# now limited is a list of functions that share a rate limit. examples:limited$f()#> [1] 1limited$g()#> [1] 2
The new functions are subject to a single rate limit, regardless of which ones are called or in what order they are called.
# the first three function calls should not trigger a delaysystem.time({limited$f(); limited$g(); limited$h()})#> user system elapsed#> 0 0 0# sleep in between tests to reset the rate limit timerSys.sleep(1)# but to evaluate a fourth function call, there will be a delaysystem.time({limited$f(); limited$g(); limited$h(); limited$f()})#> user system elapsed#> 0.00 0.00 1.04
limit_rate
is not safe to use in parallel.
The precision with which you can measure the length of time that has elapsed between two events is constrained to some degree, dependent on your operating system. In order to guarantee compliance with rate limits, this package truncates the time (specifically taking the ceiling or the floor based on which would give the most conservative estimate of elapsed time), rounding to the fraction specified in the precision
argument of token_dispenser
-- the default is 60, meaning time measurements are taken up to the 1/60th of a second. While the conservative measurements of elapsed time make it impossible to overrun the rate limit by a tiny fraction of a second (see Issue 3), they also will result in waiting times that are slightly longer than necessary (using the default precision
of 60, waiting times will be .01-.03 seconds longer than necessary).
Sys.sleep
, there were rare occasions where rate-limited functions displayed unexpected and wrong behavior (see #12 and #13). In order to fix the problem, rate-limited functions now wait at least .02 seconds longer than necessary.proc.time
instead of Sys.time
to measure time (for increased precision).NEWS.md
file to track changes to the package.