17
17
package io .grpc .cronet ;
18
18
19
19
import static com .google .common .base .Preconditions .checkArgument ;
20
+ import static com .google .common .base .Preconditions .checkNotNull ;
20
21
import static io .grpc .internal .GrpcUtil .DEFAULT_MAX_MESSAGE_SIZE ;
21
22
22
23
import com .google .common .annotations .VisibleForTesting ;
@@ -73,6 +74,9 @@ public static CronetChannelBuilder forAddress(String name, int port) {
73
74
throw new UnsupportedOperationException ("call forAddress(String, int, CronetEngine) instead" );
74
75
}
75
76
77
+ @ Nullable
78
+ private ScheduledExecutorService scheduledExecutorService ;
79
+
76
80
private final CronetEngine cronetEngine ;
77
81
78
82
private boolean alwaysUsePut = false ;
@@ -161,12 +165,30 @@ public final CronetChannelBuilder setTrafficStatsUid(int uid) {
161
165
return this ;
162
166
}
163
167
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
+
164
185
@ Override
165
186
protected final ClientTransportFactory buildTransportFactory () {
166
187
return new CronetTransportFactory (
167
188
new TaggingStreamFactory (
168
189
cronetEngine , trafficStatsTagSet , trafficStatsTag , trafficStatsUidSet , trafficStatsUid ),
169
190
MoreExecutors .directExecutor (),
191
+ scheduledExecutorService ,
170
192
maxMessageSize ,
171
193
alwaysUsePut ,
172
194
transportTracerFactory .create ());
@@ -180,20 +202,24 @@ protected Attributes getNameResolverParams() {
180
202
181
203
@ VisibleForTesting
182
204
static class CronetTransportFactory implements ClientTransportFactory {
183
- private final ScheduledExecutorService timeoutService =
184
- SharedResourceHolder .get (GrpcUtil .TIMER_SERVICE );
205
+ private final ScheduledExecutorService timeoutService ;
185
206
private final Executor executor ;
186
207
private final int maxMessageSize ;
187
208
private final boolean alwaysUsePut ;
188
209
private final StreamBuilderFactory streamFactory ;
189
210
private final TransportTracer transportTracer ;
211
+ private final boolean usingSharedScheduler ;
190
212
191
213
private CronetTransportFactory (
192
214
StreamBuilderFactory streamFactory ,
193
215
Executor executor ,
216
+ @ Nullable ScheduledExecutorService timeoutService ,
194
217
int maxMessageSize ,
195
218
boolean alwaysUsePut ,
196
219
TransportTracer transportTracer ) {
220
+ usingSharedScheduler = timeoutService == null ;
221
+ this .timeoutService = usingSharedScheduler
222
+ ? SharedResourceHolder .get (GrpcUtil .TIMER_SERVICE ) : timeoutService ;
197
223
this .maxMessageSize = maxMessageSize ;
198
224
this .alwaysUsePut = alwaysUsePut ;
199
225
this .streamFactory = streamFactory ;
@@ -216,7 +242,9 @@ public ScheduledExecutorService getScheduledExecutorService() {
216
242
217
243
@ Override
218
244
public void close () {
219
- SharedResourceHolder .release (GrpcUtil .TIMER_SERVICE , timeoutService );
245
+ if (usingSharedScheduler ) {
246
+ SharedResourceHolder .release (GrpcUtil .TIMER_SERVICE , timeoutService );
247
+ }
220
248
}
221
249
}
222
250
0 commit comments