-
Notifications
You must be signed in to change notification settings - Fork 741
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
62 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
src/main/java/org/kohsuke/github/extras/ImpatientHttpConnector.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package org.kohsuke.github.extras; | ||
|
||
import org.kohsuke.github.HttpConnector; | ||
|
||
import java.io.IOException; | ||
import java.net.HttpURLConnection; | ||
import java.net.URL; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
/** | ||
* {@link HttpConnector} wrapper that sets timeout | ||
* | ||
* @author Kohsuke Kawaguchi | ||
*/ | ||
public class ImpatientHttpConnector implements HttpConnector { | ||
private final HttpConnector base; | ||
private final int readTimeout, connectTimeout; | ||
|
||
/** | ||
* @param connectTimeout | ||
* HTTP connection timeout in milliseconds | ||
* @param readTimeout | ||
* HTTP read timeout in milliseconds | ||
*/ | ||
public ImpatientHttpConnector(HttpConnector base, int connectTimeout, int readTimeout) { | ||
this.base = base; | ||
this.connectTimeout = connectTimeout; | ||
this.readTimeout = readTimeout; | ||
} | ||
|
||
public ImpatientHttpConnector(HttpConnector base, int timeout) { | ||
this(base,timeout,timeout); | ||
} | ||
|
||
public ImpatientHttpConnector(HttpConnector base) { | ||
this(base,CONNECT_TIMEOUT,READ_TIMEOUT); | ||
} | ||
|
||
public HttpURLConnection connect(URL url) throws IOException { | ||
HttpURLConnection con = base.connect(url); | ||
con.setConnectTimeout(connectTimeout); | ||
con.setReadTimeout(readTimeout); | ||
return con; | ||
} | ||
|
||
/** | ||
* Default connection timeout in milliseconds | ||
*/ | ||
public static int CONNECT_TIMEOUT = (int) TimeUnit.SECONDS.toMillis(10); | ||
/** | ||
* Default read timeout in milliseconds | ||
*/ | ||
public static int READ_TIMEOUT = (int) TimeUnit.SECONDS.toMillis(10); | ||
} |