@@ -134,21 +134,17 @@ public class DnsNameResolver extends NameResolver {
134
134
private final String host ;
135
135
private final int port ;
136
136
137
- /** Executor that will be used if an Executor is not provide via {@link NameResolver.Args}. */
138
- private final Resource <Executor > executorResource ;
137
+ private final ObjectPool <Executor > executorPool ;
139
138
private final long cacheTtlNanos ;
140
139
private final SynchronizationContext syncContext ;
140
+ private final ServiceConfigParser serviceConfigParser ;
141
141
142
142
// Following fields must be accessed from syncContext
143
143
private final Stopwatch stopwatch ;
144
144
protected boolean resolved ;
145
145
private boolean shutdown ;
146
146
private Executor executor ;
147
147
148
- /** True if using an executor resource that should be released after use. */
149
- private final boolean usingExecutorResource ;
150
- private final ServiceConfigParser serviceConfigParser ;
151
-
152
148
private boolean resolving ;
153
149
154
150
// The field must be accessed from syncContext, although the methods on an Listener2 can be called
@@ -165,7 +161,7 @@ protected DnsNameResolver(
165
161
checkNotNull (args , "args" );
166
162
// TODO: if a DNS server is provided as nsAuthority, use it.
167
163
// https://www.captechconsulting.com/blogs/accessing-the-dusty-corners-of-dns-with-java
168
- this . executorResource = executorResource ;
164
+
169
165
// Must prepend a "//" to the name when constructing a URI, otherwise it will be treated as an
170
166
// opaque URI, thus the authority and host of the resulted URI would be null.
171
167
URI nameUri = URI .create ("//" + checkNotNull (name , "name" ));
@@ -179,11 +175,15 @@ protected DnsNameResolver(
179
175
port = nameUri .getPort ();
180
176
}
181
177
this .proxyDetector = checkNotNull (args .getProxyDetector (), "proxyDetector" );
178
+ Executor offloadExecutor = args .getOffloadExecutor ();
179
+ if (offloadExecutor != null ) {
180
+ this .executorPool = new FixedObjectPool <>(offloadExecutor );
181
+ } else {
182
+ this .executorPool = SharedResourcePool .forResource (executorResource );
183
+ }
182
184
this .cacheTtlNanos = getNetworkAddressCacheTtlNanos (isAndroid );
183
185
this .stopwatch = checkNotNull (stopwatch , "stopwatch" );
184
186
this .syncContext = checkNotNull (args .getSynchronizationContext (), "syncContext" );
185
- this .executor = args .getOffloadExecutor ();
186
- this .usingExecutorResource = executor == null ;
187
187
this .serviceConfigParser = checkNotNull (args .getServiceConfigParser (), "serviceConfigParser" );
188
188
}
189
189
@@ -200,9 +200,7 @@ protected String getHost() {
200
200
@ Override
201
201
public void start (Listener2 listener ) {
202
202
Preconditions .checkState (this .listener == null , "already started" );
203
- if (usingExecutorResource ) {
204
- executor = SharedResourceHolder .get (executorResource );
205
- }
203
+ executor = executorPool .getObject ();
206
204
this .listener = checkNotNull (listener , "listener" );
207
205
resolve ();
208
206
}
@@ -413,8 +411,8 @@ public void shutdown() {
413
411
return ;
414
412
}
415
413
shutdown = true ;
416
- if (executor != null && usingExecutorResource ) {
417
- executor = SharedResourceHolder . release ( executorResource , executor );
414
+ if (executor != null ) {
415
+ executor = executorPool . returnObject ( executor );
418
416
}
419
417
}
420
418
0 commit comments