Make the SET statement_timeout async

This commit is contained in:
Rafa de la Torre 2018-07-20 16:24:33 +02:00
parent fdab0136e6
commit cc348f3725

View File

@ -33,29 +33,34 @@ module.exports = class StreamCopy {
return cb(err); return cb(err);
} }
client.query('SET statement_timeout=' + this.timeout); client.query('SET statement_timeout=' + this.timeout, (err) => {
let streamMaker = action === ACTION_TO ? copyTo : copyFrom; if (err) {
this.stream = streamMaker(this.sql); return cb(err);
const pgstream = client.query(this.stream); }
pgstream let streamMaker = action === ACTION_TO ? copyTo : copyFrom;
.on('end', () => done()) this.stream = streamMaker(this.sql);
.on('error', err => done(err)) const pgstream = client.query(this.stream);
.on('cancelQuery', err => {
if(action === ACTION_TO) {
// See https://www.postgresql.org/docs/9.5/static/protocol-flow.html#PROTOCOL-COPY
const cancelingClient = new Client(client.connectionParameters);
cancelingClient.cancel(client, pgstream);
// see https://node-postgres.com/api/pool#releasecallback pgstream
done(err); .on('end', () => done())
} else if (action === ACTION_FROM) { .on('error', err => done(err))
client.connection.sendCopyFail('CARTO SQL API: Connection closed by client'); .on('cancelQuery', err => {
} if(action === ACTION_TO) {
}); // See https://www.postgresql.org/docs/9.5/static/protocol-flow.html#PROTOCOL-COPY
const cancelingClient = new Client(client.connectionParameters);
cancelingClient.cancel(client, pgstream);
cb(null, pgstream); // see https://node-postgres.com/api/pool#releasecallback
done(err);
} else if (action === ACTION_FROM) {
client.connection.sendCopyFail('CARTO SQL API: Connection closed by client');
}
});
cb(null, pgstream);
});
}); });
} }