Class RetryingHttpClient
- All Implemented Interfaces:
TransportHttpClient
TransportHttpClient wrapper that retries failed requests according to the RetryConfig
carried by each request's TransportOptions.
The retry configuration is read from the TransportOptions passed to every call (see
TransportOptions.retryConfig()), so it can be set globally on the client or overridden per request.
When the effective configuration is disabled
(RetryConfig.isEnabled() returns false), the call is forwarded to the delegate unchanged.
A request is retried when:
- the delegate returns a response whose status code is in
RetryConfig.retryableStatuses(), or - the delegate fails with an exception matching
RetryConfig.retryableExceptions(). Exceptions carrying a response are always classified by their status code, never by exception type.
Notes:
- Retries assume the underlying request is safe to repeat. Most Elasticsearch APIs are idempotent at the user level, but users issuing custom non-idempotent operations should think twice before enabling retries.
- Retries reissue the same logical request to the
delegate. Node selection (and any node-rotation / dead-node tracking) is the delegate's responsibility: this wrapper does not influence which node a retried request is sent to. - Backoff delays are scheduled on a
ScheduledExecutorService. A single daemon-thread scheduler is created with this client and shut down byclose(); its thread is started lazily on the first retry and stopped while idle. A scheduler can also be supplied viaRetryingHttpClient(TransportHttpClient, ScheduledExecutorService), in which case its lifecycle is the caller's responsibility.
-
Nested Class Summary
Nested classes/interfaces inherited from interface co.elastic.clients.transport.http.TransportHttpClient
TransportHttpClient.Node, TransportHttpClient.Request, TransportHttpClient.Response -
Constructor Summary
ConstructorsConstructorDescriptionRetryingHttpClient(TransportHttpClient delegate) RetryingHttpClient(TransportHttpClient delegate, ScheduledExecutorService scheduler) Build a retrying client using a user-provided scheduler. -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Close this client, freeing associated resources.createOptions(TransportOptions options) Create a client-specific options value from an existing option object.performRequest(String endpointId, TransportHttpClient.Node node, TransportHttpClient.Request request, TransportOptions options) Perform a blocking request.performRequestAsync(String endpointId, TransportHttpClient.Node node, TransportHttpClient.Request request, TransportOptions options) Perform an asynchronous request.responseStatusCode(Throwable exception) If the given exception carries an http response — like the low-level clients'ResponseException, which reports non-2xx statuses as an exception rather than as a response — returns that response's status code, otherwisenull.
-
Constructor Details
-
RetryingHttpClient
-
RetryingHttpClient
Build a retrying client using a user-provided scheduler. The external scheduler won't be shut down or reconfigured by this client.
-
-
Method Details
-
createOptions
Description copied from interface:TransportHttpClientCreate a client-specific options value from an existing option object. Ifnull, this must create the default options to which additional options can be added.This method allows implementations to return subclasses with more features (that applications can use by downcasting the result). By default, it will use
DefaultTransportOptions.- Specified by:
createOptionsin interfaceTransportHttpClient
-
responseStatusCode
Description copied from interface:TransportHttpClientIf the given exception carries an http response — like the low-level clients'ResponseException, which reports non-2xx statuses as an exception rather than as a response — returns that response's status code, otherwisenull.This allows implementation-agnostic wrappers (e.g.
RetryingHttpClient) to classify such failures by status code without knowing the underlying http library. Implementations should override this for their own response-carrying exception types.- Specified by:
responseStatusCodein interfaceTransportHttpClient
-
performRequest
public TransportHttpClient.Response performRequest(String endpointId, @Nullable TransportHttpClient.Node node, TransportHttpClient.Request request, TransportOptions options) throws IOException Description copied from interface:TransportHttpClientPerform a blocking request.- Specified by:
performRequestin interfaceTransportHttpClient- Parameters:
endpointId- the endpoint identifier. Can be used to have specific strategies depending on the endpoint.node- the node to send the request to. Ifnull, the implementation has to choose which node to send the request to, or throw anIllegalArgumentException.request- the requestoptions- additional options for the http client. Headers and request parameters set in the options have precedence over those defined by the request and should replace them in the final request sent.- Returns:
- the response
- Throws:
IOException
-
performRequestAsync
public CompletableFuture<TransportHttpClient.Response> performRequestAsync(String endpointId, @Nullable TransportHttpClient.Node node, TransportHttpClient.Request request, TransportOptions options) Description copied from interface:TransportHttpClientPerform an asynchronous request.Implementations should return a
CompletableFuturewhose cancellation also cancels any http request in flight and frees the associated resources. This allows applications to implement scenarios like timeouts or "first to respond" fan-out without leaking resources.- Specified by:
performRequestAsyncin interfaceTransportHttpClient- Parameters:
endpointId- the endpoint identifier. Can be used to have specific strategies depending on the endpoint.node- the node to send the request to. Ifnull, the implementation has to choose which node to send the request to, or throw anIllegalArgumentException.request- the requestoptions- additional options for the http client. Headers and request parameters set in the options have precedence over those defined by the request and should replace them in the final request sent.- Returns:
- a future that will be completed with the response.
-
close
Description copied from interface:TransportHttpClientClose this client, freeing associated resources.- Specified by:
closein interfaceTransportHttpClient- Throws:
IOException
-