Class BraveExecutionGate
BraveExecutionGate acts as a single-admission execution gate: at most one request may
be executed at a time. Callers attempting to submit a request while another request is in
progress will block until execution becomes available or until the calling thread is interrupted.
This class is designed to coordinate access to a rate-limited API token. It ensures:
- Strict FIFO fairness between competing callers
- Global serialization of request execution
- Consistent handling of HTTP 429 (rate limit) responses
- Retry and backoff behavior based on server-provided rate limit metadata
Concurrency model: This gate uses a fair ReentrantLock to provide mutual exclusion. Lock acquisition is
interruptible, allowing callers to cancel admission while waiting. Rate-limit backoff is
performed while holding the lock, ensuring that no subsequent request executes until the backoff
window has elapsed.
Blocking semantics: Calls to submit(HttpRequest) and submit(HttpRequest, int) are synchronous and may block for extended periods of time due to rate
limiting or retry backoff. This behavior is intentional and reflects the semantics of a shared,
rate-limited execution resource.
Default retry policy: Rate-limited (HTTP 429) responses are retried up to
DEFAULT_MAX_RETRIES times by default, sleeping for the
server-provided rate limit reset window before each attempt. The retry is what activates the
rate-aware backoff; without it, a rate-limited response would be returned without honoring the
server's backoff guidance.
Failure handling:
- Thread interruption during admission results in
RequestProcessingInterrupted - Thread interruption during retry/backoff results in
RequestRetryInterruptedException - Non-rate-limit HTTP 429 responses are treated as unrecoverable errors
This class does not perform authentication, request construction, or asynchronous scheduling. It is a coordination primitive, not a general-purpose executor.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionsubmit(@NotNull HttpRequest httpRequest) Submits a request for serialized execution with a default retry policy.submit(@NotNull HttpRequest httpRequest, int maxRetries) Submits a request for serialized execution through the execution gate.
-
Constructor Details
-
BraveExecutionGate
public BraveExecutionGate()
-
-
Method Details
-
submit
Submits a request for serialized execution through the execution gate.If another request is currently executing, the calling thread will block until execution becomes available or until the thread is interrupted.
The request will be executed synchronously and may be retried if the Brave API responds with a rate-limit (HTTP 429) error at least once. Retry behavior is governed by the
maxRetriesparameter and server-provided rate limit metadata.- Parameters:
httpRequest- theHttpRequestto executemaxRetries- the maximum number of retry attempts for rate-limited responses- Returns:
- the completed HTTP response
- Throws:
RequestProcessingInterrupted- if the thread is interrupted while waiting to acquire execution admissionRequestRetryInterruptedException- if the thread is interrupted during retry backoffBraveApiException- if an unrecoverable API error is returned by the server (e.g. Monthly limit exhaustion)BraveClientException- if a rate-limited response does not carry the rate limit window headers required to activate rate-aware backoff
-
submit
Submits a request for serialized execution with a default retry policy.This method is equivalent to calling
submit(HttpRequest, int)with thedefault retry policy.- Parameters:
httpRequest- theHttpRequestto execute- Returns:
- the completed HTTP response
- Throws:
RequestProcessingInterrupted- if the thread is interrupted while waiting to acquire execution admissionRequestRetryInterruptedException- if the thread is interrupted during retry backoffBraveApiException- if an unrecoverable API error occursBraveClientException- if a rate-limited response does not carry the rate limit window headers required to activate rate-aware backoff
-