Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add PG.prototype.disconnect method? #1193

Closed
AlJohri opened this issue Jan 8, 2017 · 8 comments
Closed

Add PG.prototype.disconnect method? #1193

AlJohri opened this issue Jan 8, 2017 · 8 comments

Comments

@AlJohri
Copy link

AlJohri commented Jan 8, 2017

Hi,

It seems that connection pool objects have an independent end function (Pool.prototype.end).

Pool.prototype.end = function (cb) {
  this.log('draining pool')
  return this._promise(cb, function (resolve, reject) {
    this.pool.drain(function () {
      this.log('pool drained, calling destroy all now')
      this.pool.destroyAllNow(resolve)
    }.bind(this))
  }.bind(this))
}

Currently the PG.prototype.end function does not call each pool's end function and instead manually closes the connection pool by calling the lower level drain and destroyAllNow functions.

PG.prototype.end = function() {
  var self = this;
  var keys = Object.keys(this._pools);
  var count = keys.length;
  if(count === 0) {
    self.emit('end');
  } else {
    keys.forEach(function(key) {
      var pool = self._pools[key];
      delete self._pools[key];
      pool.pool.drain(function() {
        pool.pool.destroyAllNow(function() {
          count--;
          if(count === 0) {
            self.emit('end');
          }
        });
      });
    });
  }
};

My requests:

  1. Can we change PG.prototype.end to use Pool.prototype.end? This would by more DRY.

  2. Can we add a PG.prototype.disconnect function? It would have the same signature as PG.prototype.connect.

PG.prototype.connect = function(config, callback) {
  ...
  var poolName = JSON.stringify(config || {});
  ...
  this._pools[poolName] = this._pools[poolName] || new this.Pool(config);
  var pool = this._pools[poolName];
  ...
  return pool.connect(callback);
}

Essentially, there is a public API to add a connection pool to _pools and I was looking for a public api to remove a particular connection pool.

The disconnect function would similarly JSON.stringify the config object to find the key and then remove it from this._pools.

Thanks,

Al

@charmander
Copy link
Collaborator

It would be better to create your own pg.Pool instance(s) as the example in the README shows: https://github.com/brianc/node-postgres#client-pooling

pg.connect shouldn’t really be used.

@vitaly-t
Copy link
Contributor

pg.connect shouldn’t really be used.

pg.connect is the only one to be used with node-postgres up to version 5.1, and it was doing a better job than the change introduced later: it was simpler, and it could always guarantee to restore lost connections, is where the new pool fails.

@charmander
Copy link
Collaborator

it could always guarantee to restore lost connections, is where the new pool fails

Could you give an example of this, please? Sounds like a potential bug report.

@vitaly-t
Copy link
Contributor

Could you give an example of this, please? Sounds like a potential bug report.

@rpedela
Copy link
Contributor

rpedela commented Feb 21, 2017

pg.connect shouldn’t really be used.

@charmander That is bad advice. If someone is using an external pooler (e.g. pgbouncer) or none at all, there is no reason to use the built-in pooling.

@charmander
Copy link
Collaborator

charmander commented Feb 21, 2017

@rpedela You’re confusing pg.connect with pg.Client.prototype.connect. pg.connect uses an implicit global pool.

@rpedela
Copy link
Contributor

rpedela commented Feb 21, 2017

Ah, you are right. Sorry.

@charmander
Copy link
Collaborator

pg.connect has been removed in pg 7.

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

No branches or pull requests

4 participants