Title: | Rate Limiting for R |
---|---|
Description: | Allows to limit the rate at which one or more functions can be called. |
Authors: | Tarak Shah |
Maintainer: | Tarak Shah <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.4.1 |
Built: | 2024-11-16 04:20:06 UTC |
Source: | https://github.com/tarakc02/ratelimitr |
Access the original function from a rate limited function
get_function(f)
get_function(f)
f |
A rate limited function or group of functions |
Access the rate limit precision
get_precision(f)
get_precision(f)
f |
A rate limited function or group of functions |
Access the rate limit(s) of a rate limited function
get_rates(f)
get_rates(f)
f |
A rate limited function or group of functions |
Limit the rate at which a function will execute
limit_rate(f, ..., precision = 60) ## S3 method for class 'list' limit_rate(f, ..., precision = 60) ## S3 method for class 'function_list' limit_rate(f, ..., precision = 60) ## S3 method for class 'function' limit_rate(f, ..., precision = 60)
limit_rate(f, ..., precision = 60) ## S3 method for class 'list' limit_rate(f, ..., precision = 60) ## S3 method for class 'function_list' limit_rate(f, ..., precision = 60) ## S3 method for class 'function' limit_rate(f, ..., precision = 60)
f |
A single function to be rate-limited, or a named list of functions |
... |
One or more rates, created using |
precision |
The precision with which time intervals can be measured, in hertz |
If f
is a single function, then a new function with the same
signature and (eventual) behavior as the original function, but rate limited.
If f
is a named list of functions, then a new list of functions with the
same names and signatures, but collectively bound by a shared rate limit.
## limiting a single function f <- limit_rate(Sys.time, rate(n = 5, period = .1)) res <- replicate(10, f()) ## show the elapsed time between each function call: round(res[-1] - head(res, -1), 3) ## for multiple functions, make sure the list is named: f <- function() 1 g <- function() 2 limited <- limit_rate(list(f = f, g = g), rate(n = 1, period = .1)) system.time({limited$f(); limited$g()})
## limiting a single function f <- limit_rate(Sys.time, rate(n = 5, period = .1)) res <- replicate(10, f()) ## show the elapsed time between each function call: round(res[-1] - head(res, -1), 3) ## for multiple functions, make sure the list is named: f <- function() 1 g <- function() 2 limited <- limit_rate(list(f = f, g = g), rate(n = 1, period = .1)) system.time({limited$f(); limited$g()})
Create a new rate
rate(n, period)
rate(n, period)
n |
Number of allowed events within a period |
period |
Length (in seconds) of measurement period |
## a function f <- function() NULL ## limit f to 10 calls per second limited_f <- limit_rate(f, rate(n = 10, period = 1))
## a function f <- function() NULL ## limit f to 10 calls per second limited_f <- limit_rate(f, rate(n = 10, period = 1))
This function does not modify the original rate-limited function, instead it returns a new function with the same rate limits (but no memory of prior function calls).
reset(f)
reset(f)
f |
A rate-limited function or group of functions |
f <- function() NULL f_lim <- limit_rate(f, rate(n = 1, period = .1)) f_lim() ## the next call to f_lim will trigger the rate limit f_lim2 <- reset(f_lim) ## but f_lim2 has a fresh start ## f_lim2 behaves as though no calls have been made system.time(f_lim2()) ## while f_lim is still constrained system.time(f_lim())
f <- function() NULL f_lim <- limit_rate(f, rate(n = 1, period = .1)) f_lim() ## the next call to f_lim will trigger the rate limit f_lim2 <- reset(f_lim) ## but f_lim2 has a fresh start ## f_lim2 behaves as though no calls have been made system.time(f_lim2()) ## while f_lim is still constrained system.time(f_lim())
UPDATE_RATE
modifies an existing rate-limited function in place,
changing the rate limits without otherwise altering the function's behavior.
When a rate limited function has its rate limits updated, the previous rate
limits and any calls that would have counted against those rate limits are
immediately forgotten, and only the new rate limits are obeyed going forward.
UPDATE_RATE(lf, ..., precision = 60)
UPDATE_RATE(lf, ..., precision = 60)
lf |
A rate-limited function or group of functions |
... |
One or more rates, created using |
precision |
The precision with which time intervals can be measured, in hertz |
f <- function() NULL f_lim <- limit_rate(f, rate(n = 1, period = .1)) # update the rate limits to 2 calls per .1 second UPDATE_RATE(f_lim, rate(n = 2, period = .1))
f <- function() NULL f_lim <- limit_rate(f, rate(n = 1, period = .1)) # update the rate limits to 2 calls per .1 second UPDATE_RATE(f_lim, rate(n = 2, period = .1))