Skip to content
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

Merged
merged 5 commits into from
Jul 7, 2022

Conversation

yousif-bugsnag
Copy link
Contributor

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 determining device.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.

Comment on lines 54 to 60
ExecutorService lookupService = Executors.newSingleThreadExecutor(new DaemonThreadFactory());
Future<String> future = lookupService.submit(new Callable<String>() {
@Override
public String call() throws UnknownHostException {
return InetAddress.getLocalHost().getHostName();
}
});
Copy link
Contributor

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();
    }

Copy link
Contributor Author

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.

Comment on lines 57 to 56
Thread resolverThread = new Thread(future, "Hostname Resolver");
resolverThread.setDaemon(true);
resolverThread.start();
future.run();
Copy link
Contributor

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).

Copy link
Contributor

@lemnik lemnik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@yousif-bugsnag yousif-bugsnag merged commit 83e9232 into next Jul 7, 2022
@yousif-bugsnag yousif-bugsnag deleted the PLAT-8493/dns-lookup-timeout branch July 7, 2022 14:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants