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 count for reuse of each connection and track connection 'freshness' #556

Merged
merged 1 commit into from May 2, 2014

Conversation

rngadam
Copy link

@rngadam rngadam commented Apr 1, 2014

add an internal variable to each client to track how many times the client was reused from the pool.

This has the side-benefit that we can now test to see if the client is a "new" connection and take action on a per connection basis.

For example, plv8 initialization function (called for each new GUC) is set on a per connection basis (before any calls to any plv8 functions) as follows:

SET plv8.start_proc = "docstore"

the client can create an init function:

    function clientInitFunction(client, cb) {
      console.log('calling client init');
      client.query('SET plv8.start_proc = "docstore"', cb);
    }

which is then called on our promise generating connect closure:

  function connect(dsn) {
    DEBUG_ENABLED && console.log('connect called with ' + dsn);
    var deferred = Q.defer();

    pg.connect(dsn, function(err, client, done) {
      if(err) {
        deferred.reject(new Error('error fetching client from pool ' + err));
        return;
      }
      if(client.__pool_count === 1 && clientInitFunction) {
        clientInitFunction(client, function(err) {
          if(err) {
            deferred.reject(new Error('error initing client ' + err));
            return;
          } else {
            deferred.resolve(new Connection(client, done));
            return;
          }
        })
      }
      deferred.resolve(new Connection(client, done));
    });
    return deferred.promise;
  }

Alternatively, I've thought of adding a new event created that would be propagated from pool -> pg but that doesn't really fit with having to wait for the custom initialization to execute before continuing. Adding hooks to add initialization functions to pg would also unnecessarily complicate the code IMO.

@rngadam
Copy link
Author

rngadam commented Apr 15, 2014

Hello @brianc, was wondering if you thought this change was reasonable? If not, I'd need to look for an alternative solution to my particular need.

@booo booo mentioned this pull request Apr 16, 2014
return cb(null, client);
});
},
destroy: function(client) {
client._destroying = true;
client.__pool_count = undefined;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you use two underscores here? Any special reason for this? Otherwise I would use one to be consistent.

@booo
Copy link
Contributor

booo commented Apr 16, 2014

Apart from my comment this looks good to me and is a useful addition. I think we should merge this. @brianc what do you think?

@rngadam
Copy link
Author

rngadam commented Apr 17, 2014

Well, even better would be no underscore at all and making it part of the official "interface" ^_^

I'll go with whatever you prefer.

@rngadam
Copy link
Author

rngadam commented Apr 23, 2014

Any comments on this @brianc?

@brianc
Copy link
Owner

brianc commented Apr 23, 2014

Been thinking about this. I think it makes okay sense. Really I'd like the pool.create function to be more extensible so this isn't needed and instead someone can supply their own create function and do whatever they want to their clients. But this is a pretty straight forward change and doesn't hurt anything so I'm cool with it. 👍 One thing though...i'd definitely drop the underscores. I usually use underscores to mark things as private: might change, do not touch. Since this is a client facing feature it's cool to just add it to the 'official' interface.

@rngadam
Copy link
Author

rngadam commented Apr 24, 2014

done, squashed into one commit.

@brianc
Copy link
Owner

brianc commented Apr 24, 2014

awesome. thank you so much! I really hate to be a stickler about this kinda thing, but I never use underscored property names in this code base. Everything is poolSize or rowCount for example. For consistency's sake could we make the property named poolCount? 🙏

@rngadam
Copy link
Author

rngadam commented Apr 25, 2014

done, sorry again, should have figured that out myself!

@rngadam
Copy link
Author

rngadam commented Apr 25, 2014

Prepared a bit of doc for after the release:
https://github.com/letsface/node-postgres/wiki/Client-Initialization

@rngadam
Copy link
Author

rngadam commented May 2, 2014

Do you want me to make any other changes to this CL?

@brianc
Copy link
Owner

brianc commented May 2, 2014

awesomeness! I'll get this merged and released in short order. :)

brianc added a commit that referenced this pull request May 2, 2014
add count for reuse of each connection and track connection 'freshness'
@brianc brianc merged commit 238e75b into brianc:master May 2, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants