Backend API. Server-side code: this runs in a Codename One backend, not in the app on the device.

public final class JdbcRateLimiter

  1. Object
  2. JdbcRateLimiter

ImplementsRateLimiter

Counts in the server’s database, in the cn1_rate_limit table of SecuritySchema, so that every process of a deployment shares one limit.

http.rateLimit("/login", RateLimitKeys.clientAddress(),
        new JdbcRateLimiter(dataSource, "login", 5, 60));

A fixed window: permits requests in each periodSeconds, counted from the first request of the window. Every request is decided by one conditional UPDATE and the number of rows it changed, so two processes counting at the same moment cannot both take the last permit.

It costs a statement or two per request it counts, which is the price of agreeing across processes; InMemoryRateLimiter costs none and counts for its own process alone. A database that cannot be reached refuses nobody: the limit is a courtesy to the server, and a store that is down must not take the application with it.

Constructors

public JdbcRateLimiter(DataSource dataSource, String name, int permits, long periodSeconds)

Methods

public RateLimiter derive(String name, int permits, long periodSeconds)A limiter over the same table, under name, read on the same clock.
public void setClock(Clock clock)
public boolean tryAcquire(String key)Counts one request under key.
public void reset(String key)Deletes the row of key.
public long retryAfterSeconds(String key)How many seconds a request just refused under key should wait before trying again: what its Retry-After says.
public int deleteExpired(long olderThanSeconds) throws IOExceptionDeletes the counts of every limiter whose window ended more than olderThanSeconds ago; for a scheduled job, since a key that is never seen again leaves its row behind.

Inherited methods

Constructor details

JdbcRateLimiter

public JdbcRateLimiter(DataSource dataSource, String name, int permits, long periodSeconds)

Parameters

dataSource DataSource
Not documented.
name String
what keeps this limiter’s counts apart from another’s in the same table; two limiters of one name share their counts on purpose
permits int
Not documented.
periodSeconds long
Not documented.

Method details

derive

public RateLimiter derive(String name, int permits, long periodSeconds)
A limiter over the same table, under name, read on the same clock.

Parameters

name String
what keeps the new limiter’s counts apart
permits int
how many requests a key may make in a period
periodSeconds long
the length of the period

setClock

public void setClock(Clock clock)

tryAcquire

public boolean tryAcquire(String key)
Counts one request under key.

Returns

whether the request is within the limit

reset

public void reset(String key)
Deletes the row of key. A database that cannot be reached leaves the count as it is, which is the safe way round for a bound on guesses.

retryAfterSeconds

public long retryAfterSeconds(String key)
How many seconds a request just refused under key should wait before trying again: what its Retry-After says. One second unless the limiter knows better.

deleteExpired

public int deleteExpired(long olderThanSeconds) throws IOException
Deletes the counts of every limiter whose window ended more than olderThanSeconds ago; for a scheduled job, since a key that is never seen again leaves its row behind.

Returns

how many rows were deleted