Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 1.8k
fix(cloudflare): Use TransformStream to keep track of streams#20452
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -129,40 +129,36 @@ export function wrapRequestHandler( | ||
| const classification = classifyResponseStreaming(res); | ||
| if (classification.isStreaming && res.body) { | ||
| // Streaming response detected - monitor consumption to keep span alive | ||
| try { | ||
| const [clientStream, monitorStream] = res.body.tee(); | ||
| // Monitor stream consumption and end span when complete | ||
| const streamMonitor = (async () => { | ||
| const reader = monitorStream.getReader(); | ||
| try { | ||
| let done = false; | ||
| while (!done) { | ||
| const result = await reader.read(); | ||
| done = result.done; | ||
| } | ||
| } catch { | ||
| // Stream error or cancellation - will end span in finally | ||
| } finally { | ||
| reader.releaseLock(); | ||
| span.end(); | ||
| waitUntil?.(flushAndDispose(client)); | ||
| } | ||
| })(); | ||
| // Keep worker alive until stream monitoring completes (otherwise span won't end) | ||
| waitUntil?.(streamMonitor); | ||
| // Return response with client stream | ||
| return new Response(clientStream, { | ||
| let ended = false; | ||
| const endSpanOnce = (): void => { | ||
| if (ended) return; | ||
| ended = true; | ||
| span.end(); | ||
| waitUntil?.(flushAndDispose(client)); | ||
| }; | ||
| const transform = new TransformStream({ | ||
| flush() { | ||
| // Source stream completed normally. | ||
| endSpanOnce(); | ||
| }, | ||
| cancel() { | ||
| // Client disconnected (or downstream cancelled). The `cancel` | ||
| // is being called while the response is still considered | ||
| // active, so this is a safe place to end the span. | ||
| endSpanOnce(); | ||
JPeer264 marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| }, | ||
| }); | ||
JPeer264 marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| return new Response(res.body.pipeThrough(transform), { | ||
| status: res.status, | ||
| statusText: res.statusText, | ||
| headers: res.headers, | ||
| }); | ||
| } catch (_e) { | ||
| // tee() failed (e.g stream already locked) - fall back to non-streaming handling | ||
| } catch { | ||
| span.end(); | ||
| waitUntil?.(flushAndDispose(client)); | ||
| return res; | ||
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.