The hardware and bandwidth for this mirror is donated by dogado GmbH, the Webhosting and Full Service-Cloud Provider. Check out our Wordpress Tutorial.
If you wish to report a bug, or if you are interested in having us mirror your free-software or open-source project, please feel free to contact us at mirror[@]dogado.de.
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)
<- function() NULL
f
# create a version of f that can only be called 10 times per second
<- limit_rate(f, rate(n = 10, period = 1))
f_lim
# time without limiting
system.time(replicate(11, f()))
#> user system elapsed
#> 0 0 0
# time with limiting
system.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.
<- limit_rate(
f_lim
f, rate(n = 10, period = .1),
rate(n = 50, period = 1)
)
# 10 calls do not trigger the rate limit
system.time(replicate(10, f_lim()))
#> user system elapsed
#> 0 0 0
# sleeping in between tests to re-set the rate limit timer
Sys.sleep(1)
# 11 function calls do trigger the rate limit
system.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 limit
system.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:
<- function() 1
f <- function() 2
g <- function() 3
h
# passing a named list to limit_rate
<- limit_rate(list(f = f, g = g, h = h), rate(n = 3, period = 1))
limited
# now limited is a list of functions that share a rate limit. examples:
$f()
limited#> [1] 1
$g()
limited#> [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 delay
system.time(
$f(); limited$g(); limited$h()}
{limited
)#> user system elapsed
#> 0 0 0
# sleep in between tests to reset the rate limit timer
Sys.sleep(1)
# but to evaluate a fourth function call, there will be a delay
system.time(
$f(); limited$g(); limited$h(); limited$f()}
{limited
)#> 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).
These binaries (installable software) and packages are in development.
They may not be fully stable and should be used with caution. We make no claims about them.
Health stats visible at Monitor.