Когда я запускаю «lsof | grep node» на моем сервере (на котором запущено приложение node.js), я получаю около 1000+ строк (db-соединения с портом 9160).Каждая строка выглядит следующим образом:
node 17006 root 160u IPv4 1362100969 0t0 TCP localhost:47813->localhost:9160 (ESTABLISHED)
Это тестовый сервер node.js, выполняющий что-то очень простое.(регистрация запроса к базе данных Cassandra с модулем Helenus)
Я был удивлен, что было так много открытых соединений, когда на данный момент определенно должно быть не более 1-2 соединений.
Значит ли это, что я неправильно завершаю соединения с БД в приложении Node?Мой код ниже.Спасибо.
var express = require('express')
, routes = require('./routes')
, app = express.createServer();
app.configure(function(){
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
});
process.on('uncaughtException', function (err) {
logger.error('Caught exception: ' + err);
});
function respond_test(req, res, next) {
var q = JSON.parse(req.query.q);
insert_db(q);
res.send('OK');
}
function insert_db(q) {
var helenus = require('helenus'),
pool = new helenus.ConnectionPool({
hosts : ['localhost:9160'],
keyspace : 'Test',
timeout : 3000
});
pool.on('error', function(err){
logger.error(err.name, err.message);
});
//makes a connection to the pool, this will return once there is at least one
//valid connection, other connections may still be pending
pool.connect(function(err, keyspace){
if(err){ throw(err); }
keyspace.get('Test', function(err, cf){
if(err){ throw(err); }
cf.insert(Date.now(), q, function(err){
if(err){ throw(err); }
});
});
});
pool.close();
}
app.get('/test', respond_test);
app.listen(80);