node.js - Koa server crashing when returning too many data -
i developed light node web server koa. contain 3 services retrieve data mongo database, return them client.
the object returned array of json objects. can contain until 750.000 objects. 1 object looks following :
{"_id":"58a4779b783dbfa853a93e09","attributes":[{"key":"nb_mots","value":"16"},{"key":"fonction","value":"président"},{"key":"groupe_acronyme","value":"ump"}],"datatype":"deputes","date":"2007-10-23t00:00:00.000z","eventtype":"loi","actor":"bernard accoyer"}
my server runs under linux (debian, guess...).
my problem is: when want retrieve whole dataset (so 750.000 objects - using "allevents" service given below), server fails. message "killed". survey state of server using command "top". of course, %cpu goes high (between 90 & 95), %mem, nothing critical (moreover same if try retrieve less data).
any idea fix that?
code of server :
var kr = require('koa-route'); var koa = require('koa'); var app = koa(); var _ = require('lodash'); var mongoclient = require("mongodb").mongoclient; var someeventspromise = function(datatype, number){ return new promise(function(resolve, reject){ mongoclient.connect("mongodb://localhost/eventdata", function(error, db) { if (error){ return reject(error); } console.log("connecté à la base de données"); db.collection('events').find({'datatype' : datatype},{"limit": number ? number : 1000}).toarray(function(err, array) { console.log('returning %d objects', array.length); db.close(); return resolve({eventsarray : array}); }); }); }); } var someevents = function * (datatype, number) { var event = yield someeventspromise(datatype, parseint(number)); this.body = event; } var alleventspromise = function(datatype){ return new promise(function(resolve, reject){ mongoclient.connect("mongodb://localhost/eventdata", function(error, db) { if (error){ return reject(error); } console.log("connecté à la base de données"); db.collection('events').find({'datatype' : datatype}).toarray(function(err, array) { console.log('returning %d objects', array.length); db.close(); return resolve({eventsarray : array}); }); }); }); } var allevents = function * (datatype) { var event = yield alleventspromise(datatype); this.body = event; } var minmaxdatespromise = function (datatype) { return new promise(function(resolve, reject){ mongoclient.connect("mongodb://localhost/eventdata", function(error, db) { if (error){ return reject(error); } console.log("connecté à la base de données"); db.collection('events').find({'datatype' : datatype}, {date : 1}).toarray(function(err, array) { console.log('returning %d objects', array.length); db.close(); let g = _.map(_.uniqby(array, function(d) {return d.date}), function(d) { return new date(d.date);}); console.log('mapping %d objects', g.length); g.sort(function(a,b) { return b - ; }); let minmax = { maxdate : g[0], mindate : g[g.length - 1]}; return resolve(minmax); }); }); }); } var minmaxdates = function * (datatype) { var dates = yield minmaxdatespromise(datatype) this.body = dates; } app.use(kr.get('/someevents/:datatype/:number', someevents)); app.use(kr.get('/allevents/:datatype', allevents)); app.use(kr.get('/minmaxdates/:datatype', minmaxdates)); app.listen(3010);
as i'm pretty sure issue related fact have many data retrieve, tried send them through stream (instead of storing them in big array).
no luck, have issue :
error: write epipe @ exports._errnoexception (util.js:1023:11) @ writewrap.afterwrite [as oncomplete] (net.js:804:14)
my code following :
var kr = require('koa-route'); var koa = require('koa'); var app = koa(); var _ = require('lodash'); var mongoclient = require('mongodb').mongoclient; var allevents = function * (datatype) { var ctx = this; var db = yield mongoclient.connect('mongodb://localhost/eventdata'); console.log("querying data " + datatype); // collection var col = db.collection('events'); ctx.body = col.find({'datatype' : datatype}).stream( { transform: function(doc) { return json.stringify(doc); } }); } app.use(kr.get('/allevents/:datatype', allevents)); app.listen(3010);
last things : results used in c# unity script (just in case).
thanks in advance.
btw: can't retrieve less data, need of them.
Comments
Post a Comment