-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
It appears that the JDK's http client has a bug which causes it to hang infinetely. This commit adds a workaround by wrapping the call into an executor with a hard timeout. See also: https://bugs.openjdk.org/browse/JDK-8258397 https://bugs.openjdk.org/browse/JDK-8208693 https://bugs.openjdk.org/browse/JDK-8254223
- Loading branch information
Showing
7 changed files
with
186 additions
and
27 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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package de.malkusch.km200.http; | ||
|
||
import java.io.IOException; | ||
import java.net.HttpURLConnection; | ||
import java.net.SocketTimeoutException; | ||
import java.net.URL; | ||
import java.net.http.HttpClient; | ||
import java.net.http.HttpTimeoutException; | ||
import java.time.Duration; | ||
|
||
import de.malkusch.km200.KM200Exception; | ||
import de.malkusch.km200.http.Http.Request.Post; | ||
|
||
public final class UrlHttp extends Http { | ||
|
||
private final String uri; | ||
private final String userAgent; | ||
private final int timeoutMillis; | ||
|
||
/** | ||
* Avoid undesired POST retries from UrlConnection | ||
* | ||
* {@link HttpClient} | ||
*/ | ||
static { | ||
System.setProperty("sun.net.http.retryPost", "false"); | ||
} | ||
|
||
public UrlHttp(String uri, String userAgent, Duration timeout) { | ||
this.uri = uri; | ||
this.userAgent = userAgent; | ||
this.timeoutMillis = (int) timeout.toMillis(); | ||
} | ||
|
||
@Override | ||
protected Response exchange(Request request) throws IOException, InterruptedException, KM200Exception { | ||
var connection = (HttpURLConnection) new URL(uri + request.path()).openConnection(); | ||
connection.setConnectTimeout(timeoutMillis); | ||
connection.setReadTimeout(timeoutMillis); | ||
connection.setRequestProperty("User-Agent", userAgent); | ||
|
||
if (request instanceof Post) { | ||
connection.setDoOutput(true); | ||
connection.setRequestProperty("Accept", "application/json"); | ||
} | ||
|
||
try { | ||
connection.connect(); | ||
|
||
if (request instanceof Post post) { | ||
try (var output = connection.getOutputStream()) { | ||
output.write(post.body()); | ||
} | ||
} | ||
|
||
var status = connection.getResponseCode(); | ||
if (status == -1) { | ||
throw new IOException(request + " received invalid HTTP response"); | ||
} | ||
|
||
try (var input = connection.getErrorStream() != null ? connection.getErrorStream() | ||
: connection.getInputStream()) { | ||
|
||
var body = input.readAllBytes(); | ||
var response = new Response(status, body); | ||
return assertHttpOk(request, response); | ||
} | ||
|
||
} catch (SocketTimeoutException e) { | ||
throw new HttpTimeoutException(request + " timed out"); | ||
|
||
} finally { | ||
connection.disconnect(); | ||
} | ||
} | ||
} |
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