For workers on a shared line (poweredge-kantoor: 160 Mbit/s): the range stream is read at most at the cap, TCP holds the coordinator back. The cap is the node's, split over its processes (also EDGE_MAX_MBIT). REBUILD.md: where workers run (connections set the speed; remote workers take CPU off homelab). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
19 lines
707 B
TypeScript
19 lines
707 B
TypeScript
import { setTimeout as sleep } from 'node:timers/promises'
|
|
|
|
/**
|
|
* Passes a byte stream on at most `bytesPerSecond` on average: a worker on a
|
|
* line it shares (`edge:node --max-mbit`) reads its range slower, and TCP
|
|
* holds the sender back. The first second's worth goes at once.
|
|
*/
|
|
export async function* throttle<T extends Uint8Array>(stream: AsyncIterable<T> | Iterable<T>, bytesPerSecond: number): AsyncGenerator<T> {
|
|
const started = Date.now()
|
|
let bytes = 0
|
|
for await (const piece of stream) {
|
|
yield piece
|
|
bytes += piece.byteLength
|
|
const due = started + ((bytes - bytesPerSecond) / bytesPerSecond) * 1000
|
|
const wait = due - Date.now()
|
|
if (wait > 0) await sleep(wait)
|
|
}
|
|
}
|