Skip to content

Commit a7fc9ef

Browse files
dansanduleacRobert Fink
authored and
Robert Fink
committed
Shared dispatcher and connection pool (#706)
1 parent 1d8a61e commit a7fc9ef

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

okhttp-clients/src/main/java/com/palantir/remoting3/okhttp/OkHttpClients.java

+14-10
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,18 @@ public final class OkHttpClients {
5454
*/
5555
private static final ExecutorService executionExecutor = Tracers.wrap(new Dispatcher().executorService());
5656

57+
/** Shared dispatcher with static executor service. */
58+
private static final Dispatcher dispatcher;
59+
60+
static {
61+
dispatcher = new Dispatcher(executionExecutor);
62+
dispatcher.setMaxRequests(256);
63+
dispatcher.setMaxRequestsPerHost(256);
64+
}
65+
66+
/** Shared connection pool. */
67+
private static final ConnectionPool connectionPool = new ConnectionPool(100, 10, TimeUnit.MINUTES);
68+
5769
/**
5870
* The {@link ScheduledExecutorService} used for scheduling call retries. This thread pool is distinct from OkHttp's
5971
* internal thread pool and from the thread pool used by {@link #executionExecutor}.
@@ -151,10 +163,9 @@ private static RemotingOkHttpClient createInternal(
151163
client.connectionSpecs(createConnectionSpecs(config.enableGcmCipherSuites()));
152164

153165
// increase default connection pool from 5 @ 5 minutes to 100 @ 10 minutes
154-
client.connectionPool(new ConnectionPool(100, 10, TimeUnit.MINUTES));
166+
client.connectionPool(connectionPool);
155167

156-
// dispatcher with static executor service
157-
client.dispatcher(createDispatcher());
168+
client.dispatcher(dispatcher);
158169

159170
return new RemotingOkHttpClient(
160171
client.build(),
@@ -212,11 +223,4 @@ private static ImmutableList<ConnectionSpec> createConnectionSpecs(boolean enabl
212223
ConnectionSpec.CLEARTEXT);
213224
}
214225

215-
private static Dispatcher createDispatcher() {
216-
Dispatcher dispatcher = new Dispatcher(executionExecutor);
217-
dispatcher.setMaxRequests(256);
218-
dispatcher.setMaxRequestsPerHost(256);
219-
return dispatcher;
220-
}
221-
222226
}

0 commit comments

Comments
 (0)