-
Notifications
You must be signed in to change notification settings - Fork 18k
database/sql: Is it possible to release expired connections as soon as possible #39449
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
Comments
/cc @kardianos |
Thanks @chanxuehong this explains the PR you sent a little more. Currently your PR has an issue where the timer will fire twice as fast every time it runs. This will consume more and more CPU and is probably not what you want. You may have a good general point here. That is, you may want to run the connection cleaner faster then the shortest timeout. There is some validity here. However, I'm guessing it isn't your core issue. I suspect that your issue will be solved in the next release with: |
Thanks @kardianos for your comment
Indeed, this will happen, in fact, I also considered it, but I personally think that this is not a problem, because under normal circumstances, we will set the ConnMaxLifeTime to be relatively large, such as 30 minutes, then it runs 48 times a day, I think I Can accept this. In particular, if we speed up the collection of conn that has expired, we will may save the retry overhead caused by driver.ErrBadConn
For Now there are many errors like the following in our error log. In order to avoid this error, we set wait_timeout to 2 times of ConnMaxLifeTime
|
I think https://go-review.googlesource.com/c/go/+/145758/ fixed this issue, you can control maxIdleTime which should be less than your db server config. |
Thanks for your comment I think that SetConnMaxIdleTime can’t solve my problem, in fact I don’t want the following to happen
|
I and also seeing these errors in my logs all day long, with the latest version of go-mysql-driver:
This error is fairly innocent as of go-mysql-driver v1.5.0. It happens when getting a connection from the pool. On the first write, go-mysql-driver performs a non-blocking read, to detect EOF (connection closed), prior to writing on a possibly closed socket. When it detects this, it returns driver.ErrBadConn. This is fairly harmless, because database/sql will immediately retry on a new connection. However, i don't think believe that setting max life time or max idle time is the correct solution for silencing these, because it requires knowledge of the server configuration, and changing these every time we point to a different database server. So instead of just connecting the host, user, password and such, we now have to take care of configuring max life time and max idle time. I believe the solution is fairly simple: Only log these if we're interested in such logging, e.g. debug level. |
This error is not innocent. This error message is saying the connection is closed not by the driver. This is dangerous because if the connection is closed when you are sending query, your application can not know your query is executed or not. There is no way to handle it safely. You should avoid this error message as possible, instead of ignoring it.
It is the correct solution to avoid connection closed by not the driver.
Yes, it is highly recommended to configure lifetime properly. I suggest "3min" or "less than 5min" for default lifetime, because I have seen connections are closed by 5min in some environment. (See docker/for-mac#2442). |
It is, but only for this specific case: When getting a connection from the pool and running the EOF check to see if the connection is stale. I should have been more clear about this, i am not saying that this error should be always silenced, its not, but for this specific case, it can be safely silenced. |
I know it but I just don't describe it is safe.
Yes. |
Hi, |
This is not an issue. You should use ConnMaxLifeTime. |
should be fixed with CL 216197 ? |
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
yes
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
At present, database/sql closes expired conn at a fixed time interval, so when ConnMaxLifeTime is the same as the database configuration or greater than half of the database side, the database may close the conn earlier than the client, so that when the client obtains the conn A driver.ErrBadConn error will be generated, and an error may be generated when the expired conn is closed
So can we set the time interval to half of the original?
What did you expect to see?
no
closing bad idle connection: EOF
What did you see instead?
The text was updated successfully, but these errors were encountered: