Skip to content

Commit 8a91522

Browse files
committed
refactor gridstore changes
1 parent 31130ea commit 8a91522

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

lib/attachments/gridstore-storage.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -231,8 +231,13 @@ class GridstoreStorage {
231231
return done();
232232
}
233233

234+
if (attachment.body.length < 255 * 1024) {
235+
// a single chunk attachment, no need for locking
236+
return done();
237+
}
238+
234239
// Try to get a lock
235-
// Using locks is required to prevent multiple messages storing the same attachment at
240+
// Using locks is required to prevent multiple messages storing the same large attachment at
236241
// the same time.
237242
// NB! Setting lock ttl too high has a downside that restarting the process would still keep
238243
// the lock and thus anyone trying to store the message would have to wait
@@ -265,8 +270,8 @@ class GridstoreStorage {
265270
return;
266271
}
267272
// most probably a race condition, try again
268-
if (tryCount++ < 5) {
269-
if (/\.chunks /.test(err.message)) {
273+
if (tryCount++ < 20) {
274+
if (err.code === 11000 || /\.chunks /.test(err.message)) {
270275
// Partial chunks detected. Might be because of:
271276
// * another process is inserting the same attachment and thus no "files" entry yet (should not happened though due to locking)
272277
// * previously deleted attachment that has not been properly removed
@@ -294,8 +299,8 @@ class GridstoreStorage {
294299

295300
// Check how old is the previous chunk
296301
let timestamp = data._id.getTimestamp();
297-
if (timestamp && typeof timestamp.getTime === 'function' && timestamp.getTime() >= Date.now() - 5 * 60 * 1000) {
298-
// chunk is newer than 5 minutes, assume race condition and try again after a while
302+
if (timestamp && typeof timestamp.getTime === 'function' && timestamp.getTime() >= Date.now() - 30 * 60 * 1000) {
303+
// chunk is newer than 30 minutes, assume race condition and try again after a while
299304
return setTimeout(tryStore, 300 + 200 * Math.random());
300305
}
301306

0 commit comments

Comments
 (0)