Skip to content

Commit

Permalink
Add HTTP connection timeouts
Browse files Browse the repository at this point in the history
  • Loading branch information
J4bbi committed Dec 10, 2020
1 parent 76a8d77 commit 66ad69c
Showing 1 changed file with 18 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -339,18 +339,27 @@ private static void processUrl(Context c, String urlStr) throws IOException, SQL
URL url = new URL(urlStr);
conn = url.openConnection();

/*
Set live connection timeout to a low value.
If the responses are slow we are fine to wait until a better time.
*/
conn.setConnectTimeout(1000);
conn.setReadTimeout(1000);

// Get the response
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
while (rd.readLine() != null) ;

rd.close();
if (((HttpURLConnection) conn).getResponseCode() != 200) {
log.error("IRUS server responded with code " + ((HttpURLConnection) conn).getResponseCode() + " : " + urlStr);
ExportUsageEventListener.logfailed(c, urlStr);
} else if (log.isDebugEnabled()) {
log.debug("Successfully posted " + urlStr + " on " + new Date());
}
} catch (Exception e) {
log.error("Failed to send url to tracker URL: " + urlStr);
}
catch (Exception e) {
log.error("Failed to send url to tracker URL: " + urlStr + " - " + e.getMessage());
ExportUsageEventListener.logfailed(c, urlStr);
}
}
Expand All @@ -361,6 +370,13 @@ private static void tryReprocessFailed(Context context, OpenURLTracker tracker)
try {
URL url = new URL(tracker.getUrl());
conn = url.openConnection();

/*
Set retry connection timeout to a higher value.
*/
conn.setConnectTimeout(5000);
conn.setReadTimeout(5000);

BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
while (rd.readLine() != null) ;
rd.close();
Expand Down

0 comments on commit 66ad69c

Please sign in to comment.