-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Implemented database reconnection loop #2003
Conversation
Hello @an-tao Could you please review this PR. Please feel free to suggest improvements. |
orm_lib/src/DbClientImpl.cc
Outdated
@@ -436,12 +436,23 @@ DbConnectionPtr DbClientImpl::newConnection(trantor::EventLoop *loop) | |||
} | |||
// Reconnect after 1 second | |||
auto loop = closeConnPtr->loop(); | |||
loop->runAfter(1, [weakPtr, loop, closeConnPtr] { | |||
trantor::TimerId reconnect_timer_id = loop->runEvery( | |||
1, [weakPtr, loop, closeConnPtr, reconnect_timer_id] { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suspect the reconnect_timer_id cannot be captured correctly. Consider using a smart pointer.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for suggestion, I will change this as soon as I finish testing.
I hope to finish today.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I reworked the code to make it work in this scenario also:
- Database down
- Start drogon application
- drogon tries to create an initial connection to the database.
4a) behavior of the previous version and the code without changes from this PR: unexpected exit.
4b) second version: If the initial connection was not established, there will be a new attempt to connect to the database after 1 second.
Thanks for the PR! |
Done! |
My latest update failed test:
I just reproduced the issue on my personal PC. I'm going to investigate this failure on Monday. |
To be noted, auto-reconnect is already supported. This exit(-1) happens on some edge cases, such as reaching open socket limits. |
From wiki
The problem is not with "some edge cases" because simple drop of connection to PostgreSQL for few seconds makes the whole Drogon's application |
The issue with |
One more update:
I'm not familiar with the internal Drogon tests. Do you have a guide how I can prepare the test environment on my PC and run all Drogon tests before updating PR? |
You could refer to the github actions configuration: https://github.com/drogonframework/drogon/tree/master/.github/workflows |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good
closes #1407
This PR adds automatic reconnection to the Postgres/MySQL database with 1-second interval if connection was not established instead of
exit(1)