-
Notifications
You must be signed in to change notification settings - Fork 425
refactor(http): non-blocking transport for JdkHttpTransport #1010
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -15,10 +15,7 @@ | |||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||
| package io.agentscope.core.model.transport; | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| import java.io.BufferedReader; | ||||||||||||||||||||||||||||||
| import java.io.IOException; | ||||||||||||||||||||||||||||||
| import java.io.InputStream; | ||||||||||||||||||||||||||||||
| import java.io.InputStreamReader; | ||||||||||||||||||||||||||||||
| import java.net.InetSocketAddress; | ||||||||||||||||||||||||||||||
| import java.net.PasswordAuthentication; | ||||||||||||||||||||||||||||||
| import java.net.Proxy; | ||||||||||||||||||||||||||||||
|
|
@@ -28,7 +25,9 @@ | |||||||||||||||||||||||||||||
| import java.net.http.HttpClient; | ||||||||||||||||||||||||||||||
| import java.net.http.HttpClient.Redirect; | ||||||||||||||||||||||||||||||
| import java.net.http.HttpClient.Version; | ||||||||||||||||||||||||||||||
| import java.net.http.HttpHeaders; | ||||||||||||||||||||||||||||||
| import java.net.http.HttpResponse.BodyHandlers; | ||||||||||||||||||||||||||||||
| import java.nio.charset.Charset; | ||||||||||||||||||||||||||||||
| import java.nio.charset.StandardCharsets; | ||||||||||||||||||||||||||||||
| import java.security.KeyManagementException; | ||||||||||||||||||||||||||||||
| import java.security.NoSuchAlgorithmException; | ||||||||||||||||||||||||||||||
|
|
@@ -38,17 +37,19 @@ | |||||||||||||||||||||||||||||
| import java.util.List; | ||||||||||||||||||||||||||||||
| import java.util.Map; | ||||||||||||||||||||||||||||||
| import java.util.Objects; | ||||||||||||||||||||||||||||||
| import java.util.concurrent.CompletableFuture; | ||||||||||||||||||||||||||||||
| import java.util.StringJoiner; | ||||||||||||||||||||||||||||||
| import java.util.concurrent.CompletionException; | ||||||||||||||||||||||||||||||
| import java.util.concurrent.Flow; | ||||||||||||||||||||||||||||||
| import java.util.concurrent.atomic.AtomicBoolean; | ||||||||||||||||||||||||||||||
| import java.util.concurrent.atomic.AtomicReference; | ||||||||||||||||||||||||||||||
| import javax.net.ssl.SSLContext; | ||||||||||||||||||||||||||||||
| import javax.net.ssl.TrustManager; | ||||||||||||||||||||||||||||||
| import javax.net.ssl.X509TrustManager; | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| import org.slf4j.Logger; | ||||||||||||||||||||||||||||||
| import org.slf4j.LoggerFactory; | ||||||||||||||||||||||||||||||
| import reactor.core.publisher.Flux; | ||||||||||||||||||||||||||||||
| import reactor.core.publisher.Mono; | ||||||||||||||||||||||||||||||
| import reactor.core.scheduler.Schedulers; | ||||||||||||||||||||||||||||||
| import reactor.core.publisher.Sinks; | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||
| * Pure JDK implementation of the HttpTransport interface. | ||||||||||||||||||||||||||||||
|
|
@@ -195,86 +196,89 @@ public Flux<String> stream(HttpRequest request) { | |||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| var jdkRequest = buildJdkRequest(request); | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| // Check status code and read error body immediately when CompletableFuture completes | ||||||||||||||||||||||||||||||
| // to avoid stream being closed before we can read it | ||||||||||||||||||||||||||||||
| CompletableFuture<java.net.http.HttpResponse<InputStream>> future = | ||||||||||||||||||||||||||||||
| client.sendAsync(jdkRequest, BodyHandlers.ofInputStream()) | ||||||||||||||||||||||||||||||
| .thenApply( | ||||||||||||||||||||||||||||||
| response -> { | ||||||||||||||||||||||||||||||
| int statusCode = response.statusCode(); | ||||||||||||||||||||||||||||||
| if (statusCode < 200 || statusCode >= 300) { | ||||||||||||||||||||||||||||||
| // Read error body immediately while stream is still open | ||||||||||||||||||||||||||||||
| String errorBody = readInputStream(response.body()); | ||||||||||||||||||||||||||||||
| log.warn( | ||||||||||||||||||||||||||||||
| "HTTP request failed. URL: {} | Status: {} | Error:" | ||||||||||||||||||||||||||||||
| + " {}", | ||||||||||||||||||||||||||||||
| request.getUrl(), | ||||||||||||||||||||||||||||||
| statusCode, | ||||||||||||||||||||||||||||||
| errorBody); | ||||||||||||||||||||||||||||||
| throw new CompletionException( | ||||||||||||||||||||||||||||||
| new HttpTransportException( | ||||||||||||||||||||||||||||||
| "HTTP request failed with status " | ||||||||||||||||||||||||||||||
| + statusCode | ||||||||||||||||||||||||||||||
| + " | " | ||||||||||||||||||||||||||||||
| + errorBody, | ||||||||||||||||||||||||||||||
| statusCode, | ||||||||||||||||||||||||||||||
| errorBody)); | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| return response; | ||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| return Mono.fromCompletionStage(future) | ||||||||||||||||||||||||||||||
| .flatMapMany(response -> processStreamResponse(response, request)) | ||||||||||||||||||||||||||||||
| .publishOn(Schedulers.boundedElastic()) | ||||||||||||||||||||||||||||||
| Sinks.Many<String> sink = Sinks.many().multicast().onBackpressureBuffer(); | ||||||||||||||||||||||||||||||
| java.net.http.HttpResponse.BodyHandler<String> responseBodyHandler = | ||||||||||||||||||||||||||||||
| responseInfo -> | ||||||||||||||||||||||||||||||
| java.net.http.HttpResponse.BodySubscribers.fromLineSubscriber(new ResponseLineSubscriber(sink, responseInfo), | ||||||||||||||||||||||||||||||
| ResponseLineSubscriber::getErrorMessage, | ||||||||||||||||||||||||||||||
| charsetFrom(responseInfo.headers()), | ||||||||||||||||||||||||||||||
| null); | ||||||||||||||||||||||||||||||
| client.sendAsync(jdkRequest, responseBodyHandler) | ||||||||||||||||||||||||||||||
| .whenComplete((response, throwable) -> { | ||||||||||||||||||||||||||||||
| if (throwable != null) { | ||||||||||||||||||||||||||||||
| //e.g. java.util.concurrent.CompletionException: java.net.ConnectException | ||||||||||||||||||||||||||||||
| sink.tryEmitError(throwable); | ||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||
| int statusCode = response.statusCode(); | ||||||||||||||||||||||||||||||
| log.debug("Received HTTP response with status code {}", statusCode); | ||||||||||||||||||||||||||||||
| if (statusCode >= 200 && statusCode < 300) { | ||||||||||||||||||||||||||||||
| //ignore successful | ||||||||||||||||||||||||||||||
| return; | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| String errorBody = response.body(); | ||||||||||||||||||||||||||||||
| log.warn("HTTP request failed. URL: {} | Status: {} | Error: {}", | ||||||||||||||||||||||||||||||
| request.getUrl(), | ||||||||||||||||||||||||||||||
| statusCode, | ||||||||||||||||||||||||||||||
| errorBody); | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||
| Flux<String> lineFlux = sink.asFlux() | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
| .onErrorMap( | ||||||||||||||||||||||||||||||
| e -> !(e instanceof HttpTransportException), | ||||||||||||||||||||||||||||||
| e -> { | ||||||||||||||||||||||||||||||
| Throwable cause = e instanceof CompletionException ? e.getCause() : e; | ||||||||||||||||||||||||||||||
| if (cause instanceof HttpTransportException) { | ||||||||||||||||||||||||||||||
| return (HttpTransportException) cause; | ||||||||||||||||||||||||||||||
| if (cause instanceof HttpTransportException hte) { | ||||||||||||||||||||||||||||||
| return hte; | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| return new HttpTransportException( | ||||||||||||||||||||||||||||||
| "SSE/NDJSON stream failed: " + e.getMessage(), e); | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
| return new HttpTransportException( | |
| "SSE/NDJSON stream failed: " + e.getMessage(), e); | |
| String causeMessage; | |
| if (cause != null && cause.getMessage() != null) { | |
| causeMessage = cause.getMessage(); | |
| } else if (cause != null) { | |
| causeMessage = cause.toString(); | |
| } else if (e.getMessage() != null) { | |
| causeMessage = e.getMessage(); | |
| } else { | |
| causeMessage = e.toString(); | |
| } | |
| return new HttpTransportException( | |
| "SSE/NDJSON stream failed: " + causeMessage, cause); |
Uh oh!
There was an error while loading. Please reload this page.