@@ -98,6 +98,8 @@ final class DnsNameResolver extends NameResolver {
9898 * <p>Default value is -1 (cache forever) if security manager is installed. If security manager is
9999 * not installed, the ttl value is {@code null} which falls back to {@link
100100 * #DEFAULT_NETWORK_CACHE_TTL_SECONDS gRPC default value}.
101+ *
102+ * <p>For android, gRPC doesn't attempt to cache; this property value will be ignored.
101103 */
102104 @ VisibleForTesting
103105 static final String NETWORKADDRESS_CACHE_TTL_PROPERTY = "networkaddress.cache.ttl" ;
@@ -143,7 +145,7 @@ final class DnsNameResolver extends NameResolver {
143145
144146 DnsNameResolver (@ Nullable String nsAuthority , String name , Attributes params ,
145147 Resource <ExecutorService > executorResource , ProxyDetector proxyDetector ,
146- Stopwatch stopwatch ) {
148+ Stopwatch stopwatch , boolean isAndroid ) {
147149 // TODO: if a DNS server is provided as nsAuthority, use it.
148150 // https://www.captechconsulting.com/blogs/accessing-the-dusty-corners-of-dns-with-java
149151 this .executorResource = executorResource ;
@@ -166,7 +168,7 @@ final class DnsNameResolver extends NameResolver {
166168 port = nameUri .getPort ();
167169 }
168170 this .proxyDetector = proxyDetector ;
169- this .resolveRunnable = new Resolve (this , stopwatch );
171+ this .resolveRunnable = new Resolve (this , stopwatch , getNetworkAddressCacheTtlNanos ( isAndroid ) );
170172 }
171173
172174 @ Override
@@ -196,10 +198,10 @@ static final class Resolve implements Runnable {
196198 private final long cacheTtlNanos ;
197199 private ResolutionResults cachedResolutionResults = null ;
198200
199- Resolve (DnsNameResolver resolver , Stopwatch stopwatch ) {
201+ Resolve (DnsNameResolver resolver , Stopwatch stopwatch , long cacheTtlNanos ) {
200202 this .resolver = resolver ;
201203 this .stopwatch = Preconditions .checkNotNull (stopwatch , "stopwatch" );
202- this .cacheTtlNanos = getNetworkAddressCacheTtlNanos () ;
204+ this .cacheTtlNanos = cacheTtlNanos ;
203205 }
204206
205207 @ Override
@@ -230,23 +232,6 @@ private boolean cacheRefreshRequired() {
230232 || (cacheTtlNanos > 0 && stopwatch .elapsed (TimeUnit .NANOSECONDS ) > cacheTtlNanos );
231233 }
232234
233- /** Returns value of network address cache ttl property. */
234- private static long getNetworkAddressCacheTtlNanos () {
235- String cacheTtlPropertyValue = System .getProperty (NETWORKADDRESS_CACHE_TTL_PROPERTY );
236- long cacheTtl = DEFAULT_NETWORK_CACHE_TTL_SECONDS ;
237- if (cacheTtlPropertyValue != null ) {
238- try {
239- cacheTtl = Long .parseLong (cacheTtlPropertyValue );
240- } catch (NumberFormatException e ) {
241- logger .log (
242- Level .WARNING ,
243- "Property({0}) valid is not valid number format({1}), fall back to default({2})" ,
244- new Object [] {NETWORKADDRESS_CACHE_TTL_PROPERTY , cacheTtlPropertyValue , cacheTtl });
245- }
246- }
247- return cacheTtl > 0 ? TimeUnit .SECONDS .toNanos (cacheTtl ) : cacheTtl ;
248- }
249-
250235 @ VisibleForTesting
251236 void resolveInternal (Listener savedListener ) {
252237 InetSocketAddress destination =
@@ -483,6 +468,31 @@ private static final List<String> getHostnamesFromChoice(
483468 ServiceConfigUtil .getList (serviceConfigChoice , SERVICE_CONFIG_CHOICE_CLIENT_HOSTNAME_KEY ));
484469 }
485470
471+ /**
472+ * Returns value of network address cache ttl property if not Android environment. For android,
473+ * DnsNameResolver does not cache the dns lookup result.
474+ */
475+ private static long getNetworkAddressCacheTtlNanos (boolean isAndroid ) {
476+ if (isAndroid ) {
477+ // on Android, ignore dns cache.
478+ return 0 ;
479+ }
480+
481+ String cacheTtlPropertyValue = System .getProperty (NETWORKADDRESS_CACHE_TTL_PROPERTY );
482+ long cacheTtl = DEFAULT_NETWORK_CACHE_TTL_SECONDS ;
483+ if (cacheTtlPropertyValue != null ) {
484+ try {
485+ cacheTtl = Long .parseLong (cacheTtlPropertyValue );
486+ } catch (NumberFormatException e ) {
487+ logger .log (
488+ Level .WARNING ,
489+ "Property({0}) valid is not valid number format({1}), fall back to default({2})" ,
490+ new Object [] {NETWORKADDRESS_CACHE_TTL_PROPERTY , cacheTtlPropertyValue , cacheTtl });
491+ }
492+ }
493+ return cacheTtl > 0 ? TimeUnit .SECONDS .toNanos (cacheTtl ) : cacheTtl ;
494+ }
495+
486496 /**
487497 * Determines if a given Service Config choice applies, and if so, returns it.
488498 *
0 commit comments