Use request 'close' event when the request is closed unexpectedly by the client

This commit is contained in:
Daniel García Aubert 2018-06-14 00:16:06 +02:00
parent 37a079363a
commit 508d688b9d
2 changed files with 29 additions and 44 deletions

View File

@ -80,34 +80,29 @@ function handleCopyTo (logger) {
return next(err); return next(err);
} }
let responseEnded = false; req.on('close', () => {
// Cancel the running COPY TO query
// See https://www.postgresql.org/docs/9.5/static/protocol-flow.html#PROTOCOL-COPY
const runningClient = client;
const cancelingClient = new Client(runningClient.connectionParameters);
cancelingClient.cancel(runningClient, pgstream);
res const err = new Error('Connection closed by client');
.on('error', err => { metrics.end(null, err);
metrics.end(null, err); pgstream.unpipe(res);
pgstream.unpipe(res); // see https://node-postgres.com/api/pool#releasecallback
done(); done(err);
return next(err);
})
.on('close', () => {
if (!responseEnded) {
streamCopy.setConnectionClosedByClient(true);
// Cancel the running COPY TO query return next(err);
// See https://www.postgresql.org/docs/9.5/static/protocol-flow.html#PROTOCOL-COPY });
const runningClient = client;
const cancelingClient = new Client(runningClient.connectionParameters);
cancelingClient.cancel(runningClient, pgstream);
const err = new Error('Connection closed by client'); res.on('error', err => {
metrics.end(null, err); metrics.end(null, err);
pgstream.unpipe(res); pgstream.unpipe(res);
// see https://node-postgres.com/api/pool#releasecallback done();
done(err);
return next(err); return next(err);
} });
})
.on('end', () => responseEnded = true);
pgstream.on('error', (err) => { pgstream.on('error', (err) => {
metrics.end(null, err); metrics.end(null, err);
@ -141,7 +136,6 @@ function handleCopyFrom (logger) {
return next(err); return next(err);
} }
let requestEnded = false;
req req
.on('error', err => { .on('error', err => {
metrics.end(null, err); metrics.end(null, err);
@ -152,15 +146,13 @@ function handleCopyFrom (logger) {
next(err); next(err);
}) })
.on('close', () => { .on('close', () => {
if (!requestEnded) { const err = new Error('Connection closed by client');
const err = new Error('Connection closed by client'); metrics.end(null, err);
metrics.end(null, err); const connection = client.connection;
const connection = client.connection; connection.sendCopyFail('CARTO SQL API: Connection closed by client');
connection.sendCopyFail('CARTO SQL API: Connection closed by client'); req.unpipe(pgstream);
req.unpipe(pgstream); done();
done(); next(err);
next(err);
}
}) })
.on('data', data => { .on('data', data => {
if (isGzip) { if (isGzip) {
@ -168,8 +160,7 @@ function handleCopyFrom (logger) {
} else { } else {
metrics.addSize(data.length); metrics.addSize(data.length);
} }
}) });
.on('end', () => requestEnded = true);
pgstream.on('error', (err) => { pgstream.on('error', (err) => {
metrics.end(null, err); metrics.end(null, err);

View File

@ -20,9 +20,7 @@ module.exports = class StreamCopy {
pgstream pgstream
.on('error', err => { .on('error', err => {
if (!this.connectionClosedByClient) { done(err);
done(err);
}
}) })
.on('end', () => { .on('end', () => {
done(); done();
@ -52,8 +50,4 @@ module.exports = class StreamCopy {
cb(null, pgstream, copyFromStream, client, done); cb(null, pgstream, copyFromStream, client, done);
}); });
} }
setConnectionClosedByClient(connectionClosedByClient) {
this.connectionClosedByClient = connectionClosedByClient;
}
}; };