Merge pull request #54 from spara/connection_pooling
added connection pooling to postgres to prevent client timeouts
This commit is contained in:
commit
feacab96f6
@ -15,6 +15,7 @@
|
|||||||
"method-override": "^2.3.5",
|
"method-override": "^2.3.5",
|
||||||
"async": "^1.5.0",
|
"async": "^1.5.0",
|
||||||
"pg": "^4.4.3",
|
"pg": "^4.4.3",
|
||||||
|
"pg-pool": "1.6.0",
|
||||||
"socket.io": "^1.3.7"
|
"socket.io": "^1.3.7"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,13 +1,25 @@
|
|||||||
var express = require('express'),
|
var express = require('express'),
|
||||||
async = require('async'),
|
async = require('async'),
|
||||||
pg = require("pg"),
|
pg = require("pg"),
|
||||||
|
Pool = require("pg-pool"),
|
||||||
cookieParser = require('cookie-parser'),
|
cookieParser = require('cookie-parser'),
|
||||||
bodyParser = require('body-parser'),
|
bodyParser = require('body-parser'),
|
||||||
methodOverride = require('method-override'),
|
methodOverride = require('method-override'),
|
||||||
app = express(),
|
app = express(),
|
||||||
server = require('http').Server(app),
|
server = require('http').Server(app),
|
||||||
io = require('socket.io')(server);
|
io = require('socket.io')(server);
|
||||||
|
|
||||||
|
var config = {
|
||||||
|
host: 'localhost',
|
||||||
|
user: 'postgres',
|
||||||
|
password: '',
|
||||||
|
database: 'postgres',
|
||||||
|
max: 10, // max number of clients in pool
|
||||||
|
idleTimeoutMillis: 1000, // close & remove clients which have been idle > 1 second
|
||||||
|
};
|
||||||
|
|
||||||
|
var client = new Pool(config)
|
||||||
|
|
||||||
io.set('transports', ['polling']);
|
io.set('transports', ['polling']);
|
||||||
|
|
||||||
var port = process.env.PORT || 4000;
|
var port = process.env.PORT || 4000;
|
||||||
@ -23,14 +35,6 @@ io.sockets.on('connection', function (socket) {
|
|||||||
|
|
||||||
async.retry(
|
async.retry(
|
||||||
{times: 1000, interval: 1000},
|
{times: 1000, interval: 1000},
|
||||||
function(callback) {
|
|
||||||
pg.connect('postgres://postgres@db/postgres', function(err, client, done) {
|
|
||||||
if (err) {
|
|
||||||
console.error("Waiting for db");
|
|
||||||
}
|
|
||||||
callback(err, client);
|
|
||||||
});
|
|
||||||
},
|
|
||||||
function(err, client) {
|
function(err, client) {
|
||||||
if (err) {
|
if (err) {
|
||||||
return console.err("Giving up");
|
return console.err("Giving up");
|
||||||
@ -45,24 +49,13 @@ function getVotes(client) {
|
|||||||
if (err) {
|
if (err) {
|
||||||
console.error("Error performing query: " + err);
|
console.error("Error performing query: " + err);
|
||||||
} else {
|
} else {
|
||||||
var votes = collectVotesFromResult(result);
|
var votes = result.rows[0].count
|
||||||
io.sockets.emit("scores", JSON.stringify(votes));
|
io.sockets.emit("scores", JSON.stringify(votes));
|
||||||
}
|
}
|
||||||
|
|
||||||
setTimeout(function() {getVotes(client) }, 1000);
|
setTimeout(function() {getVotes(client) }, 1000);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function collectVotesFromResult(result) {
|
|
||||||
var votes = {a: 0, b: 0};
|
|
||||||
|
|
||||||
result.rows.forEach(function (row) {
|
|
||||||
votes[row.vote] = parseInt(row.count);
|
|
||||||
});
|
|
||||||
|
|
||||||
return votes;
|
|
||||||
}
|
|
||||||
|
|
||||||
app.use(cookieParser());
|
app.use(cookieParser());
|
||||||
app.use(bodyParser());
|
app.use(bodyParser());
|
||||||
app.use(methodOverride('X-HTTP-Method-Override'));
|
app.use(methodOverride('X-HTTP-Method-Override'));
|
||||||
|
Loading…
Reference in New Issue
Block a user