Skip to content

Commit 6cac896

Browse files
Use in-memory lock. (#1475)
1 parent 654b95c commit 6cac896

File tree

5 files changed

+16
-49
lines changed

5 files changed

+16
-49
lines changed

lib/handlers/patch.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ async function patchHandler (req, res, next) {
6464
}
6565

6666
// Patch the graph and write it back to the file
67-
const result = await withLock(path, { mustExist: false }, async () => {
67+
const result = await withLock(path, async () => {
6868
const graph = await readGraph(resource)
6969
await applyPatch(patchObject, graph, url)
7070
return writeGraph(graph, resource, ldp.resourceMapper.resolveFilePath(req.hostname), ldp.serverUri)

lib/ldp.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ class LDP {
251251
}
252252

253253
// Directory created, now write the file
254-
return withLock(path, { mustExist: false }, () => new Promise((resolve, reject) => {
254+
return withLock(path, () => new Promise((resolve, reject) => {
255255
// HACK: the middleware in webid-oidc.js uses body-parser, thus ending the stream of data
256256
// for JSON bodies. So, the stream needs to be reset
257257
if (contentType.includes('application/json')) {
@@ -445,15 +445,15 @@ class LDP {
445445
const linkPath = this.resourceMapper._removeDollarExtension(path)
446446
try {
447447
// first delete file, then links with write permission only (atomic delete)
448-
await withLock(path, { mustExist: false }, () => promisify(fs.unlink)(path))
448+
await withLock(path, () => promisify(fs.unlink)(path))
449449

450450
const aclPath = `${linkPath}${this.suffixAcl}`
451451
if (await promisify(fs.exists)(aclPath)) {
452-
await withLock(aclPath, { mustExist: false }, () => promisify(fs.unlink)(aclPath))
452+
await withLock(aclPath, () => promisify(fs.unlink)(aclPath))
453453
}
454454
const metaPath = `${linkPath}${this.suffixMeta}`
455455
if (await promisify(fs.exists)(metaPath)) {
456-
await withLock(metaPath, { mustExist: false }, () => promisify(fs.unlink)(metaPath))
456+
await withLock(metaPath, () => promisify(fs.unlink)(metaPath))
457457
}
458458
} catch (err) {
459459
debug.container('DELETE -- unlink() error: ' + err)

lib/lock.js

Lines changed: 5 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,10 @@
1-
/* eslint-disable no-async-promise-executor */
1+
const AsyncLock = require('async-lock')
22

3-
const { lock } = require('proper-lockfile')
3+
const lock = new AsyncLock({ timeout: 30 * 1000 })
44

5-
const staleSeconds = 30
6-
7-
// Obtains a lock on the path, and maintains it until the callback finishes
8-
function withLock (path, options = {}, callback = options) {
9-
return new Promise(async (resolve, reject) => {
10-
let releaseLock, result
11-
try {
12-
// Obtain the lock
13-
releaseLock = await lock(path, {
14-
retries: 10,
15-
update: 1000,
16-
stale: staleSeconds * 1000,
17-
realpath: !!options.mustExist,
18-
onCompromised: () =>
19-
reject(new Error(`The file at ${path} was not updated within ${staleSeconds}s.`))
20-
})
21-
// Hold on to the lock until the callback's returned promise resolves
22-
result = await callback()
23-
} catch (error) {
24-
reject(error)
25-
// Ensure the lock is always released
26-
} finally {
27-
await releaseLock()
28-
}
29-
resolve(result)
30-
})
5+
// Obtains a lock on the path, and maintains it until the task finishes
6+
async function withLock (path, executeTask) {
7+
return await lock.acquire(path, executeTask)
318
}
329

3310
module.exports = withLock

package-lock.json

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

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
"dependencies": {
5858
"@solid/acl-check": "^0.3.0",
5959
"@solid/oidc-auth-manager": "^0.22.0",
60+
"async-lock": "^1.2.4",
6061
"body-parser": "^1.19.0",
6162
"bootstrap": "^3.4.1",
6263
"busboy": "^0.3.1",
@@ -92,7 +93,6 @@
9293
"nodemailer": "^6.4.11",
9394
"oidc-op-express": "^0.0.3",
9495
"owasp-password-strength-test": "^1.3.0",
95-
"proper-lockfile": "^4.1.1",
9696
"rdflib": "^1.3.1",
9797
"recursive-readdir": "^2.2.2",
9898
"rimraf": "^3.0.2",

0 commit comments

Comments
 (0)