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

Allow for better control over connection issues #6

Closed
wants to merge 1 commit into from
Closed

Allow for better control over connection issues #6

wants to merge 1 commit into from

Conversation

oliversalzburg
Copy link

Previously, the error handler on the pg.Client would cause an infinite connection loop. Additionally, the connection would be retried infinitely either way, because the amount of reconnection attempts could not be limited.
Closes #5

@voxpelli
Copy link
Owner

Some early feedback: If one should be able to set a retry limit then that should be as a feature addition to https://github.com/voxpelli/node-promised-retry – this library just adds the minimal amount of postgres stuff on top of that generic retry mechanism.

I will have to revisit this later though when I have more proper time to read and think about your feedback, don't feel I can give it the proper attention right now unfortunately.

return new Promise(function (resolve, reject) {
db.on('error', reject);
Copy link
Owner

Choose a reason for hiding this comment

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

Some other early feedback: This wouldn't work if there's a database error after the initial connect as the Promise has already been resolved by then and a Promise can only be resolved or rejected once.

The current error handler is meant to catch errors that happens after a successful connect so that if the database somehow eg. shuts down or otherwise closes the connection, that a new connection can automatically be established the next time one is needed.

Any solution would have to take that scenario into account as well.

Copy link
Author

Choose a reason for hiding this comment

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

@voxpelli Okay, I see. I'll take this into consideration and adjust the patch accordingly.

We're currently testing this change internally. When I change anything, I'll push -f into this PR.

Thanks for the feedback! :)

@oliversalzburg
Copy link
Author

@voxpelli Regarding the limit, the change will respect a property retries set through the passed options hash.

@voxpelli
Copy link
Owner

Some feedback on the limit then:

It's good that it is configurable, but I would prefer the default to be unlimited – both because that's how I want it to be in my applications where I use this module and because then this change would be backwards compatible.

The check for whether the limit has been reached or not would be better added to the promised-retry module. Probably around https://github.com/voxpelli/node-promised-retry/blob/4f66c04568ad1ad73cdfd3630337990a8619e97d/index.js#L79 – and there instead of doing a new attempt, check if the failure count has reached the limit (added as a new retryLimit option to that module as well) and then create and throw a new error about that rather then as today always returning a new attempt.

Could you move the check-part of this PR to that module instead? And just have this module forward its new retries option to that modules new retryLimit options?

@oliversalzburg
Copy link
Author

@voxpelli Sounds reasonable. Expect a PR soon.

@oliversalzburg
Copy link
Author

Step 1 should be done with voxpelli/node-promised-retry#3
Please let me know if anything needs to be changed. Once you feel like it can be merged, I would work on this PR again.

@oliversalzburg
Copy link
Author

@voxpelli What do you think about this implementation? I'll work on tests as well.

@oliversalzburg
Copy link
Author

I'm horrible at writing tests :P I'd really appreciate some guidance if you can give any.

Previously, the error handler on the pg.Client would cause an infinite
connection loop. Additionally, the connection would be retried infinitely
either way, because the amount of reconnection attempts could not be
limited.
Closes #5
@voxpelli
Copy link
Owner

Does this test actually reproduce the error from #5? Don't think I've fully grasped the scenario in which this error happens.

When trying to connect to an invalid host, as the current test does, the connect should return an error itself – no? Rather than sending it as an event? And will there ever be a case where an event will be sent before the connect callback has been called?

I may need to double check with the pg docs.

@oliversalzburg
Copy link
Author

Well, I observed the event being sent, which caused the infinite reconnect loop. Although I noticed different behavior between debug and non-debug runs.

@oliversalzburg
Copy link
Author

@voxpelli I'm wondering if what I observed is related to brianc/node-postgres#746

@voxpelli
Copy link
Owner

@oliversalzburg That sounds likely – feels like this is something that pg should preferably handle – perhaps wait to submit a fix for it here and see what happens with that module? Or what do you think?

(Perhaps @brianc himself has some thoughts?)

@oliversalzburg
Copy link
Author

@voxpelli I agree. I'm already working on a fix at brianc/node-postgres#843

@voxpelli
Copy link
Owner

@oliversalzburg Is there still something you want to get merged here now after the PR to the pg-project got accepted?

@oliversalzburg
Copy link
Author

@voxpelli Thanks for reminding me. It seems like the issue has been resolved by the changes in pg. We have not observed any more issues.

@oliversalzburg oliversalzburg deleted the fix/connection-errors branch October 26, 2015 12:52
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

2 participants