Class CachedDnsResolver

  • All Implemented Interfaces:
    java.io.Closeable, java.lang.AutoCloseable, org.apache.http.conn.DnsResolver

    public class CachedDnsResolver
    extends java.lang.Object
    implements org.apache.http.conn.DnsResolver, java.io.Closeable
    CachedDnsResolver caches the dns entries for hosts, which matches the specified host pattern. In the meantime, there is a asynchronous task running to refresh the cached entries periodically. The reason to have this customized DnsResolver: 1. If the DNS lookup is slow, AbstractNIOConnPool.lease(Object, Object) will be blocked by the slow DNS lookup, which will cause a lot of requests being blocked since the slow DNS lookup will grab the per-connection-pool lock until it is done; 2. Here we choose to use a asynchronous task to refresh the cached entries is that the synchronous refresh with TTL will still cause latency spike because of #1;
    • Constructor Summary

      Constructors 
      Constructor Description
      CachedDnsResolver​(java.lang.String cachedHostPattern, long refreshIntervalInMs, DnsLookupStats stats)  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void close()  
      protected java.util.Optional<java.net.InetAddress[]> getEntryFromDnsCache​(java.lang.String host)  
      java.net.InetAddress[] resolve​(java.lang.String host)  
      protected java.net.InetAddress[] systemGetAllByName​(java.lang.String host)  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • CachedDnsResolver

        public CachedDnsResolver​(java.lang.String cachedHostPattern,
                                 long refreshIntervalInMs,
                                 DnsLookupStats stats)
    • Method Detail

      • resolve

        public java.net.InetAddress[] resolve​(java.lang.String host)
                                       throws java.net.UnknownHostException
        Specified by:
        resolve in interface org.apache.http.conn.DnsResolver
        Throws:
        java.net.UnknownHostException
      • getEntryFromDnsCache

        protected java.util.Optional<java.net.InetAddress[]> getEntryFromDnsCache​(java.lang.String host)
      • systemGetAllByName

        protected java.net.InetAddress[] systemGetAllByName​(java.lang.String host)
                                                     throws java.net.UnknownHostException
        Throws:
        java.net.UnknownHostException
      • close

        public void close()
                   throws java.io.IOException
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
        Throws:
        java.io.IOException