Railsr.Middleware.RateLimiter (Railsr v1.0.0)

Copy Markdown View Source

Client-side token-bucket rate limiter.

Prevents the client from exceeding rate_limit_rps requests per second, providing a first line of defence before Railsr returns a 429.

The bucket refills continuously. If the bucket is empty, check_and_consume/0 returns {:error, :rate_limited}.

Summary

Functions

Attempt to consume one token from the bucket.

Returns a specification to start this module under a supervisor.

Start the rate limiter process.

Functions

check_and_consume()

@spec check_and_consume() :: :ok | {:error, :rate_limited}

Attempt to consume one token from the bucket.

Returns :ok if the request should proceed, {:error, :rate_limited} otherwise.

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

start_link(opts \\ [])

@spec start_link(keyword()) :: GenServer.on_start()

Start the rate limiter process.