brianc / node-pg-pool

A connection pool for node-postgres
MIT License
180 stars 64 forks source link

High latency after 10 queries #105

Closed michaelgrn closed 5 years ago

michaelgrn commented 5 years ago

Hey guys I have a problem. I am currently running docker with the bitnami/postgres image, using node pg for database interaction. My website is a bit high traffic, and after 10 queries suddenly it stops logging for a bit, for an undetermined amount of time (1-3 minutes?) before slowly clearing out the queue of queries and moving onto the next one. My code is as follows:

'use strict'
    var pg = require('pg');
    var Pool = require('pg-pool')

    // create a config to configure both pooling behavior
    // and client options
    // note: all config is optional and the environment variables
    // will be read if the config is not present
    var config = {
        user: 'my_user',
        host: 'postgresql',
        database: 'my_database',
        password: 'password123',
        port: 5432,
        max: 20,
        idleTimeoutMillis: 30000,
        connectionTimeoutMillis: 2000,
    };

    //this initializes a connection pool
    //it will keep idle connections open for a 30 seconds
    //and set a limit of maximum 10 idle clients
    var pool = new Pool(config);

// var pg = require('pg');
// pg.defaults.poolSize = 20;
// const { Pool } = require('pg');

/*
*   This information is established in the docker file and can be changed
* based on preference.
*/

// const pool = new Pool({
//  user: 'my_user',
//  host: 'postgresql',
//  database: 'my_database',
//  password: 'password123',
//  port: 5432,
//  max: 20,
//  idleTimeoutMillis: 30000,
//  connectionTimeoutMillis: 2000,
// });

const uuidv1 = require('uuid/v1');
var username;
var sid;
var pid;

class PostGres{
    /*
    *  Constructor establishes session ID
    */
    constructor() {
        sid = uuidv1();
        username = "None Set"
        //check to see if table exists, if not, create it.
    }

    /*
    *  Used to reset session ID and establish a 'username'
    */
    setName(name){
        username = name;
        sid = uuidv1();
    }
    /*
    *   Used to log events into the database
    */
    logEvent(eventType, eventData){
        var dt = new Date();
        var utcDate = dt.toUTCString();
        if(eventType == "Searched For"){
                pid = uuidv1();
        }
        // console.log(sid);
        // console.log(pid);
        console.log(dt);
        console.log(eventType);
        console.log(eventData);
        console.log(pool.totalCount)
        if(eventType == "Set Name/New Session"){
            this.setName(eventData)
        }
        pool.connect((err, client, done) => {
          if (err) throw err
          client.query('INSERT INTO public.alphatwo (sessionid, queryid, timestamp, eventtype, eventdata) VALUES ($1, $2, $3, $4, $5)', [sid, pid, dt, eventType, eventData], (err, res) => {
            done()

            if (err) {
              console.log(err.stack)
            } else {
              console.log('woof')
            }
          })
        })

    }
}

I left the comments in to show the variety of expirements I've run with the same results.

michaelgrn commented 5 years ago

I've also tried to use pool with promises and client releases, to no avail.

charmander commented 5 years ago

Do you still see the problem when using pool.query() instead of pool.connect() + client.query()? Also: those variables (username, sid, pid) should probably be properties of the PostGres instance.

michaelgrn commented 5 years ago

Yeah I tried changing it to pool.query instead of pool.connect()+ client.query() and I am still encountering the same problem. Any other idea what it might be?

michaelgrn commented 5 years ago

Nvm figured it out, issue with Express. Thanks!