Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const tests = [
'deadlock',
'recursive-deadlock',
'thread-main',
'lock-time',
];

(async () => {
Expand Down
18 changes: 18 additions & 0 deletions test/lock-time.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
'use strict';

const assert = require('assert').strict;
const { locks } = require('..');
const { sleep } = require('./test-utils');
Comment thread
anton-iskryzhytskyi marked this conversation as resolved.
Outdated

const TIME_TO_PROCESS = 200;
const TIME_TO_LOCK = 100;

module.exports = async () => {
const startTs = Date.now();

await locks.request('LockTime', { timeout: TIME_TO_LOCK }, async () => {
await sleep(TIME_TO_PROCESS);
});

assert.strictEqual(Date.now() - startTs < TIME_TO_PROCESS, true);
};
8 changes: 1 addition & 7 deletions test/steps.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,7 @@

const assert = require('assert').strict;
const { locks } = require('..');

const sleep = msec =>
new Promise(resolve => {
setTimeout(() => {
resolve();
}, msec);
});
const { sleep } = require('./test-utils');
Comment thread
anton-iskryzhytskyi marked this conversation as resolved.
Outdated

let counter = 0;

Expand Down
10 changes: 10 additions & 0 deletions test/test-utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
'use strict';

const sleep = msec =>
new Promise(resolve => {
setTimeout(resolve, msec);
});

module.exports = {
sleep,
};
6 changes: 1 addition & 5 deletions test/thread-worker.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
'use strict';

const threads = require('worker_threads');
const { sleep } = require('./test-utils');
Comment thread
anton-iskryzhytskyi marked this conversation as resolved.
Outdated

const { locks } = require('..');

const sleep = msec =>
new Promise(resolve => {
setTimeout(resolve, msec);
});

(async () => {
if (threads.threadId === 2) {
await sleep(10);
Expand Down
20 changes: 13 additions & 7 deletions web-locks.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ class Lock {
if (!buffer) Atomics.store(this.flag, 0, UNLOCKED);
}

enter(handler) {
enter(handler, timeout) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That it much better, but we need to compare passing timeout and timer instance.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we need to use clearTimeout on abort?

Hmm, maybe it would be better to throw an error on timeout, similar to abort behavior?
Also, move all resolve / reject invocations to one place.

return new Promise(resolve => {
this.queue.push({ handler, resolve });
this.queue.push({ handler, resolve, timeout });
this.trying = true;
setTimeout(() => {
this.tryEnter();
Expand All @@ -38,11 +38,17 @@ class Lock {
if (prev === LOCKED) return;
this.owner = true;
this.trying = false;
const { handler, resolve } = this.queue.shift();
handler(this).finally(() => {
const { handler, resolve, timeout } = this.queue.shift();

const endWork = () => {
Comment thread
anton-iskryzhytskyi marked this conversation as resolved.
Outdated
this.leave();
resolve();
});
};

if (timeout) {
setTimeout(endWork, timeout);
}
handler(this).finally(endWork);
Comment thread
anton-iskryzhytskyi marked this conversation as resolved.
Outdated
}

leave() {
Expand Down Expand Up @@ -89,7 +95,7 @@ class LockManager {
handler = options;
options = {};
}
const { mode = 'exclusive', signal = null } = options;
const { mode = 'exclusive', signal = null, timeout } = options;

let lock = this.collection.get(name);
if (!lock) {
Expand All @@ -100,7 +106,7 @@ class LockManager {
locks.send(message);
}

const finished = lock.enter(handler);
const finished = lock.enter(handler, timeout);
let aborted = null;
if (signal) {
aborted = new Promise((resolve, reject) => {
Expand Down