Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

怎么关闭长连接呢? #919

Closed
autoself opened this issue Aug 9, 2017 · 17 comments
Closed

怎么关闭长连接呢? #919

autoself opened this issue Aug 9, 2017 · 17 comments

Comments

@autoself
Copy link

autoself commented Aug 9, 2017

我设置keepalive_timeout 0 都不行

@yaoweibin
Copy link
Member

前端还是后端长连接?你是怎么判断不行的?

@autoself
Copy link
Author

后端,我是通过查看 ss 查看tcp 连接还是 ESTAB 状态。 看起来也不会很快释放。而且要等待好久。所以,这设置是不是没有效果呢?

@taoyuanyuan
Copy link
Contributor

@autoself
Copy link
Author

不是,是全局的在http{}

@taoyuanyuan
Copy link
Contributor

全局的keepalive_timeout不负责后端的长连接的.
默认upstream到后端也是短连接的, 是一直请求没传输完吗, 还是websocket ?

@autoself
Copy link
Author

我看是传输完了,不是websocket 是通过跳转的形式。很快就跳转完了,我只是写了一个server { fastcgi_pass } 也没有其他的情况

@taoyuanyuan
Copy link
Contributor

麻烦贴一下配置

@senzi1988
Copy link

@yaoweibin 您好,我也遇到类似的问题,情况是这样的:
版本用的是:Tengine/2.2.0,在http1.1的时候都是正常的,但自从使用了http2.0之后 stub_status 里面的 Writing值就一直是递增的状态,导致Aconn也是一直增加,从来不会减少,最后就是服务器内存一点点被用尽?
我的配置文件是从http1.1 拷贝过来的,只是增加了:listen 443 ssl http2;
系统的tcp_keepalive_time 为600,lvs是:900 120 300
试过设置tengine的keepalive_timeout 0;也没有解决问题?
后来经过观察了很多ES链接竟然几天都不释放(应该是永不释放,也就是链接数递增的原因):
tcp 0 0 192.168.0.250:443 121.213.20.241:2235 ESTABLISHED 56745/nginx

麻烦有时间看下我的问题谢谢了!!

@senzi1988
Copy link

@taoyuanyuan 也麻烦您有时间看下我的问题,谢谢了!!

@taoyuanyuan
Copy link
Contributor

taoyuanyuan commented Sep 19, 2017

@senzi1988 你可以先抓包看看这个ES是不是一直有数据在传输?
如果一直有数据传输, 连接就不会断. 先排除是不是这个问题.
类似指令:

sudo tcpdump -i any port 2235 and host 121.213.20.241

@senzi1988
Copy link

@taoyuanyuan 感谢对问题的关注!
今天抓包看了几个ES 状态的ip,确实没有数据传输了

@taoyuanyuan
Copy link
Contributor

taoyuanyuan commented Sep 19, 2017

@senzi1988
能不能发下更详细的抓包指令和对应ES情况?
如果一直没有包, 我的理解lvs也会断链接的, 就算nginx卡住不处理, tcp层看到的也应该是 CLOSE_WAIT 状态.

@senzi1988
Copy link

@taoyuanyuan
lvs-->nginx 走的dr模式,链接已经释放了。(ipvsadm -lnc 已经看不到链接了)
但在回包的时候 nginx-->client 链接没有释放,一直是ES状态?
抓包用的就是:sudo tcpdump -i any port 2235 and host 121.213.20.241 等了好长时间都没有数据包经过

@taoyuanyuan
Copy link
Contributor

taoyuanyuan commented Oct 1, 2017

@senzi1988

define dump_connection
  set $i = 0
  while $i < ngx_cycle->connection_n
    set $c = (ngx_connection_t *) &ngx_cycle->connections[$i]
    if $c->fd > 0 && strlen("$arg0") == $c->addr_text.len && strncmp($c->addr_text.data, "$arg0", strlen("$arg0")) == 0
   #    if $c->data
   #      set $r = (ngx_http_request_t *) $c->data
   #      p *$r
   #      printf "\n"
   #    end
        if $c->log && $c->log->action
          printf "action: %s\n", $c->log->action
        end
        if $c->read
          printf "read  timedout:%d, handler: ", $c->read->timedout
          p $c->read->handler
        end
        if $c->write
          printf "write timedout:%d, handler: ", $c->write->timedout
          p $c->write->handler
        end
        printf "\n"
    end
    set $i = $i + 1
  end
end

把如上脚本保存为文件 dump_connection.gdb
以这条ES为例:

tcp 0 0 192.168.0.250:443 121.213.20.241:2235 ESTABLISHED 56745/nginx

进行如下指令:

sudo gdb -p 56745

进入gdb界面:

(gdb) source dump_connection.gdb
(gdb) dump_connection 121.213.20.241

然后把结果贴出来看看
(执行脚本期间会暂停这个进程的其他请求, 执行完了马上退出脚本)

@chobits
Copy link
Member

chobits commented Oct 1, 2017

hi ,

确认下是否有连接泄露,执行如下命令:

$ ss -tan -o state close-wait

@senzi1988
Copy link

senzi1988 commented Oct 9, 2017

感谢 @taoyuanyuan @chobits 对问题的关注:

执行完 dump_connection.gdb 输出以下结果:
action: processing HTTP/2 connection
read timedout:0, handler: $1 = (ngx_event_handler_pt) 0x4b7635 <ngx_http_v2_read_handler>
write timedout:0, handler: $2 = (ngx_event_handler_pt) 0x4b74e2 <ngx_http_v2_write_handler>

执行完 ss -tan -o state close-wait 输出以下结果:

Recv-Q Send-Q Local Address:Port Peer Address:Port
153 0 192.168.0.25:18801 192.168.0.2:445
0 0 192.168.0.25:42900 192.168.0.43:9985
949994 0 192.168.0.25:57028 192.168.0.56:8010

@chobits
Copy link
Member

chobits commented Mar 28, 2019

老版本有h2泄露,最新版本已经修复,请查看。

Tengine-2.3.0 has been released, it has been been upgraded to nginx 1.15.9.

For more details of new version, see #1220.

@chobits chobits closed this as completed Mar 28, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants