Skip to content

Latest commit

 

History

History
82 lines (67 loc) · 7.55 KB

developer-guide.md

File metadata and controls

82 lines (67 loc) · 7.55 KB

🎓 Developer Guide

Github Workflow Build Status Github Workflow Build Status Coverage Status Maintainability Java support License Maven Central GitHub release Javadocs GitHub Stars GitHub Forks GitHub issues user repos GitHub Contributors GitHub repo size gitpod: Ready to Code GitHub release download - dcm.tar.gz)

How to set the DNS Cache of JVM safely

The DNS Cache of the JVM is maintained in the private field of the InetAddress class and is set by reflection. For specific implementation, see

Pay attention to the thread safety of setting the DNS Cache of JVM The DNS Cache of the JVM is obviously shared globally, so the setting is guaranteed to be thread-safe and there is no concurrency problem.

Taking JDK 8 as an example, by looking at the implementation of the InetAddress class, it can be determined that thread safety is ensured by the synchronized block with the addressCache field as the lock.

The key code is as follows:

/*
 * Cache the given hostname and addresses.
 */
private static void cacheAddresses(String hostname,
                                   InetAddress[] addresses,
                                   boolean success) {
    hostname = hostname.toLowerCase();
    synchronized (addressCache) {
        cacheInitIfNeeded();
        if (success) {
            addressCache.put(hostname, addresses);
        } else {
            negativeCache.put(hostname, addresses);
        }
    }
}

In the InetAddressCacheUtilForOld class, the read and write to the DNS Cache also consistently adds a synchronized block with addressCache field as the lock to ensure thread safety.

Need test different JDK versions

The implementation of this library uses the non-public API of the JDK, and different JDK implementations will be different, that is, compatible logic is required, and different versions of JDK are tested to ensure functionality.

The LTS JDK versions(8/11/17) and recent versions are tested , other JDK versions should work properly.

📚 Related Resources