Libraries / ratelimit
Overview
Zero-dependency HolyPHP rate limiting library providing Token Bucket (TokenBucket) and Sliding Window (SlidingWindowLimiter) limiters with per-key tracking.
bundled — works offline
zero dependencies
throttle
bucket
Quick start
import "ratelimit";
// Token bucket: capacity 5 tokens, refills at 1 token/second
$bucket = new TokenBucket(5.0, 1.0);
$now = 1000.0;
if ($bucket->allow("user:42", $now)) {
println("Allowed! Remaining: " . $bucket->remaining("user:42", $now));
} else {
println("Rate limited. Retry after: " . $bucket->retryAfter("user:42", $now) . "s");
}
// Sliding window: max 3 requests per 10 seconds
$limiter = new SlidingWindowLimiter(3, 10.0);
$allowed = $limiter->allow("ip:127.0.0.1", $now);
Installation
In a project
Inside a project (a folder with hphp.yaml, see Projects & packages ):
hphp install ratelimit
This adds the library to hphp.yaml, installs it into .hphp/packages/ratelimit/ and pins the exact version in hphp.lock:
require:
ratelimit: "^1.0"
Use it
import "ratelimit";
ratelimit is bundled with the compiler, so import "ratelimit"; also works in a single file without installing anything, and hphp install works offline.
Outside a project
hphp install ratelimit # for your user, shared by all scripts
hphp create myapp # or start a project, then: hphp install ratelimit
Documentation
API Reference
TokenBucket
A per-key token bucket rate limiter supporting fractional capacities, refill rates, and custom request costs.
new TokenBucket(float $capacity, float $refillRate)
$capacity: Maximum tokens a key can hold (> 0).
$refillRate: Tokens added per second (> 0).
allow(string $key, float $now = -1.0, float $cost = 1.0): bool
Refills tokens up to $now (defaults to current time() if < 0), then consumes $cost tokens if available. Returns true if allowed, false otherwise.
remaining(string $key, float $now = -1.0): float
Returns the number of tokens currently available for $key at $now without consuming any.
retryAfter(string $key, float $now = -1.0, float $cost = 1.0): float
Returns the number of seconds until $cost tokens will be available (0.0 if already available).
reset(string $key): void
Clears the tracked bucket state for $key.
resetAll(): void
SlidingWindowLimiter
A per-key sliding-window log rate limiter that tracks request timestamps within a rolling window.
new SlidingWindowLimiter(int $limit, float $windowSeconds)
$limit: Maximum number of requests allowed per window (> 0).
$windowSeconds: Window duration in seconds (> 0).
allow(string $key, float $now = -1.0): bool
Prunes timestamps older than $now - $windowSeconds and records $now if under $limit. Returns true if allowed, false otherwise.
remaining(string $key, float $now = -1.0): int
Returns how many additional requests $key can make at $now.
retryAfter(string $key, float $now = -1.0): float
Returns the number of seconds until the oldest request in the window expires (0.0 if under the limit).
reset(string $key): void
Clears the tracked request log for $key.
resetAll(): void
Helper Functions
ratelimit_token_bucket(float $capacity, float $refillRate): TokenBucket
ratelimit_sliding_window(int $limit, float $windowSeconds): SlidingWindowLimiter
Error Handling
All invalid configurations and arguments throw an Exception prefixed with ratelimit::
ratelimit: capacity must be greater than 0
ratelimit: refillRate must be greater than 0
ratelimit: limit must be greater than 0
ratelimit: windowSeconds must be greater than 0
ratelimit: key must be a non-empty string
ratelimit: cost must be greater than 0
ratelimit: cost cannot exceed bucket capacity
Examples
01 Quick start
Bundled with the compiler — import "ratelimit"; works offline in any file, or add it to a project with hphp install ratelimit.
import "ratelimit";
// Token bucket: capacity 5 tokens, refills at 1 token/second
$bucket = new TokenBucket(5.0, 1.0);
$now = 1000.0;
if ($bucket->allow("user:42", $now)) {
println("Allowed! Remaining: " . $bucket->remaining("user:42", $now));
} else {
println("Rate limited. Retry after: " . $bucket->retryAfter("user:42", $now) . "s");
}
// Sliding window: max 3 requests per 10 seconds
$limiter = new SlidingWindowLimiter(3, 10.0);
$allowed = $limiter->allow("ip:127.0.0.1", $now);
Try it hphp install ratelimit
hphp run main.hphp
Covered by the official test suite in tests/positive/lib_ratelimit.hphp.