Backend API. Server-side code: this runs in a Codename One backend, not in the app on the device.
public final class InMemoryRateLimiter
- Object
- InMemoryRateLimiter
ImplementsRateLimiter
A limit kept in this process’s memory: a token bucket per key.
RateLimiter logins = new InMemoryRateLimiter(5, 60); // 5 a minute, per key
A key starts with permits tokens and gets them back at permits per
period, evenly: a client may spend them all at once and is then let through
at the steady rate, which is what “5 a minute” means to the person waiting.
The count is per process. A server run as several instances behind a
load balancer has one count in each, so a client is allowed up to the limit
times the number of instances. Where the limit is there to keep load down
that is usually what is wanted. Where it is a security bound – attempts at
a password – count somewhere the instances share, behind the RateLimiter
interface. The counts are also gone when the process restarts.
Memory is bounded: no more than a set number of keys are remembered, 100000 unless set. A key whose bucket has refilled is forgotten, since forgetting it changes nothing; past the bound the keys idle longest are forgotten early, which gives such a key its tokens back sooner than it earned them. A flood of distinct keys therefore costs a bounded amount of memory, and the most it buys the flooder is the full bucket a new key starts with anyway.
Constructors
public InMemoryRateLimiter(int permits, long periodSeconds) | |
public InMemoryRateLimiter(int permits, long periodSeconds, int maxKeys) |
Methods
public synchronized RateLimiter derive(String name, int permits, long periodSeconds) | A limiter in this process with counts of its own, read on the same clock; the name is not needed to keep them apart. |
public synchronized void setClock(Clock clock) | Reads the time from clock instead of the machine. |
public synchronized int size() | How many keys are remembered now. |
public synchronized boolean tryAcquire(String key) | Counts one request under key. |
public synchronized void reset(String key) | Forgets what was counted under key, so the next request under it is counted from nothing. |
public synchronized long retryAfterSeconds(String key) | How many seconds a request just refused under key should wait before trying again: what its Retry-After says. |
Inherited methods
Constructor details
InMemoryRateLimiter
public InMemoryRateLimiter(int permits, long periodSeconds)Parameters
permitsint- how many requests a key may make in a period
periodSecondslong- the length of the period
InMemoryRateLimiter
public InMemoryRateLimiter(int permits, long periodSeconds, int maxKeys)Parameters
permitsint- Not documented.
periodSecondslong- Not documented.
maxKeysint- the most keys remembered at once
Method details
derive
public synchronized RateLimiter derive(String name, int permits, long periodSeconds)Parameters
nameString- what keeps the new limiter’s counts apart
permitsint- how many requests a key may make in a period
periodSecondslong- the length of the period
setClock
public synchronized void setClock(Clock clock)clock instead of the machine.size
public synchronized int size()tryAcquire
public synchronized boolean tryAcquire(String key)key.Returns
reset
public synchronized void reset(String key)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.
retryAfterSeconds
public synchronized long retryAfterSeconds(String key)key should wait before
trying again: what its Retry-After says. One second unless the limiter
knows better.