Class ConcurrencyLimiter.Builder

java.lang.Object
org.microproxy.extras.ConcurrencyLimiter.Builder
Enclosing class:
ConcurrencyLimiter

public static final class ConcurrencyLimiter.Builder extends Object
Configures a ConcurrencyLimiter.
  • Method Details

    • key

      What requests are counted by: the client's IP address by default (ConcurrencyLimiter.byClientIp()), or ConcurrencyLimiter.byUser(), ConcurrencyLimiter.byTargetHost() or any function of the request head and client connection. A null key leaves the request unlimited.
      Parameters:
      key - the function assigning each request to a concurrency group
      Returns:
      this builder
    • permits

      public ConcurrencyLimiter.Builder permits(int permits)
      Exchanges per key that may run at once (default 16); 0 refuses every request.
      Parameters:
      permits - the maximum concurrent exchanges per key
      Returns:
      this builder
    • permits

      public ConcurrencyLimiter.Builder permits(Function<String,Integer> permitsPerKey)
      Permits for particular keys; null means permits(int). Asked when a key becomes busy, and kept until it is idle again.
      Parameters:
      permitsPerKey - the function selecting the permit limit for each key
      Returns:
      this builder
    • queue

      public ConcurrencyLimiter.Builder queue(int maxWaiting, Duration maxWait)
      Lets up to maxWaiting requests per key wait up to maxWait each for a permit before they are refused. The default, 0, refuses at once.
      Parameters:
      maxWaiting - the maximum queued requests per key
      maxWait - the maximum wait for a permit
      Returns:
      this builder
    • shadow

      public ConcurrencyLimiter.Builder shadow(boolean shadow)
      Counts and reports requests over the limit (onReject, ConcurrencyLimiter.snapshot()) but lets them through, to try a limit out before enforcing it.
      Parameters:
      shadow - whether to report excess requests while allowing them through
      Returns:
      this builder
    • countTunnels

      public ConcurrencyLimiter.Builder countTunnels(boolean countTunnels)
      Whether a CONNECT tunnel (and an upgraded WebSocket connection) holds its permit for its lifetime (default true). With false, CONNECTs are not counted and upgraded connections give their permit back once the 101 has been sent; requests inside intercepted sessions are counted either way.
      Parameters:
      countTunnels - whether tunnels retain permits for their lifetime
      Returns:
      this builder
    • retryAfter

      public ConcurrencyLimiter.Builder retryAfter(Duration retryAfter)
      The Retry-After of the default 429, rounded up to seconds; null for none. Default 1 s.
      Parameters:
      retryAfter - the retry delay advertised on rejection, or null to omit it
      Returns:
      this builder
    • response

      Answers refused requests instead of the default plain-text 429.
      Parameters:
      responder - the function creating a rejection or failure response
      Returns:
      this builder
    • onReject

      Called for every request over the limit (also in shadow mode), on its connection's thread.
      Parameters:
      listener - the rejection callback
      Returns:
      this builder
    • permitTimeout

      public ConcurrencyLimiter.Builder permitTimeout(Duration permitTimeout)
      Reclaims (and logs) a permit held for longer than this, in case an exchange never ends; default 10 minutes, Duration.ZERO to turn the safety net off. Established tunnels are exempt. Set it above the slowest exchange you expect.
      Parameters:
      permitTimeout - the maximum permit lifetime, or zero to disable expiry
      Returns:
      this builder
    • build

      public ConcurrencyLimiter build()
      Creates the configured concurrency limiter.
      Returns:
      the configured concurrency limiter