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);
}
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
.on('error', err => {
metrics.end(null, err);
pgstream.unpipe(res);
done();
return next(err);
})
.on('close', () => {
if (!responseEnded) {
streamCopy.setConnectionClosedByClient(true);
const err = new Error('Connection closed by client');
metrics.end(null, err);
pgstream.unpipe(res);
// see https://node-postgres.com/api/pool#releasecallback
done(err);
// 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);
return next(err);
});
const err = new Error('Connection closed by client');
metrics.end(null, err);
pgstream.unpipe(res);
// see https://node-postgres.com/api/pool#releasecallback
done(err);
return next(err);
}
})
.on('end', () => responseEnded = true);
res.on('error', err => {
metrics.end(null, err);
pgstream.unpipe(res);
done();
return next(err);
});
pgstream.on('error', (err) => {
metrics.end(null, err);
@ -141,7 +136,6 @@ function handleCopyFrom (logger) {
return next(err);
}
let requestEnded = false;
req
.on('error', err => {
metrics.end(null, err);
@ -152,15 +146,13 @@ function handleCopyFrom (logger) {
next(err);
})
.on('close', () => {
if (!requestEnded) {
const err = new Error('Connection closed by client');
metrics.end(null, err);
const connection = client.connection;
connection.sendCopyFail('CARTO SQL API: Connection closed by client');
req.unpipe(pgstream);
done();
next(err);
}
const err = new Error('Connection closed by client');
metrics.end(null, err);
const connection = client.connection;
connection.sendCopyFail('CARTO SQL API: Connection closed by client');
req.unpipe(pgstream);
done();
next(err);
})
.on('data', data => {
if (isGzip) {
@ -168,8 +160,7 @@ function handleCopyFrom (logger) {
} else {
metrics.addSize(data.length);
}
})
.on('end', () => requestEnded = true);
});
pgstream.on('error', (err) => {
metrics.end(null, err);

View File

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