Repository navigation
Rate Limiting
Laravex provides rate limiting through the throttle middleware alias and named limiters registered with Laravex.Http.RateLimiter. Named limiters are defined once and referenced by name in route or middleware group definitions.
Register a named limiter anywhere before your routes are matched — typically in a Service-Providers or in bootstrap/app.ex:
# Named limiter with a fixed rate
Laravex.Http.RateLimiter.for("api", fn _request ->
[max: 60, decay: 1]
end)
# Named limiter that varies by user
Laravex.Http.RateLimiter.for("uploads", fn request ->
if request.user && request.user.role == "premium" do
[max: 1000, decay: 1]
else
[max: 100, decay: 1]
end
end)The callback receives the current %Laravex.Http.Request{} struct and must return a keyword list with:
| Key | Type | Description |
|---|---|---|
:max |
integer | Maximum number of requests allowed within the :decay window |
:decay |
integer | Window duration in minutes |
If no limiter is registered for a given name, the system defaults to [max: 60, decay: 1].
Apply rate limiting with the throttle middleware alias. It resolves to Laravex.Http.Middleware.ThrottleRequests.
Router.middleware("throttle:60,1", fn ->
# 60 requests per 1 minute
Router.get("/api/posts", &App.Http.Controllers.PostController.index/1)
end)
# shorthand: throttle:{max},{decay}
"throttle:100,1" # 100 per minute
"throttle:30" # 30 per minute (decay defaults to 1)
"throttle" # 60 per minute (global default)# Applies the "api" named limiter
Router.middleware("throttle:api", fn ->
Router.get("/api/resource", &handler/1)
end)The default api middleware group already includes throttling with the "api" named limiter:
# lib/laravex/http/middleware.ex (api group)
{Laravex.Http.Middleware.ThrottleRequests, [limiter: "api"]}This applies whatever rate you registered for "api" to all routes in the api group.
You can resolve the computed limit for a given limiter name programmatically:
# Without a request (limiter callbacks that accept 0 arguments)
Laravex.Http.RateLimiter.resolve("api")
# => [max: 60, decay: 1]
# With a request (limiter callbacks that accept 1 argument)
Laravex.Http.RateLimiter.resolve("uploads", request)
# => [max: 1000, decay: 1]# Returns the registered callback function (or nil if not found)
Laravex.Http.RateLimiter.get("api")The ThrottleRequests middleware:
- Resolves the limiter (by name or inline
max/decayvalues). - Builds a cache key from the limiter name and the request's remote IP (or authenticated user ID).
- Checks the Cache for the current request count.
- If the count exceeds
max, returns a429 Too Many Requestsresponse with aRetry-Afterheader. - Otherwise, increments the count (with a TTL of
decayminutes) and continues.
Laravex.Http.RateLimiter.for("orders", fn request ->
case request.user do
nil -> [max: 10, decay: 1]
user -> [max: user.daily_order_limit, decay: 60]
end
end)Router.middleware("throttle:orders", fn ->
Router.post("/orders", &App.Http.Controllers.OrderController.store/1)
end)-
Middleware —
throttlealias resolution - Cache — rate limiter state is stored in the cache
- Routing — attaching middleware to route groups
Laravex — Laravel-faithful application skeleton & runtime for Elixir.
GitHub
·
Docs
·
Limitations
·
Laravel Docs
Not affiliated with Laravel LLC or the Elixir core team. Built to feel like Laravel on the BEAM.
- Home
- Installation
- Configuration
- Directory-Structure
- Request-Lifecycle
- Service-Container
- Service-Providers
- Deployment
- Routing
- Middleware
- CSRF-Protection
- Controllers
- Requests
- Responses
- Views
- Blade-Templates
- Asset-Bundling
- Session
- Validation
- Error-Handling
- Logging
- Artisan-Console
- Broadcasting
- Cache
- Collections
- Events
- File-Storage
- Helpers
- HTTP-Client
- Localization
- Notifications
- Pipeline
- Queues
- Rate-Limiting
- Strings
- Task-Scheduling