Skip to content
Source
Sign inSign up

admission.ts

17 lines557 Bytes

1import { HttpError } from './errors.ts';
2​
3// Isolate-wide resource counters contain no request data or I/O promises. Rate
4// limiting controls traffic; these counters additionally bound simultaneous buffers.
5export class Admission {
6 private stores = 0;
7 private reads = 0;
8 acquire(store: boolean): () => void {
9 if (store ? this.stores >= 2 : this.reads >= 4) throw new HttpError(503, 'concurrency_limit');
10 if (store) this.stores++;
11 else this.reads++;
12 return () => {
13 if (store) this.stores--;
14 else this.reads--;
15 };
16 }