Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Locking timeout #24

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion test/lock-time.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const assert = require('assert').strict;
const { locks } = require('..');
const { sleep } = require('./test-utils');
anton-iskryzhytskyi marked this conversation as resolved.
Show resolved Hide resolved

const TIME_TO_PROCESS = 2000;
const TIME_TO_PROCESS = 200;
const TIME_TO_LOCK = 100;

module.exports = async () => {
Expand Down
21 changes: 10 additions & 11 deletions web-locks.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,20 @@ const UNLOCKED = 1;
let locks = null; // LockManager instance

class Lock {
constructor({ name, mode = 'exclusive', buffer = null, timeout = null }) {
constructor(name, mode = 'exclusive', buffer = null) {
this.name = name;
this.mode = mode; // 'exclusive' or 'shared'
this.queue = [];
this.owner = false;
this.trying = false;
this.timeout = timeout;
this.buffer = buffer ? buffer : new SharedArrayBuffer(4);
this.flag = new Int32Array(this.buffer, 0, 1);
if (!buffer) Atomics.store(this.flag, 0, UNLOCKED);
}

enter(handler) {
enter(handler, timeout) {
Copy link
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.

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 @@ -39,15 +38,15 @@ class Lock {
if (prev === LOCKED) return;
this.owner = true;
this.trying = false;
const { handler, resolve } = this.queue.shift();
const { handler, resolve, timeout } = this.queue.shift();

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

if (typeof this.timeout === 'number') {
setTimeout(endWork, this.timeout);
if (timeout) {
setTimeout(endWork, timeout);
}
handler(this).finally(endWork);
anton-iskryzhytskyi marked this conversation as resolved.
Show resolved Hide resolved
}
Expand Down Expand Up @@ -100,14 +99,14 @@ class LockManager {

let lock = this.collection.get(name);
if (!lock) {
lock = new Lock({ name, mode, timeout });
lock = new Lock(name, mode);
this.collection.set(name, lock);
const { buffer } = lock;
const message = { webLocks: true, kind: 'create', name, mode, buffer };
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 Expand Up @@ -154,9 +153,9 @@ class LockManager {

receive(message) {
if (!message.webLocks) return;
const { kind, name, mode, buffer, timeout } = message;
const { kind, name, mode, buffer } = message;
if (kind === 'create') {
const lock = new Lock({ name, mode, buffer, timeout });
const lock = new Lock(name, mode, buffer);
this.collection.set(name, lock);
return;
}
Expand Down