Import packages/webrtc/src/serial-send.ts

This commit is contained in:
QQ 2026-11-14 15:05:53 +08:00
parent e704f15821
commit 233084c740
1 changed files with 13 additions and 0 deletions

View File

@ -0,0 +1,13 @@
/** Serialize encryption with transmission, bounded even when the transport stalls. */
export class SerialSend {
private tail: Promise<void> = Promise.resolve()
private bytes = 0
private count = 0
async run(bytes: number, send: () => Promise<void>): Promise<void> {
if (this.bytes + bytes > 8 * 1024 * 1024 || this.count >= 256) throw new Error('Encrypted send queue exceeds its limit')
this.bytes += bytes; this.count += 1
const task = this.tail.then(send)
this.tail = task.catch(() => undefined)
try { await task } finally { this.bytes -= bytes; this.count -= 1 }
}
}