Skip to content

Commit

Permalink
Update to OkHttp 4 and use daemon threads in dispatcher. (#3946)
Browse files Browse the repository at this point in the history
  • Loading branch information
anuraaga authored Dec 3, 2021
1 parent 737a5e2 commit 74fb36b
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 7 deletions.
6 changes: 1 addition & 5 deletions dependencyManagement/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ val DEPENDENCY_BOMS = listOf(
"com.google.guava:guava-bom:31.0.1-jre",
"com.google.protobuf:protobuf-bom:3.18.1",
"com.linecorp.armeria:armeria-bom:1.13.3",
"com.squareup.okhttp3:okhttp-bom:4.9.3",
"io.grpc:grpc-bom:1.41.0",
"io.zipkin.brave:brave-bom:5.13.3",
"io.zipkin.reporter2:zipkin-reporter-bom:2.16.3",
Expand Down Expand Up @@ -73,11 +74,6 @@ val DEPENDENCIES = listOf(
"com.google.code.findbugs:jsr305:3.0.2",
"com.google.guava:guava-beta-checker:1.0",
"com.lmax:disruptor:3.4.4",
// using old version of okhttp to avoid pulling in kotlin stdlib
// not using (old) okhttp bom because that is pulling in old guava version
// and overriding the guava bom
"com.squareup.okhttp3:okhttp:3.14.9",
"com.squareup.okhttp3:okhttp-tls:3.14.9",
"com.sun.net.httpserver:http:20070405",
"com.tngtech.archunit:archunit-junit5:0.21.0",
"com.uber.nullaway:nullaway:0.9.2",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import io.opentelemetry.exporter.otlp.internal.Marshaler;
import io.opentelemetry.exporter.otlp.internal.RetryPolicy;
import io.opentelemetry.exporter.otlp.internal.TlsUtil;
import io.opentelemetry.exporter.otlp.internal.okhttp.OkHttpUtil;
import io.opentelemetry.exporter.otlp.internal.okhttp.RetryInterceptor;
import java.net.URI;
import java.net.URISyntaxException;
Expand Down Expand Up @@ -121,7 +122,8 @@ public GrpcExporterBuilder<T> setMeterProvider(MeterProvider meterProvider) {

@Override
public GrpcExporter<T> build() {
OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder();
OkHttpClient.Builder clientBuilder =
new OkHttpClient.Builder().dispatcher(OkHttpUtil.newDispatcher());

Headers.Builder headers = this.headers != null ? this.headers : new Headers.Builder();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,9 @@ public OkHttpExporterBuilder<T> setMeterProvider(MeterProvider meterProvider) {

public OkHttpExporter<T> build() {
OkHttpClient.Builder clientBuilder =
new OkHttpClient.Builder().callTimeout(Duration.ofNanos(timeoutNanos));
new OkHttpClient.Builder()
.dispatcher(OkHttpUtil.newDispatcher())
.callTimeout(Duration.ofNanos(timeoutNanos));

if (trustedCertificatesPem != null) {
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.exporter.otlp.internal.okhttp;

import io.opentelemetry.sdk.internal.DaemonThreadFactory;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import okhttp3.Dispatcher;

/** Utilities for OkHttp. */
public final class OkHttpUtil {

/** Returns a {@link Dispatcher} using daemon threads, otherwise matching the OkHttp default. */
public static Dispatcher newDispatcher() {
return new Dispatcher(
new ThreadPoolExecutor(
0,
Integer.MAX_VALUE,
60,
TimeUnit.SECONDS,
new SynchronousQueue<>(),
new DaemonThreadFactory("okhttp-dispatch")));
}

private OkHttpUtil() {}
}

0 comments on commit 74fb36b

Please sign in to comment.