Search in sources :

Example 1 with SimpleRequestBuilder

use of org.apache.hc.client5.http.async.methods.SimpleRequestBuilder in project bender by Nextdoor.

the class Http2Transport method sendBatch.

public void sendBatch(byte[] raw) throws TransportException {
    /*
     * Wrap the call with retry logic to avoid intermittent ES issues.
     */
    Callable<SimpleHttpResponse> callable = () -> {
        SimpleHttpResponse resp;
        String responseString = null;
        SimpleRequestBuilder rb = SimpleRequestBuilder.post().setHttpHost(this.target).setPath(this.url.getPath());
        /*
       * Do the call, read response, release connection so it is available for use again, and
       * finally check the response.
       */
        if (this.useGzip) {
            rb = rb.addHeader("Accept-Encoding", "gzip");
            resp = execute(rb, raw, ContentType.DEFAULT_BINARY);
        } else {
            resp = execute(rb, raw, getUncompressedContentType());
        }
        responseString = resp.getBodyText();
        if (responseString == null || responseString == "") {
            responseString = "Empty Reponse";
        }
        checkResponse(resp, responseString);
        return resp;
    };
    RetryConfig config = new RetryConfigBuilder().retryOnSpecificExceptions(TransportException.class).withMaxNumberOfTries(this.retries + 1).withDelayBetweenTries(this.retryDelayMs, ChronoUnit.MILLIS).withExponentialBackoff().build();
    try {
        new CallExecutor(config).execute(callable);
    } catch (RetriesExhaustedException ree) {
        logger.warn("transport failed after " + ree.getCallResults().getTotalTries() + " tries.");
        throw new TransportException(ree.getCallResults().getLastExceptionThatCausedRetry());
    } catch (UnexpectedException ue) {
        throw new TransportException(ue);
    }
}
Also used : RetryConfigBuilder(com.evanlennick.retry4j.RetryConfigBuilder) CallExecutor(com.evanlennick.retry4j.CallExecutor) UnexpectedException(com.evanlennick.retry4j.exception.UnexpectedException) RetryConfig(com.evanlennick.retry4j.RetryConfig) RetriesExhaustedException(com.evanlennick.retry4j.exception.RetriesExhaustedException) SimpleRequestBuilder(org.apache.hc.client5.http.async.methods.SimpleRequestBuilder) TransportException(com.nextdoor.bender.ipc.TransportException) SimpleHttpResponse(org.apache.hc.client5.http.async.methods.SimpleHttpResponse)

Aggregations

CallExecutor (com.evanlennick.retry4j.CallExecutor)1 RetryConfig (com.evanlennick.retry4j.RetryConfig)1 RetryConfigBuilder (com.evanlennick.retry4j.RetryConfigBuilder)1 RetriesExhaustedException (com.evanlennick.retry4j.exception.RetriesExhaustedException)1 UnexpectedException (com.evanlennick.retry4j.exception.UnexpectedException)1 TransportException (com.nextdoor.bender.ipc.TransportException)1 SimpleHttpResponse (org.apache.hc.client5.http.async.methods.SimpleHttpResponse)1 SimpleRequestBuilder (org.apache.hc.client5.http.async.methods.SimpleRequestBuilder)1