Skip to content

Commit

Permalink
Bump version to v1.0.2: Improve README and add negative test for inva…
Browse files Browse the repository at this point in the history
…lid key
  • Loading branch information
ori88c committed Feb 27, 2025
1 parent 8bf8b1a commit 7f1663c
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 20 deletions.
30 changes: 14 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,25 +129,23 @@ export class StockOrdersConsumer {
const semaphore = new ZeroBackpressureSemaphore<void>();
const userLock = new ZeroOverheadKeyedLock<void>();

// Register a new order only when the semaphore indicates availability,
// to avoid backpressure.
for (const { value, key } of orderMessages) {
const order: IStockOrder = JSON.parse(value.toString());
const userID = key.toString();

await semaphore.startExecution(async (): Promise<void> => {
const alreadyActive = userLock.isActiveKey(userID);
const executeExclusivePromise = userLock.executeExclusive(
userID,
(): Promise<void> => this._processOrder(order)
);
if (alreadyActive) {
// No need to await; this key already occupies capacity in the semaphore.
return;
}

await executeExclusivePromise;
});
const alreadyActive = userLock.isActiveKey(userID);
const executeExclusive = () => userLock.executeExclusive(
userID,
() => this._processOrder(order)
);
if (alreadyActive) {
// No need to await; this key already occupies capacity in the semaphore.
// In other words, the semaphore is currently processing a previous order
// belonging to the current userID.
executeExclusive();
continue;
}

await semaphore.startExecution(executeExclusive);
}

// Graceful teardown is not only important during application shutdowns;
Expand Down
10 changes: 10 additions & 0 deletions dist/zero-overhead-keyed-promise-lock.test.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/zero-overhead-keyed-promise-lock.test.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "zero-overhead-keyed-promise-lock",
"version": "1.0.1",
"version": "1.0.2",
"description": "An efficient keyed Promise lock for Node.js projects, ensuring the mutually exclusive execution of tasks associated with the same key. Key features include active key metrics and the ability to gracefully await the completion of all currently executing or pending tasks, making it ideal for robust production applications requiring smooth teardown.",
"repository": {
"type": "git",
Expand Down
Loading

0 comments on commit 7f1663c

Please sign in to comment.