guodongxiaren commented on issue #1895:
URL:
https://github.com/apache/incubator-brpc/issues/1895#issuecomment-1221225264
可以通过给客户端socket设置SO_KEEPALIVE参数来实现。开启keepalive操作系统会来探活然后关闭已经断开的socket。
```cpp
if (setsockopt(client_fd, SOL_SOCKET, SO_KEEPALIVE, &val, sizeof(val))
== -1) {
...
}
```
对代码侵入性比较小。另外可以把把TCP_KEEPIDLE、TCP_KEEPINTVL、TCP_KEEPCNT三个socket选项的值提供给Channel设置。
让用户自定义探活动时间
```cpp
if (setsockopt(client_fd, IPPROTO_TCP, TCP_KEEPIDLE, &val1, sizeof(val))
< 0) {
...
}
if (setsockopt(cliend_fd, IPPROTO_TCP, TCP_KEEPINTVL, &val2,
sizeof(val)) < 0) {
...
}
if (setsockopt(, IPPROTO_TCP, TCP_KEEPCNT, &val, sizeof(val3)) < 0) {
...
}
```
不设置的时候会走系统中对应的配置:
```bash
cat /proc/sys/net/ipv4/tcp_keepalive_time
7200
cat /proc/sys/net/ipv4/tcp_keepalive_intvl
75
cat /proc/sys/net/ipv4/tcp_keepalive_probes
9
```
2个小时以后会被操作系统关闭。
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]