speedbump
Types
Represents the actual limit on the requests that can be made. We use a token bucket algorithm, and a token refill rate defined in nanoseconds, which is small enough for this packages use cases.
pub type Limit {
Limit(
ns_per_token: Int,
tokens: Int,
burst: Int,
description: String,
)
}
Constructors
-
Limit( ns_per_token: Int, tokens: Int, burst: Int, description: String, )
Just an alias for the actor implementation.
pub type RateLimiter =
actor.Started(process.Subject(Msg))
Values
pub fn ask(
rate_limiter: actor.Started(process.Subject(Msg)),
timeout_ms: Int,
n_requests: Int,
) -> Int
Ask the rate limiter how much time is left before you can make n requests. The response is in nanoseconds.
pub fn hits_per_hour(hits hits: Int) -> Limit
Creates a limit of hits number of requests per hour.
pub fn hits_per_hours(hits hits: Int, hours hrs: Int) -> Limit
Creates a limit of hits number of requests per hours hours.
pub fn hits_per_minute(hits hits: Int) -> Limit
Creates a limit of hits number of requests per minute.
pub fn hits_per_minutes(
hits hits: Int,
minutes mins: Int,
) -> Limit
Creates a limit of hits number of requests per minutes minutes.
pub fn hits_per_second(hits hits: Int) -> Limit
Creates a limit of hits number of requests per second.
pub fn hits_per_seconds(
hits hits: Int,
seconds secs: Int,
) -> Limit
Creates a limit of hits number of requests per seconds seconds.
pub fn lazy_guard(
rate_limiter: actor.Started(process.Subject(Msg)),
timeout_ms: Int,
or_else: fn(String) -> a,
do: fn() -> a,
) -> a
This function allows the rate limiter to be used as a lazy guard. Example:
use <- speedbump.lazy_guard(limiter, fn(limit_description) {
// ... Construct the appropriate error. `limit_description` is a text description of the limit that was violated.
})
// ... Continue with the function
pub fn start(
limits: List(Limit),
) -> Result(actor.Started(process.Subject(Msg)), actor.StartError)
Start a new rate limiter actor.
pub fn supervised(
limits: List(Limit),
) -> supervision.ChildSpecification(process.Subject(Msg))
Get a constructor for a rate limiter that can be used as part of a larger supervision tree.