use of com.palantir.conjure.java.api.config.service.BasicCredentials 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());
}
Aggregations