Skip to contents

Wraps f so at most n calls are permitted within any rolling period-second window. When the limit is reached, either sleeps until a slot opens (.wait = TRUE, the default) or raises an error of class "fmisc_rate_limit_exceeded" (.wait = FALSE).

Usage

with_rate_limit(f, n, period = 1, ..., .wait = TRUE)

Arguments

f

A function to wrap.

n

Positive integer. Maximum calls per window.

period

Positive numeric. Window length in seconds.

...

Reserved for future extension.

.wait

Logical. If TRUE sleep to obey the limit; if FALSE error immediately when the limit would be exceeded.

Value

A function that forwards its arguments to f.

Examples

if (FALSE) { # \dontrun{
g <- with_rate_limit(function(x) x, n = 2, period = 1)
g(1)
g(2)
g(3) # third call sleeps until the window rolls
} # }