aboutsummaryrefslogtreecommitdiffstats
path: root/configserver-client/src/main/java/ai/vespa/hosted/client/HttpConfigServerClient.java
blob: e00489f0c642785ca74c64567da44a53ba893511 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// Copyright Verizon Media. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package ai.vespa.hosted.client;

import ai.vespa.util.http.hc5.VespaHttpClientBuilder;
import com.yahoo.vespa.athenz.api.AthenzIdentity;
import com.yahoo.vespa.athenz.tls.AthenzIdentityVerifier;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager;
import org.apache.hc.client5.http.protocol.HttpClientContext;
import org.apache.hc.core5.http.ClassicHttpRequest;
import org.apache.hc.core5.http.ClassicHttpResponse;
import org.apache.hc.core5.http.io.SocketConfig;
import org.apache.hc.core5.util.TimeValue;
import org.apache.hc.core5.util.Timeout;

import java.io.IOException;
import java.util.Collection;
import java.util.Set;

/**
 * @author jonmv
 */
public class HttpConfigServerClient extends AbstractConfigServerClient {

    private final CloseableHttpClient client;

    public HttpConfigServerClient(Collection<AthenzIdentity> serverIdentities, String userAgent) {
        if (serverIdentities.isEmpty())
            throw new IllegalArgumentException("At least one trusted server identity must be provided");

        this.client = createClient(serverIdentities, userAgent);
    }

    @Override
    public void close() throws IOException {
        client.close();
    }

    @Override
    protected ClassicHttpResponse execute(ClassicHttpRequest request, HttpClientContext context) throws IOException {
        return client.execute(request, context);
    }

    private static CloseableHttpClient createClient(Collection<AthenzIdentity> serverIdentities, String userAgent) {
        return VespaHttpClientBuilder.create(socketFactories -> {
                                                 var manager = new PoolingHttpClientConnectionManager(socketFactories);
                                                 manager.setMaxTotal(256);
                                                 manager.setDefaultMaxPerRoute(8);
                                                 manager.setDefaultSocketConfig(SocketConfig.custom().setSoTimeout(Timeout.ofSeconds(5)).build());
                                                 manager.setValidateAfterInactivity(TimeValue.ofSeconds(10));
                                                 return manager;
                                             },
                                             new AthenzIdentityVerifier(Set.copyOf(serverIdentities)),
                                             false)
                                     .disableAutomaticRetries()
                                     .setUserAgent(userAgent)
                                     .build();
    }

}