Skip to content

Commit 77bcb50

Browse files
committed
ImportEtherpad: Limit in-flight DB queries
1 parent 5b3575a commit 77bcb50

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

src/node/utils/ImportEtherpad.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
const AttributePool = require('../../static/js/AttributePool');
2020
const {Pad} = require('../db/Pad');
21+
const async = require('async');
2122
const authorManager = require('../db/AuthorManager');
2223
const db = require('../db/DB');
2324
const hooks = require('../../static/js/pluginfw/hooks');
@@ -47,12 +48,16 @@ exports.setPadRaw = async (padId, r) => {
4748
if (originalPadId !== padId) throw new Error('unexpected pad ID in record');
4849
};
4950

51+
// Limit the number of in-flight database queries so that the queries do not time out when
52+
// importing really large files.
53+
const q = async.queue(async (task) => await task(), 100);
54+
5055
// First validate and transform values. Do not commit any records to the database yet in case
5156
// there is a problem with the data.
5257

5358
const dbRecords = new Map();
5459
const existingAuthors = new Set();
55-
await Promise.all(Object.entries(records).map(async ([key, value]) => {
60+
await Promise.all(Object.entries(records).map(([key, value]) => q.pushAsync(async () => {
5661
if (!value) {
5762
return;
5863
}
@@ -91,7 +96,7 @@ exports.setPadRaw = async (padId, r) => {
9196
return;
9297
}
9398
dbRecords.set(key, value);
94-
}));
99+
})));
95100

96101
const pad = new Pad(padId, {
97102
// Only fetchers are needed to check the pad's integrity.
@@ -109,7 +114,7 @@ exports.setPadRaw = async (padId, r) => {
109114
await pad.check();
110115

111116
await Promise.all([
112-
...[...dbRecords].map(async ([k, v]) => await db.set(k, v)),
113-
...[...existingAuthors].map(async (authorId) => await authorManager.addPad(authorId, padId)),
117+
...[...dbRecords].map(([k, v]) => q.pushAsync(() => db.set(k, v))),
118+
...[...existingAuthors].map((a) => q.pushAsync(() => authorManager.addPad(a, padId))),
114119
]);
115120
};

0 commit comments

Comments
 (0)