prsm/packages/duplex/src/server/ids.ts
nvms 20fa3707ff fix: improve connection reliability and add comprehensive tests
- Make connect() methods return Promises for better async control
- Remove automatic connections in constructors to prevent race conditions
- Handle ECONNRESET errors gracefully during disconnection
- Add comprehensive test suite covering reconnection, timeouts, and concurrency
2025-03-26 16:51:03 -04:00

45 lines
869 B
TypeScript

export class IdManager {
ids: Array<true | false> = [];
index: number = 0;
maxIndex: number;
constructor(maxIndex: number = 2 ** 16 - 1) {
this.maxIndex = maxIndex;
}
release(id: number) {
if (id < 0 || id > this.maxIndex) {
throw new TypeError(
`ID must be between 0 and ${this.maxIndex}. Got ${id}.`,
);
}
this.ids[id] = false;
}
reserve(): number {
const startIndex = this.index;
while (true) {
const i = this.index;
if (!this.ids[i]) {
this.ids[i] = true;
return i;
}
if (this.index >= this.maxIndex) {
this.index = 0;
} else {
this.index++;
}
if (this.index === startIndex) {
throw new Error(
`All IDs are reserved. Make sure to release IDs when they are no longer used.`,
);
}
}
}
}