Skip to content

Instantly share code, notes, and snippets.

@lerouxb
Last active August 29, 2015 14:22
Show Gist options
  • Select an option

  • Save lerouxb/7e6e806f3b90982a523b to your computer and use it in GitHub Desktop.

Select an option

Save lerouxb/7e6e806f3b90982a523b to your computer and use it in GitHub Desktop.
node-postgres weirdness
async = require 'async'
pg = require 'pg'
executeSQL = (sql, params, callback) ->
console.log params, "connecting..."
pg.connect process.env.DATABASE_URL, (err, client, done) ->
console.log params, "...ok"
return callback(err) if err
client.query sql, params, (err, result) ->
done(client)
return callback err, result
runQueries = (callback) ->
query = (id, cb) ->
executeSQL "SELECT * FROM folders WHERE account_id = $1", [id], cb
ids = (num for num in [0..20])
async.map ids, query, callback
if require.main == module
console.log "testing..."
runQueries (err, results) ->
console.log "DONE", err
$ coffee test.coffee
testing...
[ 0 ] 'connecting...'
[ 1 ] 'connecting...'
[ 2 ] 'connecting...'
[ 3 ] 'connecting...'
[ 4 ] 'connecting...'
[ 5 ] 'connecting...'
[ 6 ] 'connecting...'
[ 7 ] 'connecting...'
[ 8 ] 'connecting...'
[ 9 ] 'connecting...'
[ 10 ] 'connecting...'
[ 11 ] 'connecting...'
[ 12 ] 'connecting...'
[ 13 ] 'connecting...'
[ 14 ] 'connecting...'
[ 15 ] 'connecting...'
[ 16 ] 'connecting...'
[ 17 ] 'connecting...'
[ 18 ] 'connecting...'
[ 19 ] 'connecting...'
[ 20 ] 'connecting...'
[ 0 ] '...ok'
[ 1 ] '...ok'
[ 2 ] '...ok'
[ 3 ] '...ok'
[ 4 ] '...ok'
[ 5 ] '...ok'
[ 6 ] '...ok'
[ 7 ] '...ok'
[ 8 ] '...ok'
[ 9 ] '...ok'
$ coffee test.coffee
testing...
[ 0 ] 'connecting...'
[ 0 ] '...ok'
[ 1 ] 'connecting...'
[ 1 ] '...ok'
[ 2 ] 'connecting...'
[ 2 ] '...ok'
[ 3 ] 'connecting...'
[ 3 ] '...ok'
[ 4 ] 'connecting...'
[ 4 ] '...ok'
[ 5 ] 'connecting...'
[ 5 ] '...ok'
[ 6 ] 'connecting...'
[ 6 ] '...ok'
[ 7 ] 'connecting...'
[ 7 ] '...ok'
[ 8 ] 'connecting...'
[ 8 ] '...ok'
[ 9 ] 'connecting...'
[ 9 ] '...ok'
[ 10 ] 'connecting...'
[ 10 ] '...ok'
[ 11 ] 'connecting...'
[ 11 ] '...ok'
[ 12 ] 'connecting...'
[ 12 ] '...ok'
[ 13 ] 'connecting...'
[ 13 ] '...ok'
[ 14 ] 'connecting...'
[ 14 ] '...ok'
[ 15 ] 'connecting...'
[ 15 ] '...ok'
[ 16 ] 'connecting...'
[ 16 ] '...ok'
[ 17 ] 'connecting...'
[ 17 ] '...ok'
[ 18 ] 'connecting...'
[ 18 ] '...ok'
[ 19 ] 'connecting...'
[ 19 ] '...ok'
[ 20 ] 'connecting...'
[ 20 ] '...ok'
DONE null
@lerouxb

lerouxb commented Jun 11, 2015

Copy link
Copy Markdown
Author

With async.map, after 10 connections (the default connection pool size) it just doesn't return a connection and the process just exists. I deliberately made it create 20 connections (21 actually. oops.) in one node process in parallel to demonstrate the issue.

If I change it to run in series everything works fine because there's only one connection at a time.

Isn't it supposed to wait for a connection to open up? Those are really really short lived connections.

@lerouxb

lerouxb commented Jun 11, 2015

Copy link
Copy Markdown
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment