Files
Daan MeijerandClaude Opus 5.5 42b6cac3ee
Test and deploy / test (push) Successful in 38s
Test and deploy / deploy (push) Skipped
Rebuild: edge:node --max-mbit caps a node's download
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>
2026-10-09 09:01:57 +02:00

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)
}
}