public static InetAddress getAddress(String host) throws UnknownHostException {
        if (timeToClean()) {
            synchronized (storedAddresses) {
                if (timeToClean()) {
                    cleanOldAddresses();
                }
            }
        }
        Pair<InetAddress, Long> cachedAddress;
        synchronized (storedAddresses) {
            cachedAddress = storedAddresses.get(host);
        }
        if (cachedAddress != null) {
            //host DNS entry was cached
            InetAddress address = cachedAddress.getFirst();
            if (address == null) {
                throw new UnknownHostException("Could not find host " + host + " (cached response)");
            } else {
                return address;
            }
        } else {
            //host DNS entry was not cached
            fetchDnsAddressLock.acquireUninterruptibly();
            try {
                InetAddress addr = InetAddress.getByName(host);
                synchronized (storedAddresses) {
                    storedAddresses.put(host, new Pair<>(addr, System.currentTimeMillis()));
                }
                return addr;
            } catch (UnknownHostException exp) {
                synchronized (storedAddresses) {
                    storedAddresses.put(host, new Pair<InetAddress, Long>(null, System.currentTimeMillis()));
                }
                Log.i("[Dns lookup] " + host + " --> not found");
                throw exp;
            } finally {
                fetchDnsAddressLock.release();
            }

        }
    }
public static InetAddress getAddress ( String host ) throws UnknownHostException { if ( timeToClean ( ) ) { synchronized ( storedAddresses ) { if ( timeToClean ( ) ) { cleanOldAddresses ( ) ; } } } Pair < InetAddress , Long > cachedAddress ; synchronized ( storedAddresses ) { cachedAddress = storedAddresses . get ( host ) ; } if ( cachedAddress != null ) { InetAddress address = cachedAddress . getFirst ( ) ; if ( address == null ) { throw new UnknownHostException ( "Could not find host " + host + " (cached response)" ) ; } else { return address ; } } else { fetchDnsAddressLock . acquireUninterruptibly ( ) ; try { InetAddress addr = InetAddress . getByName ( host ) ; synchronized ( storedAddresses ) { storedAddresses . put ( host , new Pair < > ( addr , System . currentTimeMillis ( ) ) ) ; } return addr ; } catch ( UnknownHostException exp ) { synchronized ( storedAddresses ) { storedAddresses . put ( host , new Pair < InetAddress , Long > ( null , System . currentTimeMillis ( ) ) ) ; } Log . i ( "[Dns lookup] " + host + " --> not found" ) ; throw exp ; } finally { fetchDnsAddressLock . release ( ) ; } } }
only allow 5 simultaneous DNS requests