docker for mac tcp链接会被断开

背景:最近再写一个即时聊天的应用用到到了swoole+异步redis,应用是跑在docker容器里面的。开发中发现docker中的redis长链接会被关闭寻找规律发现,第一次会再120秒内关闭,然后些后每次关闭都是固定的120秒。

要定位这个问题,首先想到的是在编译swoole_redis的时候有一大串waring,经过重新编译以后更换父级镜像发现,问题的原因不在这里。

再次延伸问题,在docker中部署了一个phpredis扩展,尝试同步的redis是否有这个问题,发现这个问题在同步redis中也存在。这就很尴尬了。

Start: 18:16:32string(24) "read error on connection"
End: 18:17:18
[[[[[ 46 s]]]]]

Start: 18:17:18string(24) "read error on connection"
End: 18:19:18
[[[[[ 120 s]]]]]

Start: 18:19:18string(24) "read error on connection"
End: 18:21:18
[[[[[ 120 s]]]]]

继续延伸这个问题,采用命令行模式下的telnet进行测试,发现这个问题还是存在。这说明问题已经上升到docker容器发起的redis链接的问题了。又尝试了调试不同节点不同部署方案的redis节点还是有这个问题。

real 0m54.099s
user 0m0.010s
sys 0m0.000s
Trying 192.168.80.18...
Connected to 192.168.80.18.
Escape character is '^]'.
Connection closed by foreign host.

real 2m0.055s
user 0m0.000s
sys 0m0.000s
Trying 192.168.80.18...
Connected to 192.168.80.18.
Escape character is '^]'.
Connection closed by foreign host.

real 2m0.039s
user 0m0.000s
sys 0m0.000s
Trying 192.168.80.18...
Connected to 192.168.80.18.
Escape character is '^]'.
Connection closed by foreign host.

real 2m0.043s
user 0m0.000s
sys 0m0.000s
Trying 192.168.80.18...
Connected to 192.168.80.18.
Escape character is '^]'.
Connection closed by foreign host.

最后,还是谷歌再次教我做人了。https://github.com/docker/for-mac/issues/2406

原来是docker for mac的一个小bug,已经被提出来了。所以在docker for linux中应该是不存在的,去linux测试机上做测试 发现果然不存在。

Thanks for the report. There is currently a 2 minute timeout on idle connections in the stateful firewall but there is also code which attempts to trigger TCP keep-alive packets every few seconds. I suspect the keep-alive code isn't working.

发表评论

电子邮件地址不会被公开。 必填项已用*标注

Captcha Code