fix: pace captured SSE replay
This commit is contained in:
+15
-11
@@ -4079,7 +4079,7 @@ function cloneResponseHeaders(sourceHeaders) {
|
||||
return headers;
|
||||
}
|
||||
|
||||
const CAPTURED_STREAM_REPLAY_CHUNKS_PER_TICK = 1;
|
||||
const CAPTURED_STREAM_REPLAY_DELAY_MS = 5;
|
||||
|
||||
function cloneBufferList(chunks) {
|
||||
if (!Array.isArray(chunks) || chunks.length === 0) {
|
||||
@@ -4091,13 +4091,16 @@ function cloneBufferList(chunks) {
|
||||
}
|
||||
|
||||
async function writeBufferedStreamChunks(res, chunks) {
|
||||
let writesSinceYield = 0;
|
||||
for (const chunk of chunks || []) {
|
||||
if (!chunk || chunk.length === 0 || res.destroyed || res.writableEnded) {
|
||||
continue;
|
||||
const replayChunks = (chunks || []).filter((chunk) => chunk && chunk.length > 0);
|
||||
res.socket?.setNoDelay(true);
|
||||
res.flushHeaders?.();
|
||||
|
||||
for (let index = 0; index < replayChunks.length; index += 1) {
|
||||
if (res.destroyed || res.writableEnded) {
|
||||
break;
|
||||
}
|
||||
const chunk = replayChunks[index];
|
||||
const accepted = res.write(chunk);
|
||||
writesSinceYield += 1;
|
||||
if (!accepted) {
|
||||
await new Promise((resolve) => {
|
||||
let settled = false;
|
||||
@@ -4117,12 +4120,13 @@ async function writeBufferedStreamChunks(res, chunks) {
|
||||
res.once("close", onClose);
|
||||
res.once("error", onClose);
|
||||
});
|
||||
writesSinceYield = 0;
|
||||
continue;
|
||||
}
|
||||
if (writesSinceYield >= CAPTURED_STREAM_REPLAY_CHUNKS_PER_TICK) {
|
||||
writesSinceYield = 0;
|
||||
await new Promise((resolve) => setImmediate(resolve));
|
||||
if (
|
||||
index < replayChunks.length - 1 &&
|
||||
!res.destroyed &&
|
||||
!res.writableEnded
|
||||
) {
|
||||
await sleep(CAPTURED_STREAM_REPLAY_DELAY_MS);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user