Skip to content

Commit 49000e8

Browse files
committed
Improving docs of waitForAvailability, and transition from null to undefined
1 parent b21a5c1 commit 49000e8

File tree

4 files changed

+32
-11
lines changed

4 files changed

+32
-11
lines changed

dist/zero-backpressure-semaphore.d.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ export declare class ZeroBackpressureSemaphore<T, UncaughtErrorType = Error> {
135135
/**
136136
* waitForAvailability
137137
*
138-
* This method resolves once at least one slot (slot) is available for job execution.
138+
* This method resolves once at least one slot is available for job execution.
139139
* In other words, it resolves when the semaphore is available to trigger a new job immediately.
140140
*
141141
* ### Example Use Case
@@ -149,6 +149,13 @@ export declare class ZeroBackpressureSemaphore<T, UncaughtErrorType = Error> {
149149
* To prevent such potential backpressure, users can utilize the `waitForAvailability` method
150150
* before consuming the next message.
151151
*
152+
* ### Rarely Needed
153+
* This method can be useful when the system is experiencing high load (as indicated by CPU and/or memory
154+
* usage metrics), and you want to pause further async operations until an available job slot opens up.
155+
* However, the same effect can be achieved with `startExecution` alone if the async logic
156+
* (which you intend to delay until availability) is performed *inside the job* rather than as a
157+
* preliminary step. Therefore, `waitForAvailability` is more of a design choice than a necessity.
158+
*
152159
* @returns A promise that resolves once at least one slot is available.
153160
*/
154161
waitForAvailability(): Promise<void>;

dist/zero-backpressure-semaphore.js

Lines changed: 11 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/zero-backpressure-semaphore.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/zero-backpressure-semaphore.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ type PromiseResolveType = (value: void | PromiseLike<void>) => void;
5151
*/
5252
export class ZeroBackpressureSemaphore<T, UncaughtErrorType = Error> {
5353
private readonly _availableSlotsStack: Array<number>;
54-
private readonly _slots: Array<Promise<T> | null>;
54+
private readonly _slots: Array<Promise<T> | undefined>;
5555

5656
// Slot availability indicator:
5757
// A pending `_waitForAvailableSlot` promise indicates "all slots are taken". Its resolve
@@ -83,7 +83,7 @@ export class ZeroBackpressureSemaphore<T, UncaughtErrorType = Error> {
8383
this._availableSlotsStack[i] = i;
8484
}
8585

86-
this._slots = new Array(maxConcurrentJobs).fill(null);
86+
this._slots = new Array(maxConcurrentJobs).fill(undefined);
8787
}
8888

8989
/**
@@ -186,7 +186,7 @@ export class ZeroBackpressureSemaphore<T, UncaughtErrorType = Error> {
186186
* @returns A promise that resolves when all currently executing jobs are completed.
187187
*/
188188
public async waitForAllExecutingJobsToComplete(): Promise<void> {
189-
const pendingJobs = this._slots.filter(job => job !== null);
189+
const pendingJobs = this._slots.filter(job => job !== undefined);
190190
if (pendingJobs.length > 0) {
191191
await Promise.allSettled(pendingJobs);
192192
}
@@ -195,7 +195,7 @@ export class ZeroBackpressureSemaphore<T, UncaughtErrorType = Error> {
195195
/**
196196
* waitForAvailability
197197
*
198-
* This method resolves once at least one slot (slot) is available for job execution.
198+
* This method resolves once at least one slot is available for job execution.
199199
* In other words, it resolves when the semaphore is available to trigger a new job immediately.
200200
*
201201
* ### Example Use Case
@@ -209,6 +209,13 @@ export class ZeroBackpressureSemaphore<T, UncaughtErrorType = Error> {
209209
* To prevent such potential backpressure, users can utilize the `waitForAvailability` method
210210
* before consuming the next message.
211211
*
212+
* ### Rarely Needed
213+
* This method can be useful when the system is experiencing high load (as indicated by CPU and/or memory
214+
* usage metrics), and you want to pause further async operations until an available job slot opens up.
215+
* However, the same effect can be achieved with `startExecution` alone if the async logic
216+
* (which you intend to delay until availability) is performed *inside the job* rather than as a
217+
* preliminary step. Therefore, `waitForAvailability` is more of a design choice than a necessity.
218+
*
212219
* @returns A promise that resolves once at least one slot is available.
213220
*/
214221
public async waitForAvailability(): Promise<void> {
@@ -296,7 +303,7 @@ export class ZeroBackpressureSemaphore<T, UncaughtErrorType = Error> {
296303
// Triggered by `startExecution`: A background job.
297304
this._uncaughtErrors.push(err);
298305
} finally {
299-
this._slots[allottedSlot] = null;
306+
this._slots[allottedSlot] = undefined;
300307
this._availableSlotsStack.push(allottedSlot);
301308

302309
// Handle state change: from unavailable to available.

0 commit comments

Comments
 (0)