-
Notifications
You must be signed in to change notification settings - Fork 34
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement timeout for dns hostname lookup #179
Conversation
ExecutorService lookupService = Executors.newSingleThreadExecutor(new DaemonThreadFactory()); | ||
Future<String> future = lookupService.submit(new Callable<String>() { | ||
@Override | ||
public String call() throws UnknownHostException { | ||
return InetAddress.getLocalHost().getHostName(); | ||
} | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of adding a timeout to the lookupHostname
method, maybe we should instead wrap the "startup" cache in a daemon thread?
public static void initializeCache() {
Thread hostnameLookup = new Thread("Hostname Lookup") {
@Override
public void run() {
getHostnameValue();
}
};
hostnameLookup.setDaemon(true);
hostnameLookup.start();
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As discussed, I've moved the cache init to a daemon thread to prevent blocking on initialization, but retained the timeout on the hostname lookup as getHostnameValue
may also be called from elsewhere before the cache is initialized.
Thread resolverThread = new Thread(future, "Hostname Resolver"); | ||
resolverThread.setDaemon(true); | ||
resolverThread.start(); | ||
future.run(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will need to be threaded. future.run()
will wait for the hostname to be resolved before returning, which means that future.get(HOSTNAME_LOOKUP_TIMEOUT...
will return immediately (and never timeout).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Goal
This PR aims to fix a freeze reported in #176.
DNS hostname lookups with
InetAddress.getLocalHost()
can be very slow to run in certain situations and this can cause apps to hang on initialisation when determiningdevice.hostname
Design
Added a 10s timeout when performing the DNS hostname lookup. If it cannot be resolved in that time then the metadata is simply not set. The timeout period here is fairly arbitrary.
Testing
Couldn't reproduce the slow DNS lookup, but ran unit tests and manually tested with the example app to confirm that DNS hostname still resolves successfully.