Package dev.relism.flash.ext.limiter
Class Guard
java.lang.Object
dev.relism.flash.ext.limiter.Guard
Manual rate-limit guard for lambda routes.
Available via FlashContext:
Guard guard = ctx.require(Guard.class);
limit(java.lang.String, int, long, java.util.concurrent.TimeUnit) creates a Middleware that is composed once at route registration
time — the resolver lambda is captured directly from the registry (no runtime map lookup):
// 50 req/s per IP — fixed window (default)
app.get("/api/search", handler, guard.limit("ip", 50, 1, TimeUnit.SECONDS));
// 10 req/min per authenticated user — token bucket
app.post("/api/export", handler, guard.limit("auth_user", 10, 1, TimeUnit.MINUTES, LimitStrategy.TOKEN_BUCKET));
The resolver name is looked up once here (at wiring time, not on each request).
If the name is not registered, InitializationException
is thrown immediately.
-
Method Summary
Modifier and TypeMethodDescriptionReturns aMiddlewarethat enforces the given rate limit usingLimitStrategy.FIXED_WINDOW.limit(String resolverKey, int requests, long window, TimeUnit unit, LimitStrategy strategy) Returns aMiddlewarethat enforces the given rate limit with the specified strategy.
-
Method Details
-
limit
Returns aMiddlewarethat enforces the given rate limit usingLimitStrategy.FIXED_WINDOW.- Parameters:
resolverKey- name registered viaLimiterConfig.registerResolver(java.lang.String, dev.relism.flash.ext.limiter.KeyResolver)requests- maximum requests allowed per windowwindow- window duration inunitunit- time unit forwindow
-
limit
public Middleware limit(String resolverKey, int requests, long window, TimeUnit unit, LimitStrategy strategy) Returns aMiddlewarethat enforces the given rate limit with the specified strategy.- Parameters:
resolverKey- name registered viaLimiterConfig.registerResolver(java.lang.String, dev.relism.flash.ext.limiter.KeyResolver)requests- maximum requests allowed per windowwindow- window duration inunitunit- time unit forwindowstrategy- rate-limit algorithm
-