我使用github.com/garyburd/redigo作为我的应用程序同时阅读和编写Redis的例程。我在Singleton Pattern中使用了redigo NewRedisClient(),并设置了MAXACTIVE = 100,MAXIDLE = 100,IDLETIMEOUT = 60。
应用程序启动后,我发现Redis Server有很多TIME_WAIT在增长。像:
root@goofy-27253489-lax5m:/# netstat -anltp | grep TIME_WAIT | wc -l
10466
root@goofy-27253489-lax5m:/# netstat -anltp | grep TIME_WAIT | wc -l
11776
root@goofy-27253489-lax5m:/# netstat -anltp | grep TIME_WAIT | wc -l
12554
root@goofy-27253489-lax5m:/# netstat -anltp | grep TIME_WAIT | wc -l
16017
我每次打开时都会打印活动计数和空闲计数。获取(),显示:
ActiveCount: 1, MaxActive: 100, MaxIdle: 100, Pool: 0xc420288580, Conn: 0xc420220000, IdleSize: 0
ActiveCount: 3, MaxActive: 100, MaxIdle: 100, Pool: 0xc420288580, Conn: 0xc42064e0a0, IdleSize: 0
ActiveCount: 3, MaxActive: 100, MaxIdle: 100, Pool: 0xc420288580, Conn: 0xc4202960a0, IdleSize: 0
ActiveCount: 3, MaxActive: 100, MaxIdle: 100, Pool: 0xc420288580, Conn: 0xc4206765a0, IdleSize: 0
ActiveCount: 3, MaxActive: 100, MaxIdle: 100, Pool: 0xc420288580, Conn: 0xc420296140, IdleSize: 0
ActiveCount: 3, MaxActive: 100, MaxIdle: 100, Pool: 0xc420288580, Conn: 0xc42034c0a0, IdleSize: 0
ActiveCount: 3, MaxActive: 100, MaxIdle: 100, Pool: 0xc420288580, Conn: 0xc42034c320, IdleSize: 0
ActiveCount: 3, MaxActive: 100, MaxIdle: 100, Pool: 0xc420288580, Conn: 0xc42064e280, IdleSize: 0
ActiveCount: 3, MaxActive: 100, MaxIdle: 100, Pool: 0xc420288580, Conn: 0xc42034c3c0, IdleSize: 0
为什么会有这么多TIME_WAIT?我泄漏了一些连接吗?
答案 0 :(得分:1)
当你完成游戏后,你是否正在返回游泳池的连接,per the documentation?
请求处理程序从池中获取连接,并在处理程序完成时关闭连接:
func serveHome(w http.ResponseWriter, r *http.Request) {
conn := pool.Get()
defer conn.Close()
...
}
如上所示,不返回到池的连接会导致您看到的连接泄漏。