I try read records from db and send back to client as ajax request's response. Here my test program:
Child process(test.js):
const zlib = require('zlib');
const t = require('through2');
const QueryStream = require('pg-query-stream');
const JSONStream = require('JSONStream');
const {
Pool
} = require('pg');
const config = require('./modules/config');
const pool = new Pool(config.get('db'));
(async function () {
const conn = await pool.connect();
var query = new QueryStream('SELECT * FROM teszt001.records ORDER BY rec_time ASC LIMIT 500', []);
let first = true;
var stream = conn.query(query);
stream.on('end', () => {
conn.release();
});
stream.pipe(t.obj(
function (chunk, enc, cb) {
if (first) {
first = false;
this.push(Object.keys(chunk));
}
this.push(Object.values(chunk));
cb();
}
))
.pipe(JSONStream.stringify()).pipe(zlib.createGzip()).pipe(process.stdout);
})();
Main process:
const express = require('express');
const app = express();
const {
fork
} = require('child_process');
const common = require('./modules/common');
app.get('/', (req, res) => {
let view;
if (common.identity(req).level == '1' || common.identity(req).level == '2') {
view = 'adm';
} else {
view = common.identity(req).username;
}
res.set('Content-Encoding', 'gzip');
res.set('Content-Type', 'text/plain');
let queryFilter = {};
let child = fork('./test.js', [req.session.user.schema, JSON.stringify(queryFilter), view], {
stdio: ['ignore', 'pipe', 'inherit', 'ipc']
});
child.stdout.pipe(res);
});
server.listen(3005, () => {
console.log(`Server started!`);
});
module.exports = server;
When all the data goes through to the main process, the end event takes about 20 seconds. I did some wrong or is it a bug?