Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

db.stream(): add timeout #566

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion lib/util/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,23 @@ const queryFuncs = (db, obj) => {
for (let i = 0; i < xs.length; i += 1) result[i] = f(xs[i]);
return result;
};
obj.stream = (s) => new Promise((resolve, reject1) => db.stream(s, resolve).catch(reject1));
obj.stream = (s) => new Promise((resolve, reject1) => db.stream(s, stream => {
const streamTimeout = setTimeout(() => {
// TODO should probably be stream.destroy(new Error('Stream timed out')), but
// stream.destroy(error) may currently throw other issues:
// * slonik with #347 unpatched: terminates process with: Emitted 'error' event on Transform instance at
// * slonik with #347 patched: there are intermittent issues with this too TODO add link to the underlying pg issue
// see: https://github.com/gajus/slonik/issues/347
stream.destroy();
}, 30000); // REVIEW this was picked arbitrarily, but seems to work ok in testing. It should probably be a bigger number than HTTP request timeout etc; it might be important to check production response times for successful, long-lived requests and make this timeout bigger than those

stream.on('close', () => {
clearTimeout(streamTimeout);
});

resolve(stream);
}).catch(reject1));

obj.stream.map = (f) => (strm) => PartialPipe.of(strm, mapStream(({ row }) => f(row)));
/* eslint-enable no-param-reassign */
};
Expand Down