Skip to content

Commit

Permalink
fix: deduplicate move operations
Browse files Browse the repository at this point in the history
  • Loading branch information
wraithgar committed May 13, 2023
1 parent de12400 commit 9ce43b8
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
14 changes: 11 additions & 3 deletions lib/content/write.js
Expand Up @@ -15,6 +15,9 @@ const fsm = require('fs-minipass')

module.exports = write

// Cache of move operations in process so we don't duplicate
const moveOperations = {}

async function write (cache, data, opts = {}) {
const { algorithms, size, integrity } = opts

Expand Down Expand Up @@ -159,15 +162,20 @@ async function makeTmp (cache, opts) {
async function moveToDestination (tmp, cache, sri, opts) {
const destination = contentPath(cache, sri)
const destDir = path.dirname(destination)

await fs.mkdir(destDir, { recursive: true })
if (moveOperations[destination]) {
return moveOperations[destination]
}
moveOperations[destination] = fs.mkdir(destDir, { recursive: true })
.then(() => moveFile(tmp.target, destination, { overwrite: false }))
try {
await moveFile(tmp.target, destination, { overwrite: false })
await moveOperations[destination]
tmp.moved = true
} catch (err) {
if (!err.message.startsWith('The destination file exists')) {
throw Object.assign(err, { code: 'EEXIST' })
}
} finally {
delete moveOperations[destination]
}
}

Expand Down
14 changes: 14 additions & 0 deletions test/put.js
Expand Up @@ -128,3 +128,17 @@ t.test('signals error if error writing to cache', async t => {
t.equal(bulkErr.code, 'EBADSIZE', 'got error from bulk write')
t.equal(streamErr.code, 'EBADSIZE', 'got error from stream write')
})

t.test('concurrent puts', async t => {
const CACHE = t.testdir()
await Promise.all([
put(CACHE, KEY, CONTENT),
put(CACHE, KEY, CONTENT),
put(CACHE, KEY, CONTENT),
put(CACHE, KEY, CONTENT),
put(CACHE, KEY, CONTENT),
put(CACHE, KEY, CONTENT),
put(CACHE, KEY, CONTENT),
])
t.ok('No errors')
})

0 comments on commit 9ce43b8

Please sign in to comment.