fix(transport): resolve SSE stream timeout in JdkHttpTransport#1322
Open
jujn wants to merge 1 commit into
Open
fix(transport): resolve SSE stream timeout in JdkHttpTransport#1322jujn wants to merge 1 commit into
jujn wants to merge 1 commit into
Conversation
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Close #1302
This PR introduces a structural fix for
JdkHttpTransportwhen handling long-running Server-Sent Events (SSE) streams, particularly for LLMs with long Time-To-First-Token (TTFT) requirements.Previously, the combination of JDK HttpClient's absolute timeout semantics and synchronous stream reading within a reactive pipeline led to premature request terminations, leaked connections, and severe NIO thread pool starvation.
Key Changes & Architectural Improvements:
Removed Absolute Timeout for Streams: Bypassed the
java.net.http.HttpRequest.timeout()for streaming requests. The JDK's absolute timeout forcefully cuts off connections regardless of ongoing data transfer, which is fundamentally incompatible with LLM streaming.Dual-Stage Reactive Timeout Strategy:
Moved timeout management to the Reactor layer using a custom
timeout()operator:responseTimeout(TTFT): Tolerates long-thinking models waiting for the first byte/token (Default: 5 mins).streamIdleTimeout(Inter-token gap): Resets upon receiving every data chunk (Default: 30 seconds).Prevent NIO Thread Starvation:
Added
.subscribeOn(Schedulers.boundedElastic())after the HTTP response is received. This guarantees that the blocking I/O operations (BufferedReader.readLine()) used to parse the NDJSON/SSE stream do not hijack and block the JDK HttpClient's highly limited internal NIO selector threads.Fixed "Ghost Connection" Leaks:
Wrapped the asynchronous JDK request in
Mono.fromFuture(() -> client.sendAsync(...)). TheSupplierensures lazy execution, and usingfromFuture(instead offromCompletionStage) guarantees that Reactor's cancellation signals correctly triggerfuture.cancel(true), closing the underlying socket if the downstream aborts or times out before headers arrive.Checklist
Please check the following items before code is ready to be reviewed.
mvn spotless:applymvn test)