|
| 1 | +package datadog.communication.http.client.jetty; |
| 2 | + |
| 3 | +import datadog.communication.http.HttpRetryPolicy; |
| 4 | +import datadog.communication.http.client.HttpClient; |
| 5 | +import datadog.communication.http.client.HttpClientRequest; |
| 6 | +import datadog.communication.http.client.HttpClientResponse; |
| 7 | +import datadog.communication.http.client.HttpTransport; |
| 8 | +import java.io.IOException; |
| 9 | +import java.net.URI; |
| 10 | +import java.util.ArrayList; |
| 11 | +import java.util.LinkedHashMap; |
| 12 | +import java.util.List; |
| 13 | +import java.util.Map; |
| 14 | +import java.util.Set; |
| 15 | +import java.util.concurrent.ConcurrentHashMap; |
| 16 | +import java.util.concurrent.ExecutionException; |
| 17 | +import java.util.concurrent.TimeUnit; |
| 18 | +import java.util.concurrent.TimeoutException; |
| 19 | +import javax.annotation.Nullable; |
| 20 | +import org.eclipse.jetty.client.HttpClientTransport; |
| 21 | +import org.eclipse.jetty.client.HttpProxy; |
| 22 | +import org.eclipse.jetty.client.api.Authentication; |
| 23 | +import org.eclipse.jetty.client.api.ContentResponse; |
| 24 | +import org.eclipse.jetty.client.api.Request; |
| 25 | +import org.eclipse.jetty.client.util.BasicAuthentication; |
| 26 | +import org.eclipse.jetty.client.util.BytesContentProvider; |
| 27 | +import org.eclipse.jetty.http.HttpField; |
| 28 | +import org.eclipse.jetty.unixsocket.client.HttpClientTransportOverUnixSockets; |
| 29 | + |
| 30 | +final class JettyHttpClient implements HttpClient { |
| 31 | + private final long requestTimeoutMillis; |
| 32 | + private final HttpRetryPolicy.Factory retryPolicyFactory; |
| 33 | + private final org.eclipse.jetty.client.HttpClient client; |
| 34 | + private final boolean closeClientOnClose; |
| 35 | + private final Set<Request> inFlightRequests = ConcurrentHashMap.newKeySet(); |
| 36 | + private volatile boolean closed; |
| 37 | + |
| 38 | + JettyHttpClient( |
| 39 | + HttpTransport transport, |
| 40 | + @Nullable String unixDomainSocketPath, |
| 41 | + long connectTimeoutMillis, |
| 42 | + long requestTimeoutMillis, |
| 43 | + long responseTimeoutMillis, |
| 44 | + @Nullable String proxyHost, |
| 45 | + @Nullable Integer proxyPort, |
| 46 | + @Nullable String proxyUsername, |
| 47 | + @Nullable String proxyPassword, |
| 48 | + HttpRetryPolicy.Factory retryPolicyFactory, |
| 49 | + @Nullable org.eclipse.jetty.client.HttpClient externallyProvidedClient, |
| 50 | + boolean closeClientOnClose) { |
| 51 | + if (transport == HttpTransport.NAMED_PIPE) { |
| 52 | + throw new IllegalArgumentException("Jetty HTTP client supports only TCP and UDS transport"); |
| 53 | + } |
| 54 | + |
| 55 | + this.requestTimeoutMillis = requestTimeoutMillis; |
| 56 | + this.retryPolicyFactory = retryPolicyFactory; |
| 57 | + |
| 58 | + if (externallyProvidedClient != null) { |
| 59 | + this.client = externallyProvidedClient; |
| 60 | + this.closeClientOnClose = closeClientOnClose; |
| 61 | + startClient(this.client); |
| 62 | + return; |
| 63 | + } |
| 64 | + |
| 65 | + org.eclipse.jetty.client.HttpClient client = createClient(transport, unixDomainSocketPath); |
| 66 | + client.setConnectTimeout(connectTimeoutMillis); |
| 67 | + client.setIdleTimeout(responseTimeoutMillis); |
| 68 | + if (proxyHost != null && proxyPort != null) { |
| 69 | + client.getProxyConfiguration().getProxies().add(new HttpProxy(proxyHost, proxyPort)); |
| 70 | + if (proxyUsername != null) { |
| 71 | + URI proxyUri = URI.create("http://" + proxyHost + ":" + proxyPort); |
| 72 | + client |
| 73 | + .getAuthenticationStore() |
| 74 | + .addAuthentication( |
| 75 | + new BasicAuthentication( |
| 76 | + proxyUri, Authentication.ANY_REALM, proxyUsername, proxyPassword)); |
| 77 | + } |
| 78 | + } |
| 79 | + startClient(client); |
| 80 | + this.client = client; |
| 81 | + this.closeClientOnClose = true; |
| 82 | + } |
| 83 | + |
| 84 | + @Override |
| 85 | + public HttpClientResponse execute(HttpClientRequest request) throws IOException { |
| 86 | + if (closed) { |
| 87 | + throw new IOException("http client is closed"); |
| 88 | + } |
| 89 | + |
| 90 | + try (HttpRetryPolicy retryPolicy = retryPolicyFactory.create()) { |
| 91 | + while (true) { |
| 92 | + try { |
| 93 | + HttpClientResponse response = executeOnce(request); |
| 94 | + if (!retryPolicy.shouldRetry(new ResponseAdapter(response))) { |
| 95 | + return response; |
| 96 | + } |
| 97 | + } catch (Exception e) { |
| 98 | + IOException io = e instanceof IOException ? (IOException) e : new IOException(e); |
| 99 | + if (!retryPolicy.shouldRetry(io)) { |
| 100 | + throw io; |
| 101 | + } |
| 102 | + } |
| 103 | + retryPolicy.backoff(); |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + private HttpClientResponse executeOnce(HttpClientRequest request) throws IOException { |
| 109 | + Request jettyRequest = toJettyRequest(request); |
| 110 | + inFlightRequests.add(jettyRequest); |
| 111 | + try { |
| 112 | + ContentResponse response = |
| 113 | + jettyRequest.timeout(requestTimeoutMillis, TimeUnit.MILLISECONDS).send(); |
| 114 | + return toFacadeResponse(response); |
| 115 | + } catch (InterruptedException e) { |
| 116 | + Thread.currentThread().interrupt(); |
| 117 | + throw new IOException("request interrupted", e); |
| 118 | + } catch (ExecutionException e) { |
| 119 | + Throwable cause = e.getCause(); |
| 120 | + throw cause instanceof IOException ? (IOException) cause : new IOException(cause); |
| 121 | + } catch (TimeoutException e) { |
| 122 | + throw new IOException("request timeout", e); |
| 123 | + } finally { |
| 124 | + inFlightRequests.remove(jettyRequest); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + private Request toJettyRequest(HttpClientRequest request) { |
| 129 | + Request jettyRequest = client.newRequest(request.uri()).method(request.method()); |
| 130 | + for (Map.Entry<String, List<String>> header : request.headers().entrySet()) { |
| 131 | + for (String value : header.getValue()) { |
| 132 | + jettyRequest.header(header.getKey(), value); |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + byte[] body = request.body(); |
| 137 | + if (body.length > 0) { |
| 138 | + jettyRequest.content(new BytesContentProvider(body)); |
| 139 | + } |
| 140 | + return jettyRequest; |
| 141 | + } |
| 142 | + |
| 143 | + private static HttpClientResponse toFacadeResponse(ContentResponse response) { |
| 144 | + Map<String, List<String>> headers = new LinkedHashMap<>(); |
| 145 | + for (HttpField header : response.getHeaders()) { |
| 146 | + headers |
| 147 | + .computeIfAbsent(header.getName(), ignored -> new ArrayList<>()) |
| 148 | + .add(header.getValue()); |
| 149 | + } |
| 150 | + return new HttpClientResponse(response.getStatus(), headers, response.getContent()); |
| 151 | + } |
| 152 | + |
| 153 | + @Override |
| 154 | + public void close() { |
| 155 | + closed = true; |
| 156 | + |
| 157 | + for (Request request : inFlightRequests) { |
| 158 | + request.abort(new IOException("http client is closed")); |
| 159 | + } |
| 160 | + inFlightRequests.clear(); |
| 161 | + |
| 162 | + if (closeClientOnClose) { |
| 163 | + try { |
| 164 | + client.stop(); |
| 165 | + } catch (Exception ignored) { |
| 166 | + } |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + private static void startClient(org.eclipse.jetty.client.HttpClient client) { |
| 171 | + if (!client.isStarted()) { |
| 172 | + try { |
| 173 | + client.start(); |
| 174 | + } catch (Exception e) { |
| 175 | + throw new IllegalStateException("Unable to start Jetty HTTP client", e); |
| 176 | + } |
| 177 | + } |
| 178 | + } |
| 179 | + |
| 180 | + private static org.eclipse.jetty.client.HttpClient createClient( |
| 181 | + HttpTransport transport, @Nullable String unixDomainSocketPath) { |
| 182 | + if (transport == HttpTransport.UNIX_DOMAIN_SOCKET) { |
| 183 | + HttpClientTransport udsTransport = |
| 184 | + new HttpClientTransportOverUnixSockets(unixDomainSocketPath); |
| 185 | + return new org.eclipse.jetty.client.HttpClient(udsTransport, null); |
| 186 | + } |
| 187 | + return new org.eclipse.jetty.client.HttpClient(); |
| 188 | + } |
| 189 | + |
| 190 | + private static final class ResponseAdapter implements HttpRetryPolicy.Response { |
| 191 | + private final HttpClientResponse response; |
| 192 | + |
| 193 | + private ResponseAdapter(HttpClientResponse response) { |
| 194 | + this.response = response; |
| 195 | + } |
| 196 | + |
| 197 | + @Override |
| 198 | + public int code() { |
| 199 | + return response.statusCode(); |
| 200 | + } |
| 201 | + |
| 202 | + @Override |
| 203 | + public @Nullable String header(String name) { |
| 204 | + return response.header(name); |
| 205 | + } |
| 206 | + } |
| 207 | +} |
0 commit comments