Skip to content

Commit

Permalink
refactor code. Fix possible race condition
Browse files Browse the repository at this point in the history
  • Loading branch information
NickOvt committed Sep 23, 2024
1 parent daa9767 commit d195fb9
Showing 1 changed file with 34 additions and 20 deletions.
54 changes: 34 additions & 20 deletions lib/attachments/gridstore-storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,23 @@ class GridstoreStorage {
});
}

updateFileWithContentHashMetadata(args, callback, hash, calculatedFileContentHash) {
this.gridfs.collection(this.bucketName + '.files').findOneAndUpdate(
{
_id: hash
},
{
$set: {
'metadata.fileContentHash': calculatedFileContentHash
}
},
{
returnDocument: 'after'
},
() => callback(...args) // do not really care about error here. If error then highly likely the file has not been uploaded either
);
}

async get(attachmentId) {
let attachmentData = await this.gridfs.collection(this.bucketName + '.files').findOne({
_id: attachmentId
Expand Down Expand Up @@ -131,6 +148,12 @@ class GridstoreStorage {

let attachmentCallback = (...args) => {
// store finished uploading, add the hash of the file contents to file metadata
let calculatedFileContentHash;

if (args.length > 2) {
calculatedFileContentHash = args[2];
}

if (storeLock) {
log.silly('GridStore', '[%s] UNLOCK lock=%s status=%s', instance, lockId, storeLock.success ? 'locked' : 'empty');
if (storeLock.success) {
Expand All @@ -139,26 +162,11 @@ class GridstoreStorage {
// might be already finished if retrying after delay
return;
}
if (args.length > 2) {
const calculatedFileContentHash = args[2];

if (calculatedFileContentHash) {
this.gridfs.collection(this.bucketName + '.files').findOneAndUpdate(
{
_id: hash
},
{
$set: {
'metadata.fileContentHash': calculatedFileContentHash
}
},
{
returnDocument: 'after'
}
);
}
if (calculatedFileContentHash) {
// locked upload, new file
this.updateFileWithContentHashMetadata(args, callback, hash, calculatedFileContentHash);
return; // return from attachmentCallback. Top level callback will be ran after hash update
}
callback(...args);
});
// unset variable to prevent double releasing
storeLock = false;
Expand All @@ -170,6 +178,11 @@ class GridstoreStorage {
// might be already finished if retrying after delay
return;
}
if (calculatedFileContentHash) {
// no lock upload, new file
this.updateFileWithContentHashMetadata(args, callback, hash, calculatedFileContentHash);
return; // return from attachmentCallback. Top level callback will be ran after hash update
}
callback(...args);
};

Expand Down Expand Up @@ -308,7 +321,8 @@ class GridstoreStorage {
store.once('finish', () => attachmentCallback(null, id, fileHashCalculator.hash));

if (!metadata.decoded) {
store.end(attachment.body);
fileHashCalculator.pipe(store);
fileHashCalculator.end(attachment.body);
} else {
let decoder = new libbase64.Decoder();
decoder.pipe(fileHashCalculator).pipe(store);
Expand Down

0 comments on commit d195fb9

Please sign in to comment.