Crates.io | throttlecrab-server |
lib.rs | throttlecrab-server |
version | 0.4.31 |
created_at | 2025-07-30 19:14:21.343926+00 |
updated_at | 2025-09-14 18:33:05.103458+00 |
description | A high-performance rate limiting server with multiple protocol support |
homepage | https://github.com/lazureykis/throttlecrab |
repository | https://github.com/lazureykis/throttlecrab |
max_upload_size | |
id | 1773918 |
size | 316,673 |
A high-performance rate limiting server with multiple protocol support, built on throttlecrab.
cargo install throttlecrab-server
Start the server and make rate-limited requests:
# Start the server with HTTP transport
throttlecrab-server --http --http-port 8080
# In another terminal, make requests with curl
# First request - allowed
curl -X POST http://localhost:8080/throttle \
-H "Content-Type: application/json" \
-d '{"key": "api-key-123", "max_burst": 3, "count_per_period": 10, "period": 60}'
# Response:
# {"allowed":true,"limit":3,"remaining":2,"reset_after":60,"retry_after":0}
# Make more requests to see rate limiting in action
curl -X POST http://localhost:8080/throttle \
-H "Content-Type: application/json" \
-d '{"key": "api-key-123", "max_burst": 3, "count_per_period": 10, "period": 60}'
# Response when rate limited:
# {"allowed":false,"limit":3,"remaining":0,"reset_after":58,"retry_after":6}
All CLI arguments can be configured via environment variables with the THROTTLECRAB_
prefix:
# Transport configuration
export THROTTLECRAB_HTTP=true
export THROTTLECRAB_HTTP_HOST=0.0.0.0
export THROTTLECRAB_HTTP_PORT=8080
export THROTTLECRAB_REDIS=true
export THROTTLECRAB_REDIS_HOST=0.0.0.0
export THROTTLECRAB_REDIS_PORT=6379
# Store configuration
export THROTTLECRAB_STORE=adaptive
export THROTTLECRAB_STORE_CAPACITY=200000
export THROTTLECRAB_STORE_MIN_INTERVAL=10
# General configuration
export THROTTLECRAB_BUFFER_SIZE=100000
export THROTTLECRAB_LOG_LEVEL=info
# CLI arguments override environment variables
THROTTLECRAB_HTTP_PORT=8080 throttlecrab-server --http --http-port 7070
# Server will use port 7070 (CLI takes precedence)
Transport | Protocol | Throughput | Latency (P99) | Latency (P50) |
---|---|---|---|---|
HTTP | JSON | 175K req/s | 327 μs | 176 μs |
gRPC | Protobuf | 163K req/s | 377 μs | 188 μs |
Redis | RESP | 184K req/s | 275 μs | 170 μs |
You can run tests on your hardware with cd integration-tests && ./run-transport-test.sh -t all -T 32 -r 10000
Endpoint: POST /throttle
Request Body (JSON):
{
"key": "user:123",
"max_burst": 10,
"count_per_period": 100,
"period": 60,
"quantity": 1
}
Note: quantity
is optional (defaults to 1).
Response (JSON):
{
"allowed": true,
"limit": 10,
"remaining": 9,
"reset_after": 60,
"retry_after": 0
}
See proto/throttlecrab.proto
for the service definition. Use any gRPC client library to connect.
The server implements Redis Serialization Protocol (RESP), making it compatible with any Redis client.
Port: Default 6379 (configurable with --redis-port
)
Commands:
THROTTLE key max_burst count_per_period period [quantity]
- Check rate limitPING
- Health checkQUIT
- Close connectionExample using redis-cli:
redis-cli -p 6379
> THROTTLE user:123 10 100 60
1) (integer) 1 # allowed (1=yes, 0=no)
2) (integer) 10 # limit
3) (integer) 9 # remaining
4) (integer) 60 # reset_after (seconds)
5) (integer) 0 # retry_after (seconds)
Example using Redis client libraries:
import redis
r = redis.Redis(host='localhost', port=6379)
result = r.execute_command('THROTTLE', 'user:123', 10, 100, 60)
# result: [1, 10, 9, 60, 0]
Use any HTTP client, gRPC client library, or Redis client to connect to throttlecrab-server. See examples/
directory for implementation examples.
GET /health
(available on HTTP port)GET /metrics
(Prometheus format, available on HTTP port)/metrics
endpointthrottlecrab_uptime_seconds
: Server uptime in secondsthrottlecrab_requests_total
: Total requests processed across all transportsthrottlecrab_requests_by_transport{transport="http|grpc|redis"}
: Requests per transportthrottlecrab_requests_allowed
: Total allowed requeststhrottlecrab_requests_denied
: Total denied requeststhrottlecrab_requests_errors
: Total internal errorsthrottlecrab_top_denied_keys{key="...",rank="1-100"}
: Top denied keys by count# Monitor denial rate
rate(throttlecrab_requests_denied[5m]) / rate(throttlecrab_requests_total[5m])
# Alert on high error rate
rate(throttlecrab_requests_errors[5m]) > 0.01
Store Type | Use Case | Cleanup Strategy |
---|---|---|
periodic |
Predictable load | Fixed intervals |
probabilistic |
High throughput | Random sampling |
adaptive |
Variable load | Self-tuning |