Skip to content

Commit

Permalink
Merge pull request #359 from jglick/SocketTimeoutException-JENKINS-45142
Browse files Browse the repository at this point in the history
[JENKINS-45142] Retry connections after getting SocketTimeoutException
  • Loading branch information
kohsuke committed Jun 29, 2017
2 parents e9b59c6 + cb76203 commit 0f21eba
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/main/java/org/kohsuke/github/Requester.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.SocketTimeoutException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
Expand All @@ -57,6 +58,7 @@
import org.apache.commons.lang.StringUtils;

import static java.util.Arrays.asList;
import java.util.logging.Level;
import static java.util.logging.Level.*;
import static org.kohsuke.github.GitHub.MAPPER;

Expand Down Expand Up @@ -579,6 +581,10 @@ private void setRequestMethod(HttpURLConnection uc) throws IOException {
}

private <T> T parse(Class<T> type, T instance) throws IOException {
return parse(type, instance, 2);
}

private <T> T parse(Class<T> type, T instance, int timeouts) throws IOException {
InputStreamReader r = null;
int responseCode = -1;
String responseMessage = null;
Expand Down Expand Up @@ -609,6 +615,10 @@ private <T> T parse(Class<T> type, T instance) throws IOException {
// to preserve backward compatibility
throw e;
} catch (IOException e) {
if (e instanceof SocketTimeoutException && timeouts > 0) {
LOGGER.log(Level.INFO, "timed out accessing " + uc.getURL() + "; will try " + timeouts + " more time(s)", e);
return parse(type, instance, timeouts - 1);
}
throw new HttpException(responseCode, responseMessage, uc.getURL(), e);
} finally {
IOUtils.closeQuietly(r);
Expand Down

0 comments on commit 0f21eba

Please sign in to comment.