Skip to content

Commit 37c5a7e

Browse files
committed
cronet: allow application to provide all threads
1 parent 900b396 commit 37c5a7e

File tree

1 file changed

+31
-4
lines changed

1 file changed

+31
-4
lines changed

cronet/src/main/java/io/grpc/cronet/CronetChannelBuilder.java

+31-4
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package io.grpc.cronet;
1818

1919
import static com.google.common.base.Preconditions.checkArgument;
20+
import static com.google.common.base.Preconditions.checkNotNull;
2021
import static io.grpc.internal.GrpcUtil.DEFAULT_MAX_MESSAGE_SIZE;
2122

2223
import com.google.common.annotations.VisibleForTesting;
@@ -73,6 +74,9 @@ public static CronetChannelBuilder forAddress(String name, int port) {
7374
throw new UnsupportedOperationException("call forAddress(String, int, CronetEngine) instead");
7475
}
7576

77+
@Nullable
78+
private ScheduledExecutorService scheduledExecutorService;
79+
7680
private final CronetEngine cronetEngine;
7781

7882
private boolean alwaysUsePut = false;
@@ -161,12 +165,29 @@ public final CronetChannelBuilder setTrafficStatsUid(int uid) {
161165
return this;
162166
}
163167

168+
/**
169+
* Provides a custom scheduled executor service.
170+
*
171+
* <p>It's an optional parameter. If the user has not provided a scheduled executor service when
172+
* the channel is built, the builder will use a static cached thread pool.
173+
*
174+
* @return this
175+
*
176+
* @since 1.12.0
177+
*/
178+
public final CronetChannelBuilder scheduledExecutorService(
179+
ScheduledExecutorService scheduledExecutorService) {
180+
this.scheduledExecutorService =
181+
checkNotNull(scheduledExecutorService, "scheduledExecutorService");
182+
return this;
183+
}
184+
164185
@Override
165186
protected final ClientTransportFactory buildTransportFactory() {
166187
return new CronetTransportFactory(
167188
new TaggingStreamFactory(
168189
cronetEngine, trafficStatsTagSet, trafficStatsTag, trafficStatsUidSet, trafficStatsUid),
169-
MoreExecutors.directExecutor(),
190+
MoreExecutors.directExecutor(), scheduledExecutorService,
170191
maxMessageSize,
171192
alwaysUsePut,
172193
transportTracerFactory.create());
@@ -180,20 +201,24 @@ protected Attributes getNameResolverParams() {
180201

181202
@VisibleForTesting
182203
static class CronetTransportFactory implements ClientTransportFactory {
183-
private final ScheduledExecutorService timeoutService =
184-
SharedResourceHolder.get(GrpcUtil.TIMER_SERVICE);
204+
private final ScheduledExecutorService timeoutService;
185205
private final Executor executor;
186206
private final int maxMessageSize;
187207
private final boolean alwaysUsePut;
188208
private final StreamBuilderFactory streamFactory;
189209
private final TransportTracer transportTracer;
210+
private final boolean usingSharedScheduler;
190211

191212
private CronetTransportFactory(
192213
StreamBuilderFactory streamFactory,
193214
Executor executor,
215+
@Nullable ScheduledExecutorService timeoutService,
194216
int maxMessageSize,
195217
boolean alwaysUsePut,
196218
TransportTracer transportTracer) {
219+
usingSharedScheduler = timeoutService == null;
220+
this.timeoutService = usingSharedScheduler
221+
? SharedResourceHolder.get(GrpcUtil.TIMER_SERVICE) : timeoutService;
197222
this.maxMessageSize = maxMessageSize;
198223
this.alwaysUsePut = alwaysUsePut;
199224
this.streamFactory = streamFactory;
@@ -216,7 +241,9 @@ public ScheduledExecutorService getScheduledExecutorService() {
216241

217242
@Override
218243
public void close() {
219-
SharedResourceHolder.release(GrpcUtil.TIMER_SERVICE, timeoutService);
244+
if (usingSharedScheduler) {
245+
SharedResourceHolder.release(GrpcUtil.TIMER_SERVICE, timeoutService);
246+
}
220247
}
221248
}
222249

0 commit comments

Comments
 (0)