Interface RateLimitKey

Functional Interface:
This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.

@FunctionalInterface public interface RateLimitKey
Chooses the key a request is counted under by RateLimit: one budget per distinct key.

A key is text of 1 to 512 characters. It is built from facts the server established (the transport peer, a trusted proxy's view, the verified identity), never from a request header an attacker chooses freely; otherwise a client would pick a fresh key per request and never be limited. RateLimit bounds how many keys it tracks, so a key function that is flooded with new values evicts old entries instead of growing memory.

An empty or oversized result means the request has no usable key. RateLimit then counts it under ANONYMOUS, one shared budget, so keyless requests are still limited.

Implementations are called concurrently and must be thread-safe and fast.

  • Field Details

  • Method Details

    • key

      Optional<String> key(Context context)
      Returns the key of the request.
      Parameters:
      context - request context
      Returns:
      the key, or empty when this function has none
    • peerAddress

      static RateLimitKey peerAddress()
      Keys by the transport peer address (Request.remoteAddress()), ignoring every header. This is the default of RateLimit: it cannot be spoofed, but behind a reverse proxy every client shares the proxy's address; use clientAddress(TrustedProxies) there.
      Returns:
      the peer-address key function
    • clientAddress

      static RateLimitKey clientAddress(TrustedProxies proxies)
      Keys by the client address resolved through trusted proxies: forwarding headers are believed only from configured proxies, as in TrustedProxies.resolve(com.jsgalactic.axiom.http.Request).
      Parameters:
      proxies - the trusted proxy configuration
      Returns:
      the client-address key function
    • principal

      static RateLimitKey principal(RateLimitKey anonymous)
      Keys by the authenticated principal, falling back to another function for anonymous requests. Place the authenticating middleware before the limiter.
      Parameters:
      anonymous - used when the request has no identity, typically peerAddress()
      Returns:
      the principal key function