Skip to content

Commit 4fa1681

Browse files
committed
fix: 优化上行数据处理逻辑,增加取消上行reader的处理,改进缓冲管理
1 parent 1a13d7d commit 4fa1681

1 file changed

Lines changed: 36 additions & 26 deletions

File tree

_worker.js

Lines changed: 36 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -664,15 +664,23 @@ async function 处理叉HTTP请求(request, yourUUID, 反代上下文 = {}) {
664664
const 搬运Promise = 上行合包器.readable.pipeTo(socket.writable, { signal: abortController.signal });
665665
void 搬运Promise.catch(() => { });
666666
const 上行reader = request.body.getReader();
667+
const 取消上行reader = () => {
668+
try { 上行reader.cancel(abortController.signal.reason).catch(() => { }); } catch (e) { }
669+
};
670+
abortController.signal.addEventListener('abort', 取消上行reader, { once: true });
667671
try {
668-
while (true) {
669-
const { done, value } = await 上行reader.read();
670-
if (done) break;
671-
if (value?.byteLength) await 上行合包器.写入(value);
672+
try {
673+
while (true) {
674+
const { done, value } = await 上行reader.read();
675+
if (done) break;
676+
if (value?.byteLength) await 上行合包器.写入(value);
677+
}
678+
} finally {
679+
abortController.signal.removeEventListener('abort', 取消上行reader);
680+
try { 上行reader.releaseLock() } catch (e) { }
672681
}
673-
await 上行合包器.结束();
674682
} finally {
675-
try { 上行reader.releaseLock() } catch (e) { }
683+
try { await 上行合包器.结束() } catch (e) { }
676684
}
677685
await 搬运Promise;
678686
})();
@@ -2592,7 +2600,8 @@ function 创建上行Grain合包流(目标字节 = 上行合包目标字节) {
25922600
? new IdentityTransformStream()
25932601
: new TransformStream();
25942602
const writer = identity.writable.getWriter();
2595-
let 缓冲 = new Uint8Array(0);
2603+
const 缓冲 = new Uint8Array(目标字节);
2604+
let 缓冲长度 = 0;
25962605
let 定时器 = null;
25972606
let 在途写 = null;
25982607

@@ -2603,12 +2612,17 @@ function 创建上行Grain合包流(目标字节 = 上行合包目标字节) {
26032612
}
26042613
};
26052614

2615+
const 串行写 = async (chunk) => {
2616+
if (在途写) await 在途写;
2617+
在途写 = writer.write(chunk);
2618+
try { await 在途写 } finally { 在途写 = null; }
2619+
};
2620+
26062621
const 冲刷 = async () => {
2607-
if (缓冲.byteLength) {
2608-
const chunk = 缓冲;
2609-
缓冲 = new Uint8Array(0);
2610-
在途写 = writer.write(chunk);
2611-
try { await 在途写 } finally { 在途写 = null; }
2622+
if (缓冲长度) {
2623+
const chunk = 缓冲.slice(0, 缓冲长度);
2624+
缓冲长度 = 0;
2625+
await 串行写(chunk);
26122626
}
26132627
};
26142628

@@ -2623,27 +2637,23 @@ function 创建上行Grain合包流(目标字节 = 上行合包目标字节) {
26232637
return {
26242638
readable: identity.readable,
26252639
写入: async (chunk) => {
2626-
if (在途写) await 在途写;
26272640
const data = 数据转Uint8Array(chunk);
26282641
if (!data.byteLength) return;
2629-
if (缓冲.byteLength === 0 && data.byteLength >= 目标字节) {
2642+
if (缓冲长度 === 0 && data.byteLength >= 目标字节) {
26302643
清理定时器();
2631-
await writer.write(data);
2644+
await 串行写(data);
26322645
return;
26332646
}
2634-
const 合并后 = 缓冲.byteLength + data.byteLength;
2635-
if (合并后 >= 目标字节) {
2636-
const output = new Uint8Array(合并后);
2637-
output.set(缓冲, 0);
2638-
output.set(data, 缓冲.byteLength);
2639-
缓冲 = new Uint8Array(0);
2647+
if (缓冲长度 + data.byteLength >= 目标字节) {
2648+
const output = new Uint8Array(缓冲长度 + data.byteLength);
2649+
output.set(缓冲.subarray(0, 缓冲长度), 0);
2650+
output.set(data, 缓冲长度);
2651+
缓冲长度 = 0;
26402652
清理定时器();
2641-
await writer.write(output);
2653+
await 串行写(output);
26422654
} else {
2643-
const output = new Uint8Array(合并后);
2644-
output.set(缓冲, 0);
2645-
output.set(data, 缓冲.byteLength);
2646-
缓冲 = output;
2655+
缓冲.set(data, 缓冲长度);
2656+
缓冲长度 += data.byteLength;
26472657
启动定时器();
26482658
}
26492659
},

0 commit comments

Comments
 (0)