Package org.microproxy.extras
Class ConcurrencyLimiter.Builder
java.lang.Object
org.microproxy.extras.ConcurrencyLimiter.Builder
- Enclosing class:
ConcurrencyLimiter
Configures a
ConcurrencyLimiter.-
Method Summary
Modifier and TypeMethodDescriptionbuild()Creates the configured concurrency limiter.countTunnels(boolean countTunnels) Whether aCONNECTtunnel (and an upgraded WebSocket connection) holds its permit for its lifetime (default true).What requests are counted by: the client's IP address by default (ConcurrencyLimiter.byClientIp()), orConcurrencyLimiter.byUser(),ConcurrencyLimiter.byTargetHost()or any function of the request head and client connection.onReject(ConcurrencyLimiter.RejectListener listener) Called for every request over the limit (also in shadow mode), on its connection's thread.permits(int permits) Exchanges per key that may run at once (default 16); 0 refuses every request.Permits for particular keys;nullmeanspermits(int).permitTimeout(Duration permitTimeout) Reclaims (and logs) a permit held for longer than this, in case an exchange never ends; default 10 minutes,Duration.ZEROto turn the safety net off.Lets up tomaxWaitingrequests per key wait up tomaxWaiteach for a permit before they are refused.response(ConcurrencyLimiter.Responder responder) Answers refused requests instead of the default plain-text429.retryAfter(Duration retryAfter) TheRetry-Afterof the default429, rounded up to seconds; null for none.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.
-
Method Details
-
key
What requests are counted by: the client's IP address by default (ConcurrencyLimiter.byClientIp()), orConcurrencyLimiter.byUser(),ConcurrencyLimiter.byTargetHost()or any function of the request head and client connection. Anullkey leaves the request unlimited.- Parameters:
key- the function assigning each request to a concurrency group- Returns:
- this builder
-
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
Permits for particular keys;nullmeanspermits(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
Lets up tomaxWaitingrequests per key wait up tomaxWaiteach for a permit before they are refused. The default, 0, refuses at once.- Parameters:
maxWaiting- the maximum queued requests per keymaxWait- the maximum wait for a permit- Returns:
- this builder
-
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
Whether aCONNECTtunnel (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 the101has been sent; requests inside intercepted sessions are counted either way.- Parameters:
countTunnels- whether tunnels retain permits for their lifetime- Returns:
- this builder
-
retryAfter
TheRetry-Afterof the default429, rounded up to seconds; null for none. Default 1 s.- Parameters:
retryAfter- the retry delay advertised on rejection, ornullto omit it- Returns:
- this builder
-
response
Answers refused requests instead of the default plain-text429.- 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
Reclaims (and logs) a permit held for longer than this, in case an exchange never ends; default 10 minutes,Duration.ZEROto 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
Creates the configured concurrency limiter.- Returns:
- the configured concurrency limiter
-