Interface RateLimiter
- All Known Implementing Classes:
InMemoryRateLimiter, JdbcRateLimiter
Counts requests under a key and says when there have been too many.
An application that keeps its counts somewhere of its own -- a database, so that several servers share one count -- implements this.
-
Method Summary
Modifier and TypeMethodDescriptiondefault RateLimiterA limiter that keeps its counts where this one does, apart from this one's, with a limit of its own; null when this limiter cannot make one, which is what it answers unless it says more.default voidForgets what was counted underkey, so the next request under it is counted from nothing.default longretryAfterSeconds(String key) How many seconds a request just refused underkeyshould wait before trying again: what itsRetry-Aftersays.booleantryAcquire(String key) Counts one request underkey.
-
Method Details
-
tryAcquire
Counts one request underkey.- Returns:
- whether the request is within the limit
-
retryAfterSeconds
How many seconds a request just refused underkeyshould wait before trying again: what itsRetry-Aftersays. One second unless the limiter knows better. -
reset
Forgets what was counted under
key, so the next request under it is counted from nothing.A bound on wrong guesses uses this: the guess is counted before it is looked at, so that guesses made together cannot each be let through as the last one, and a right one hands the count back. A limiter that cannot forget does nothing here, which is what it does unless it says more; a right guess then costs what a wrong one does.
-
derive
A limiter that keeps its counts where this one does, apart from this one's, with a limit of its own; null when this limiter cannot make one, which is what it answers unless it says more.
The parts of the layer that count something themselves -- attempts at a second factor, tries at a device's code -- ask the application's limiter bean for one each. That is how a bean declared to throttle an API at 600 a minute comes to count guesses at a one-time code at five in five minutes, in the same database, without the two sharing either a count or a limit.
- Parameters:
name- what keeps the new limiter's counts apartpermits- how many requests a key may make in a periodperiodSeconds- the length of the period
-