Rate Limiting for R

Allows to limit the rate at which one or more functions can be called.

CRAN_Status_Badge Travis-CI Build Status Coverage Status


This package is available on CRAN. To install:



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.

f <- function() NULL
f_lim <- limit_rate(f, rate(n = 10, period = 1))
# 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

Multiple rates

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(
    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
# 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

Multiple functions sharing one (or more) rate limit(s)

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() 1
g <- function() 2
h <- function() 3
# passing a named list to limit_rate
limited <- 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:
#> [1] 1
#> [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
    {limited$f(); limited$g(); limited$h()}
#>    user  system elapsed 
#>       0       0       0
# sleep in between tests to reset the rate limit timer
# but to evaluate a fourth function call, there will be a delay
    {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).


ratelimitr 0.4.1

  • update maintainer email address

ratelimitr 0.4.0

  • added the method UPDATE_RATE to modify existing rate-limited functions in place.

ratelimitr 0.3.8

  • ratelimitr now measures time from just after prior function executions, rather than just before. This allows rate limits to be obeyed even in the presence of network latency (see #14). Thanks to @stephlocke.
  • Due to inherent imprecision of 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.
  • Use proc.time instead of Sys.time to measure time (for increased precision).

ratelimitr 0.3.7

  • Edit unit tests so that tests relying on microbenchmark ("Suggests") are conditional on microbenchmark's presence

ratelimitr 0.3.6

  • Added a NEWS.md file to track changes to the package.
  • First release on CRAN

Reference manual

It appears you don't have a PDF plugin for this browser. You can click here to download the reference manual.


0.4.1 by Tarak Shah, 3 years ago


Report a bug at https://github.com/tarakc02/ratelimitr/issues

Browse source code at https://github.com/cran/ratelimitr

Authors: Tarak Shah

Documentation:   PDF Manual  

MIT + file LICENSE license

Imports assertthat

Suggests testthat, microbenchmark, knitr, rmarkdown, covr

Imported by WikidataQueryServiceR, ffscrapr, opencage, polite.

See at CRAN