Use request 'close' event when the request is closed unexpectedly by the client
This commit is contained in:
parent
37a079363a
commit
508d688b9d
@ -80,19 +80,7 @@ function handleCopyTo (logger) {
|
|||||||
return next(err);
|
return next(err);
|
||||||
}
|
}
|
||||||
|
|
||||||
let responseEnded = false;
|
req.on('close', () => {
|
||||||
|
|
||||||
res
|
|
||||||
.on('error', err => {
|
|
||||||
metrics.end(null, err);
|
|
||||||
pgstream.unpipe(res);
|
|
||||||
done();
|
|
||||||
return next(err);
|
|
||||||
})
|
|
||||||
.on('close', () => {
|
|
||||||
if (!responseEnded) {
|
|
||||||
streamCopy.setConnectionClosedByClient(true);
|
|
||||||
|
|
||||||
// Cancel the running COPY TO query
|
// Cancel the running COPY TO query
|
||||||
// See https://www.postgresql.org/docs/9.5/static/protocol-flow.html#PROTOCOL-COPY
|
// See https://www.postgresql.org/docs/9.5/static/protocol-flow.html#PROTOCOL-COPY
|
||||||
const runningClient = client;
|
const runningClient = client;
|
||||||
@ -104,10 +92,17 @@ function handleCopyTo (logger) {
|
|||||||
pgstream.unpipe(res);
|
pgstream.unpipe(res);
|
||||||
// see https://node-postgres.com/api/pool#releasecallback
|
// see https://node-postgres.com/api/pool#releasecallback
|
||||||
done(err);
|
done(err);
|
||||||
|
|
||||||
return next(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) => {
|
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,7 +146,6 @@ 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;
|
||||||
@ -160,7 +153,6 @@ function handleCopyFrom (logger) {
|
|||||||
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);
|
||||||
|
@ -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;
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user