Handling DoS due to unresponsive remote hosts
During a server start-up procedure, I access a remote host for some initialisation data. Unfortunately, the remote host in question is notorious for being rando开发者_如何学JAVAmly unresponsive to connections (lets say 3% of the time this happens). The result is that the connection hangs indefinitely (possibly forever), and my server doesn't start up in a reasonable amount of time, if at all. It's hung for an hour before.
A connection timeout:
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(connectionTimeout);
for (int i = 0; i <= connectionRetries; i++) {
try {
log.info("Establishing connection to ClassNotes service.");
conn.connect();
break;
} catch (SocketTimeoutException e) {
log.warn("Timed out connecting to ClassNotes service.");
}
}
does not work, since the connection is already established with the server when it hangs. The problem seems to be when it tries to get an InputStream from the server.
For any one who has had to work with disobediant services, should I thread the process of getting the InputStream with a finite number of retries, and just deal with one or two potential threads hanging in the JVM's memory indefinitely? Or, is there a more common way to handle this in Java?
It could also be useful to know that a server starting up without the information from the remote host is not as devastating as the server not starting up at all.
Thanks in advance.
You can setReadTimeout() in addition to the connection timeout.
You could put this logic in a Runnable object, then create a Thread that contacts the service. If the Thread hasn't finished in a specified amount of time, then kill that Thread and try again. Do this until it works or until you've reached the retry limit, and then continue on with startup.
精彩评论