Skip to content

Commit 3193a32

Browse files
committed
Add lock on put.
1 parent 4e6eb10 commit 3193a32

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

lib/ldp.js

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -255,16 +255,24 @@ class LDP {
255255
}
256256

257257
// Directory created, now write the file
258-
return await new Promise((resolve, reject) => {
259-
const file = stream.pipe(fs.createWriteStream(path))
260-
file.on('error', function () {
261-
reject(error(500, 'Error writing data'))
262-
})
263-
file.on('finish', function () {
264-
debug.handlers('PUT -- Wrote data to: ' + path)
265-
resolve()
258+
let releaseLock
259+
try {
260+
releaseLock = await lock(path, { retries: 10, realpath: false })
261+
return await new Promise((resolve, reject) => {
262+
const file = stream.pipe(fs.createWriteStream(path))
263+
file.on('error', function () {
264+
reject(error(500, 'Error writing data'))
265+
})
266+
file.on('finish', function () {
267+
debug.handlers('PUT -- Wrote data to: ' + path)
268+
resolve()
269+
})
266270
})
267-
})
271+
} finally {
272+
if (releaseLock) {
273+
await releaseLock()
274+
}
275+
}
268276
}
269277

270278
async exists (hostname, path, searchIndex = true) {

0 commit comments

Comments
 (0)