Reputation: 6872
Just cross-posting this from github.
I am using xorm 0.4.3 with go-mysql. We are on Golang 1.4.
We have specified maxIdleConnetions
and maxOpenConnections
in xorm
as below:-
var orm *xorm.Engine
...
orm.SetMaxOpenConns(50)
orm.SetMaxIdleConns(5)
And we are using the same single xorm
instance to query Mysql.
But still we are seeing lot of connections in TCP Connection Establised
state which is way over the numbers I have configured in maxIdleConnetions
and maxOpenConnections
state when we lsof
:-
app 8747 10568 sandeshsharma 16u IPv4 691032 0t0 TCP 127.0.0.1:57337->127.0.0.1:mysql (ESTABLISHED)
We have also observed that even if we stop the MySQL, the connection numbers still remain fixed but in the CLOSED_WAIT
state. If we shutdown the app then all connections go away.
app 8747 10844 sandeshsharma 38u IPv4 505058 0t0 TCP 127.0.0.1:54160->127.0.0.1:mysql (CLOSE_WAIT)
However in mysql process list it is showing the correct number of connections as I have specified in maxIdleConnetions
and maxOpenConnections
.
Can some one please explain me this behaviour? Why are we observing so much TCP connections even though we have specified maxIdleConnetions
and maxOpenConnections
to 5 & 50 respectively?
Upvotes: 0
Views: 2254
Reputation: 479
First of all, Go 1.4 is too old. Use latest Go 1.6. This answer is written with knowledge of Go 1.6. So some details may be different in your case.
There are four state in connection: connecting, idle, inuse, and closing. MaxOpenConnections limits number of connection in connecting, idle, inuse state. So, if your application closes and reopen connection quickly, it can happen.
Since TCP is CLOSED_WAIT
state in MySQL server side, your app is waiting EOF from connection. I suppose your app is under very high load
and slow at reading EOF and closing connection. Until read EOF and close connection, TCP state is ESTABLISHED on client side, regardless TCP state in server side.
I recommend you to update Go and "go-sql-driver/mysql", and set MaxIdleConns equal to MaxOpenConns to avoid high reconnect rate. Instead, you can use SetConnMaxLifetime (new API in Go 1.6) to close connections when your app is idle.
Upvotes: 1