使用Xorm和Go-MySQL进行连接池 [英] Connection pooling with xorm and go-mysql

查看:104
本文介绍了使用Xorm和Go-MySQL进行连接池的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只需交叉发布 github .

我正在将xorm 0.4.3与 go-mysql 一起使用.我们使用的是Golang 1.4.

I am using xorm 0.4.3 with go-mysql. We are on Golang 1.4.

我们在 xorm 中指定了 maxIdleConnetions maxOpenConnections ,如下所示:-

We have specified maxIdleConnetions and maxOpenConnections in xorm as below:-

var orm *xorm.Engine
...
orm.SetMaxOpenConns(50)
orm.SetMaxIdleConns(5)

并且我们使用相同的单个 xorm 实例来查询Mysql.

And we are using the same single xorm instance to query Mysql.

但是我们仍然看到很多连接处于 TCP Connection Establised 状态,这已经超过了我在 maxIdleConnetions maxOpenConnections 中配置的数量当我们 lsof 时的状态:-

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)

我们还观察到,即使我们停止MySQL,连接号仍保持固定,但处于 CLOSED_WAIT 状态.如果我们关闭应用程序,则所有连接都会消失.

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)

但是在mysql进程列表中,它显示了我在 maxIdleConnetions maxOpenConnections 中指定的正确连接数.

However in mysql process list it is showing the correct number of connections as I have specified in maxIdleConnetions and maxOpenConnections.

有人可以向我解释这种行为吗?即使将 maxIdleConnetions maxOpenConnections 指定为5& ;,为什么我们仍然观察到如此多的TCP连接?50个?

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?

推荐答案

首先,Go 1.4太旧了.使用最新的Go 1.6.这个答案是在Go 1.6的知识基础上编写的.因此某些细节可能与您的情况有所不同.

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.

连接有四种状态:连接,空闲,使用中和关闭.MaxOpenConnections限制处于连接,空闲,使用状态的连接数.因此,如果您的应用程序关闭并快速重新打开连接,则可能会发生.

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.

由于MySQL服务器端TCP处于 CLOSED_WAIT 状态,因此您的应用正在等待EOF连接.我想您的应用负载很高并且读取EOF和关闭连接的速度很慢.在读取EOF并关闭连接之前,无论服务器端的TCP状态如何,客户端的TCP状态都已建立.

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.

我建议您更新Go和"go-sql-driver/mysql",并设置MaxIdleConns等于MaxOpenConns,以避免高重新连接率.相反,您可以使用 SetConnMaxLifetime (Go 1.6中的新API)关闭在您的应用空闲时进行连接.

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.

这篇关于使用Xorm和Go-MySQL进行连接池的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆