-
-
Notifications
You must be signed in to change notification settings - Fork 13
Locking timeout #24
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
base: master
Are you sure you want to change the base?
Locking timeout #24
Changes from 2 commits
b46ab15
07c7387
f9099d4
364e6b7
ed212e4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,6 +8,7 @@ const tests = [ | |
| 'deadlock', | ||
| 'recursive-deadlock', | ||
| 'thread-main', | ||
| 'lock-time', | ||
| ]; | ||
|
|
||
| (async () => { | ||
|
|
||
| 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'); | ||
|
|
||
| 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); | ||
| }; | ||
| 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, | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,9 +22,9 @@ class Lock { | |
| if (!buffer) Atomics.store(this.flag, 0, UNLOCKED); | ||
| } | ||
|
|
||
| enter(handler) { | ||
| enter(handler, timeout) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Hmm, maybe it would be better to throw an error on timeout, similar to abort behavior? |
||
| return new Promise(resolve => { | ||
| this.queue.push({ handler, resolve }); | ||
| this.queue.push({ handler, resolve, timeout }); | ||
| this.trying = true; | ||
| setTimeout(() => { | ||
| this.tryEnter(); | ||
|
|
@@ -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 = () => { | ||
|
anton-iskryzhytskyi marked this conversation as resolved.
Outdated
|
||
| this.leave(); | ||
| resolve(); | ||
| }); | ||
| }; | ||
|
|
||
| if (timeout) { | ||
| setTimeout(endWork, timeout); | ||
| } | ||
| handler(this).finally(endWork); | ||
|
anton-iskryzhytskyi marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| leave() { | ||
|
|
@@ -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) { | ||
|
|
@@ -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) => { | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.