在使用redigo连接池进行并发操作时,出现了太多的TIME_WAIT状态。

huangapple go评论135阅读模式
英文:

Too many TIME_WAIT after concurrent when using redigo pool

问题

我在我的应用程序中使用了github.com/garyburd/redigo来实现并发读写Redis的go routines。我在Singleton模式中使用了redigo的NewRedisClient()函数,并设置了MAXACTIVE=100、MAXIDLE=100、IDLETIMEOUT=60。

应用程序启动后,我发现与Redis服务器之间有很多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

我还在每次调用pool.Get()时打印了活动连接数和空闲连接数,结果如下:

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状态?我是否泄漏了一些连接?

英文:

I used github.com/garyburd/redigo for my application go routines concurrently reading and writing Redis. I used redigo NewRedisClient() in Singleton Pattern, and set MAXACTIVE=100, MAXIDLE=100, IDLETIMEOUT=60.

The application started and I found there are many TIME_WAIT to Redis Server growing. like:

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

And I also print the active count and idle count every time when I pool.Get(), it showed:

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

Why there were so many TIME_WAIT ? Did I leak some connections ?

答案1

得分: 2

你在使用完连接后是否按照文档中的要求将其返回到连接池中?

>根据文档,请求处理程序从连接池中获取连接,并在处理程序完成后关闭连接:

func serveHome(w http.ResponseWriter, r *http.Request) {
    conn := pool.Get()
    defer conn.Close()
    ...
}

如果不像上面的示例那样将连接返回到连接池中,可能会导致你所看到的连接泄漏问题。

英文:

Are you returning connections to the pool when you're done with them, per the documentation?

>A request handler gets a connection from the pool and closes the connection when the handler is done:

func serveHome(w http.ResponseWriter, r *http.Request) {
    conn := pool.Get()
    defer conn.Close()
    ...
}

Not returning connections to the pool as shown above would result in the connection leak you're seeing.

huangapple
  • 本文由 发表于 2017年8月19日 19:42:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/45771111.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定