From 205174255fe42891fec4e8047e552d8ce758d9c1 Mon Sep 17 00:00:00 2001 From: pk5ls20 Date: Thu, 17 Oct 2024 22:41:39 +0800 Subject: [PATCH] feat: Introduce a 10ms delay to `sendSsoCmdReqByContend` and cache `prepareUpload` requests --- src/core/packet/client.ts | 2 ++ src/core/packet/highway/session.ts | 10 +++++++--- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/core/packet/client.ts b/src/core/packet/client.ts index 37b84822..78cb4910 100644 --- a/src/core/packet/client.ts +++ b/src/core/packet/client.ts @@ -4,6 +4,7 @@ import WebSocket, { Data } from "ws"; import crypto, { createHash } from "crypto"; import { NapCatCore } from "@/core"; import { PacketHexStr } from "@/core/packet/packer"; +import {sleep} from "@/common/helper"; export interface RecvPacket { type: string, // 仅recv @@ -171,6 +172,7 @@ export class PacketClient { const md5 = crypto.createHash('md5').update(data).digest('hex'); const trace_id = (this.randText(4) + md5 + data).slice(0, data.length / 2); this.sendCommand(cmd, data, trace_id, rsp, 20000, async () => { + await sleep(10); await this.napCatCore.context.session.getMsgService().sendSsoCmdReqByContend(cmd, trace_id); }).then((res) => resolve(res)).catch((e: Error) => reject(e)); }); diff --git a/src/core/packet/highway/session.ts b/src/core/packet/highway/session.ts index 3e559482..40e3527c 100644 --- a/src/core/packet/highway/session.ts +++ b/src/core/packet/highway/session.ts @@ -32,6 +32,7 @@ export class PacketHighwaySession { protected sig: PacketHighwaySig; protected logger: LogWrapper; protected packer: PacketPacker; + private cachedPrepareReq: Promise | null = null; constructor(logger: LogWrapper, client: PacketClient) { this.packetClient = client; @@ -53,12 +54,15 @@ export class PacketHighwaySession { } if (this.sig.sigSession === null || this.sig.sessionKey === null) { this.logger.logWarn('[Highway] sigSession or sessionKey not available!'); - await this.prepareUpload(); + if (this.cachedPrepareReq === null) { + this.cachedPrepareReq = this.prepareUpload().finally(() => { + this.cachedPrepareReq = null; + }); + } + await this.cachedPrepareReq; } } - // TODO: add signal to auto prepare when ready - // TODO: refactor private async prepareUpload(): Promise { const packet = this.packer.packHttp0x6ff_501(); const req = await this.packetClient.sendPacket('HttpConn.0x6ff_501', packet, true);