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,19 +80,7 @@ function handleCopyTo (logger) {
return next(err);
}
let responseEnded = false;
res
.on('error', err => {
metrics.end(null, err);
pgstream.unpipe(res);
done();
return next(err);
})
.on('close', () => {
if (!responseEnded) {
streamCopy.setConnectionClosedByClient(true);
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;
@ -104,10 +92,17 @@ function handleCopyTo (logger) {
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,7 +146,6 @@ 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;
@ -160,7 +153,6 @@ function handleCopyFrom (logger) {
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);
}
})
.on('end', () => {
done();
@ -52,8 +50,4 @@ module.exports = class StreamCopy {
cb(null, pgstream, copyFromStream, client, done);
});
}
setConnectionClosedByClient(connectionClosedByClient) {
this.connectionClosedByClient = connectionClosedByClient;
}
};