From cb7620395a18caace541e66b7637194d89ef8d21 Mon Sep 17 00:00:00 2001 From: Jesse Glick Date: Wed, 28 Jun 2017 17:09:37 -0400 Subject: [PATCH] [JENKINS-45142] Retry connections after getting SocketTimeoutException. --- src/main/java/org/kohsuke/github/Requester.java | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/main/java/org/kohsuke/github/Requester.java b/src/main/java/org/kohsuke/github/Requester.java index 3cd338b8c2..d7d6239190 100644 --- a/src/main/java/org/kohsuke/github/Requester.java +++ b/src/main/java/org/kohsuke/github/Requester.java @@ -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; @@ -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; @@ -579,6 +581,10 @@ private void setRequestMethod(HttpURLConnection uc) throws IOException { } private T parse(Class type, T instance) throws IOException { + return parse(type, instance, 2); + } + + private T parse(Class type, T instance, int timeouts) throws IOException { InputStreamReader r = null; int responseCode = -1; String responseMessage = null; @@ -609,6 +615,10 @@ private T parse(Class 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);