Skip to content

Commit

Permalink
fix: deduplicate move operations (#203)
Browse files Browse the repository at this point in the history
  • Loading branch information
wraithgar committed May 16, 2023
1 parent de12400 commit 988d77a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
17 changes: 14 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 = new Map()

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

Expand Down Expand Up @@ -159,15 +162,23 @@ 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.has(destination)) {
return moveOperations.get(destination)
}
moveOperations.set(
destination,
fs.mkdir(destDir, { recursive: true })
.then(() => moveFile(tmp.target, destination, { overwrite: false }))
)
try {
await moveFile(tmp.target, destination, { overwrite: false })
await moveOperations.get(destination)
tmp.moved = true
} catch (err) {
if (!err.message.startsWith('The destination file exists')) {
throw Object.assign(err, { code: 'EEXIST' })
}
} finally {
moveOperations.delete(destination)
}
}

Expand Down
16 changes: 16 additions & 0 deletions test/put.js
@@ -1,6 +1,7 @@
'use strict'

const fs = require('fs/promises')
const path = require('path')
const index = require('../lib/entry-index')
const memo = require('../lib/memoization')
const t = require('tap')
Expand Down Expand Up @@ -128,3 +129,18 @@ 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),
])
const tmpFiles = await fs.readdir(path.join(CACHE, 'tmp'))
t.strictSame(tmpFiles, [], 'Nothing left in tmp')
})

0 comments on commit 988d77a

Please sign in to comment.