Class RetryingHttpClient

java.lang.Object
co.elastic.clients.transport.http.RetryingHttpClient
All Implemented Interfaces:
TransportHttpClient

public final class RetryingHttpClient extends Object implements TransportHttpClient
A 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:

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 by close(); its thread is started lazily on the first retry and stopped while idle. A scheduler can also be supplied via RetryingHttpClient(TransportHttpClient, ScheduledExecutorService), in which case its lifecycle is the caller's responsibility.
  • Constructor Details

    • RetryingHttpClient

      public RetryingHttpClient(TransportHttpClient delegate)
    • RetryingHttpClient

      public RetryingHttpClient(TransportHttpClient delegate, ScheduledExecutorService scheduler)
      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

      public TransportOptions createOptions(@Nullable TransportOptions options)
      Description copied from interface: TransportHttpClient
      Create a client-specific options value from an existing option object. If null, 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:
      createOptions in interface TransportHttpClient
    • responseStatusCode

      @Nullable public Integer responseStatusCode(Throwable exception)
      Description copied from interface: TransportHttpClient
      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, otherwise null.

      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:
      responseStatusCode in interface TransportHttpClient
    • performRequest

      public TransportHttpClient.Response performRequest(String endpointId, @Nullable TransportHttpClient.Node node, TransportHttpClient.Request request, TransportOptions options) throws IOException
      Description copied from interface: TransportHttpClient
      Perform a blocking request.
      Specified by:
      performRequest in interface TransportHttpClient
      Parameters:
      endpointId - the endpoint identifier. Can be used to have specific strategies depending on the endpoint.
      node - the node to send the request to. If null, the implementation has to choose which node to send the request to, or throw an IllegalArgumentException.
      request - the request
      options - 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: TransportHttpClient
      Perform an asynchronous request.

      Implementations should return a CompletableFuture whose 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:
      performRequestAsync in interface TransportHttpClient
      Parameters:
      endpointId - the endpoint identifier. Can be used to have specific strategies depending on the endpoint.
      node - the node to send the request to. If null, the implementation has to choose which node to send the request to, or throw an IllegalArgumentException.
      request - the request
      options - 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

      public void close() throws IOException
      Description copied from interface: TransportHttpClient
      Close this client, freeing associated resources.
      Specified by:
      close in interface TransportHttpClient
      Throws:
      IOException