Skip to content

Commit c3c3f4a

Browse files
committed
feat: 添加上行Grain合包流功能以优化数据传输
1 parent 2b27a6d commit c3c3f4a

1 file changed

Lines changed: 88 additions & 2 deletions

File tree

_worker.js

Lines changed: 88 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -641,7 +641,7 @@ async function 处理叉HTTP请求(request, yourUUID, 反代上下文 = {}) {
641641
if (已清理) return;
642642
已清理 = true;
643643
try { abortController.abort(reason) } catch (e) { }
644-
失效TCP连接世代(remoteConnWrapper); // 关闭 socket + 世代 +1
644+
失效TCP连接世代(remoteConnWrapper);
645645
};
646646

647647
const 占位WS = { readyState: WebSocket.OPEN };
@@ -660,7 +660,21 @@ async function 处理叉HTTP请求(request, yourUUID, 反代上下文 = {}) {
660660
}
661661

662662
const 上行Promise = (async () => {
663-
await request.body.pipeTo(socket.writable, { signal: abortController.signal });
663+
const 上行合包器 = 创建上行Grain合包流();
664+
const 搬运Promise = 上行合包器.readable.pipeTo(socket.writable, { signal: abortController.signal });
665+
void 搬运Promise.catch(() => { });
666+
const 上行reader = request.body.getReader();
667+
try {
668+
while (true) {
669+
const { done, value } = await 上行reader.read();
670+
if (done) break;
671+
if (value?.byteLength) await 上行合包器.写入(value);
672+
}
673+
await 上行合包器.结束();
674+
} finally {
675+
try { 上行reader.releaseLock() } catch (e) { }
676+
}
677+
await 搬运Promise;
664678
})();
665679

666680
const 响应流 = typeof IdentityTransformStream !== 'undefined'
@@ -2573,6 +2587,78 @@ function 创建Grain收纳器(容量, 复制合包结果 = false) {
25732587
};
25742588
}
25752589

2590+
function 创建上行Grain合包流(目标字节 = 上行合包目标字节) {
2591+
const identity = typeof IdentityTransformStream !== 'undefined'
2592+
? new IdentityTransformStream()
2593+
: new TransformStream();
2594+
const writer = identity.writable.getWriter();
2595+
let 缓冲 = new Uint8Array(0);
2596+
let 定时器 = null;
2597+
let 在途写 = null;
2598+
2599+
const 清理定时器 = () => {
2600+
if (定时器) {
2601+
clearTimeout(定时器);
2602+
定时器 = null;
2603+
}
2604+
};
2605+
2606+
const 冲刷 = async () => {
2607+
if (缓冲.byteLength) {
2608+
const chunk = 缓冲;
2609+
缓冲 = new Uint8Array(0);
2610+
在途写 = writer.write(chunk);
2611+
try { await 在途写 } finally { 在途写 = null; }
2612+
}
2613+
};
2614+
2615+
const 启动定时器 = () => {
2616+
if (定时器) return;
2617+
定时器 = setTimeout(() => {
2618+
定时器 = null;
2619+
冲刷().catch(e => { });
2620+
}, 1);
2621+
};
2622+
2623+
return {
2624+
readable: identity.readable,
2625+
写入: async (chunk) => {
2626+
if (在途写) await 在途写;
2627+
const data = 数据转Uint8Array(chunk);
2628+
if (!data.byteLength) return;
2629+
if (缓冲.byteLength === 0 && data.byteLength >= 目标字节) {
2630+
清理定时器();
2631+
await writer.write(data);
2632+
return;
2633+
}
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);
2640+
清理定时器();
2641+
await writer.write(output);
2642+
} else {
2643+
const output = new Uint8Array(合并后);
2644+
output.set(缓冲, 0);
2645+
output.set(data, 缓冲.byteLength);
2646+
缓冲 = output;
2647+
启动定时器();
2648+
}
2649+
},
2650+
结束: async () => {
2651+
清理定时器();
2652+
try {
2653+
await 冲刷();
2654+
await writer.close();
2655+
} finally {
2656+
try { writer.releaseLock() } catch (e) { }
2657+
}
2658+
}
2659+
};
2660+
}
2661+
25762662
function 创建上行写入队列({ 获取写入器, 获取连接任务 = null, 释放写入器, 重试连接, 关闭连接, 名称 = '上行队列' }) {
25772663
const grain = 创建Grain收纳器(上行合包目标字节);
25782664
let draining = false;

0 commit comments

Comments
 (0)