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

package com.codename1.backend.security.ratelimit

Limits on how often a client may ask: a RateLimiter counts requests under a key, and a RateLimitKeyResolver says which key a request counts under – its client’s address, who it is signed in as, its session, its API key.

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

A request over its limit is answered 429 with Retry-After, before it reaches anything that costs: a password hash, a query.

Types

class InMemoryRateLimiterA limit kept in this process’s memory: a token bucket per key.
class JdbcRateLimiterCounts in the server’s database, in the cn1_rate_limit table of SecuritySchema, so that every process of a deployment shares one limit.
class KeyValueRateLimiterCounts in the server’s key/value store – Redis once cn1.kv.url names one – so every process of a deployment shares one limit, at one round trip per request and with no table to clean up.
interface RateLimitKeyResolverSays which key a request counts under.
class RateLimitKeysThe keys a request is usually counted under.
interface RateLimiterCounts requests under a key and says when there have been too many.