@Override
    public void dispatch(HttpRequest req, CompletableFuture<HttpResponse> vessel) {
        client.getExecutor().execute(() -> {
            Endpoint endpoint = findLeastBusyEndpoint(endpoints);
            try {
                endpoint.inflight.incrementAndGet();
                long reqTimeoutMillis = req.timeLeft().toMillis();
                if (reqTimeoutMillis <= 0) {
                    vessel.completeExceptionally(new TimeoutException("operation timed out after '" + req.timeout() + "'"));
                    return;
                }
                Request jettyReq = client.newRequest(URI.create(endpoint.uri + req.pathAndQuery()))
                        .version(HttpVersion.HTTP_2)
                        .method(HttpMethod.fromString(req.method()))
                        .headers(hs -> req.headers().forEach((k, v) -> hs.add(k, v.get())))
                        .idleTimeout(IDLE_TIMEOUT.toMillis(), MILLISECONDS)
                        .timeout(reqTimeoutMillis, MILLISECONDS);
                if (req.body() != null) {
                    boolean shouldCompress = compression == gzip || compression == auto && req.body().length > 512;
                    byte[] bytes;
                    if (shouldCompress) {
                        ByteArrayOutputStream buffer = new ByteArrayOutputStream(1 << 10);
                        try (GZIPOutputStream zip = new GZIPOutputStream(buffer)) {
                            zip.write(req.body());
                        } catch (IOException e) { throw new UncheckedIOException(e); }
                        bytes = buffer.toByteArray();
                        jettyReq.headers(hs -> hs.add(HttpHeader.CONTENT_ENCODING, "gzip"));
                    } else {
                        bytes = req.body();
                    }
                    jettyReq.body(new BytesRequestContent(APPLICATION_JSON.asString(), bytes));
                }
                log.log(Level.FINER, () ->
                        String.format("Dispatching request %s (%s)", req, System.identityHashCode(vessel)));
                jettyReq.send(new BufferingResponseListener() {
                    @Override
                    public void onComplete(Result result) {
                        log.log(Level.FINER, () ->
                                String.format("Completed request %s (%s): %s",
                                        req, System.identityHashCode(vessel),
                                        result.isFailed()
                                                ? result.getFailure().toString() : result.getResponse().getStatus()));
                        endpoint.inflight.decrementAndGet();
                        if (result.isFailed()) {
                            if (result.getFailure() instanceof RetryableRequestException)
                                // Request can be safely retried as it was cancelled locally
                                // https://github.com/jetty/jetty.project/issues/13131
                                vessel.completeExceptionally(new RetryableException(result.getFailure()));
                            else
                                vessel.completeExceptionally(result.getFailure());
                        }
                        else vessel.complete(new JettyResponse(result.getResponse(), getContent()));
                    }
                });
            } catch (Throwable t) {
                log.log(t instanceof Exception ? Level.FINE : Level.WARNING, "Failed to dispatch request: " + req, t.getMessage());
                endpoint.inflight.decrementAndGet();
                vessel.completeExceptionally(t);
            }
        });
    }
Client implementation based on Jetty HTTP Client

@author bjorncs
vespa-feed-client/src/main/java/ai/vespa/feed/client/impl/JettyCluster.java