Search in sources :

Example 11 with UserAgent

use of com.palantir.conjure.java.api.config.service.UserAgent in project conjure-java-runtime by palantir.

the class OkHttpClients method createInternal.

private static RemotingOkHttpClient createInternal(OkHttpClient.Builder client, ClientConfiguration config, Class<?> serviceClass, boolean randomizeUrlOrder, boolean reshuffle, Supplier<BackoffStrategy> backoffStrategyFunction) {
    boolean enableClientQoS = shouldEnableQos(config.clientQoS());
    ConcurrencyLimiters concurrencyLimiters = new ConcurrencyLimiters(limitReviver.get(), config.taggedMetricRegistry(), serviceClass, enableClientQoS);
    client.addInterceptor(CatchThrowableInterceptor.INSTANCE);
    client.addInterceptor(SpanTerminatingInterceptor.INSTANCE);
    // Order is important, this interceptor must be applied prior to ConcurrencyLimitingInterceptor
    // in order to prevent concurrency limiters from leaking.
    client.addInterceptor(ResponseCapturingInterceptor.INSTANCE);
    // Routing
    if (config.nodeSelectionStrategy().equals(NodeSelectionStrategy.ROUND_ROBIN)) {
        checkArgument(!config.failedUrlCooldown().isZero(), "If nodeSelectionStrategy is ROUND_ROBIN then failedUrlCooldown must be positive");
    }
    UrlSelectorImpl urlSelector = UrlSelectorImpl.createWithFailedUrlCooldown(randomizeUrlOrder ? UrlSelectorImpl.shuffle(config.uris()) : config.uris(), reshuffle, config.failedUrlCooldown(), Clock.systemUTC());
    if (config.meshProxy().isPresent()) {
        // TODO(rfink): Should this go into the call itself?
        client.addInterceptor(new MeshProxyInterceptor(config.meshProxy().get()));
    }
    // We implement our own redirect logic.
    client.followRedirects(false);
    // SSL
    SSLSocketFactory sslSocketFactory = MetricRegistries.instrument(config.taggedMetricRegistry(), new KeepAliveSslSocketFactory(config.sslSocketFactory()), serviceClass.getSimpleName());
    client.sslSocketFactory(sslSocketFactory, config.trustManager());
    if (config.fallbackToCommonNameVerification()) {
        client.hostnameVerifier(Okhttp39HostnameVerifier.INSTANCE);
    }
    // Intercept calls to augment request meta data
    if (enableClientQoS) {
        client.addInterceptor(new ConcurrencyLimitingInterceptor());
    }
    ClientMetrics clientMetrics = ClientMetrics.of(config.taggedMetricRegistry());
    client.addInterceptor(DeprecationWarningInterceptor.create(clientMetrics, serviceClass));
    client.addInterceptor(InstrumentedInterceptor.create(clientMetrics, config.hostEventsSink().orElse(NoOpHostEventsSink.INSTANCE), serviceClass));
    client.addInterceptor(OkhttpTraceInterceptor.INSTANCE);
    UserAgent agent = config.userAgent().orElseThrow(() -> new SafeIllegalArgumentException("UserAgent is required"));
    client.addInterceptor(UserAgentInterceptor.of(augmentUserAgent(agent, serviceClass)));
    // timeouts
    // Note that Feign overrides OkHttp timeouts with the timeouts given in FeignBuilder#Options if given, or
    // with its own default otherwise.
    client.connectTimeout(config.connectTimeout());
    client.readTimeout(config.readTimeout());
    client.writeTimeout(config.writeTimeout());
    // proxy
    client.proxySelector(config.proxy());
    if (config.proxyCredentials().isPresent()) {
        BasicCredentials basicCreds = config.proxyCredentials().get();
        final String credentials = Credentials.basic(basicCreds.username(), basicCreds.password());
        client.proxyAuthenticator((_route, response) -> response.request().newBuilder().header(HttpHeaders.PROXY_AUTHORIZATION, credentials).build());
    }
    client.connectionSpecs(createConnectionSpecs());
    if (!config.enableHttp2().orElse(DEFAULT_ENABLE_HTTP2)) {
        client.protocols(ImmutableList.of(Protocol.HTTP_1_1));
    }
    // increase default connection pool from 5 @ 5 minutes to 100 @ 10 minutes
    client.connectionPool(connectionPool);
    client.dispatcher(dispatcher);
    // global metrics (addMetrics is idempotent, so this works even when multiple clients are created)
    config.taggedMetricRegistry().addMetrics("from", DispatcherMetricSet.class.getSimpleName(), dispatcherMetricSet);
    return new RemotingOkHttpClient(client.build(), backoffStrategyFunction, config.nodeSelectionStrategy(), urlSelector, schedulingExecutor.get(), executionExecutor, concurrencyLimiters, config.serverQoS(), config.retryOnTimeout(), config.retryOnSocketException());
}
Also used : SafeIllegalArgumentException(com.palantir.logsafe.exceptions.SafeIllegalArgumentException) UserAgent(com.palantir.conjure.java.api.config.service.UserAgent) SSLSocketFactory(javax.net.ssl.SSLSocketFactory) BasicCredentials(com.palantir.conjure.java.api.config.service.BasicCredentials)

Aggregations

UserAgent (com.palantir.conjure.java.api.config.service.UserAgent)11 DialogueClients (com.palantir.dialogue.clients.DialogueClients)4 RemotingClientConfigs (com.palantir.atlasdb.config.RemotingClientConfigs)3 Map (java.util.Map)3 Optional (java.util.Optional)3 Iterables (com.google.common.collect.Iterables)2 AtlasDbConfig (com.palantir.atlasdb.config.AtlasDbConfig)2 AtlasDbRuntimeConfig (com.palantir.atlasdb.config.AtlasDbRuntimeConfig)2 AuxiliaryRemotingParameters (com.palantir.atlasdb.config.AuxiliaryRemotingParameters)2 ImmutableServerListConfig (com.palantir.atlasdb.config.ImmutableServerListConfig)2 LeaderConfig (com.palantir.atlasdb.config.LeaderConfig)2 LockDiagnosticComponents (com.palantir.atlasdb.debug.LockDiagnosticComponents)2 AtlasDbHttpClients (com.palantir.atlasdb.http.AtlasDbHttpClients)2 Schema (com.palantir.atlasdb.table.description.Schema)2 AtlasDbMetrics (com.palantir.atlasdb.util.AtlasDbMetrics)2 MetricsManager (com.palantir.atlasdb.util.MetricsManager)2 ReloadingFactory (com.palantir.dialogue.clients.DialogueClients.ReloadingFactory)2 PingableLeader (com.palantir.leader.PingableLeader)2 LeadershipCoordinator (com.palantir.leader.proxy.LeadershipCoordinator)2 SafeIllegalArgumentException (com.palantir.logsafe.exceptions.SafeIllegalArgumentException)2