增加TCP Keepalive选项设置功能,避免部分fd因网络故障无法淘汰。

This commit is contained in:
Lu Qiuwen
2018-09-21 16:11:54 +08:00
parent c0d1b9cf63
commit 1a70d3948a
3 changed files with 113 additions and 21 deletions

View File

@@ -30,6 +30,7 @@
#include <ssl_stream.h>
#include <tcp_stream.h>
#include <proxy.h>
#include <netinet/tcp.h>
#ifndef TFE_CONFIG_OUTPUT_LIMIT_DEFAULT
#define TFE_CONFIG_OUTPUT_LIMIT_DEFAULT (1024 * 1024)
@@ -799,6 +800,74 @@ __errout:
return NULL;
}
void __stream_fd_option_setup(struct tfe_stream_private * _stream, evutil_socket_t fd)
{
struct tfe_stream * stream = &_stream->head;
struct tfe_proxy_tcp_options * tcp_options = &_stream->proxy_ref->tcp_options;
/* Make it non-blocking */
evutil_make_socket_nonblocking(fd);
/* Recv Buffer */
if (tcp_options->sz_rcv_buffer >= 0)
{
if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, (const void *) &tcp_options->sz_rcv_buffer, sizeof(int)) == -1)
{
TFE_STREAM_LOG_ERROR(stream, "setsockopt(SO_RCVBUF, %d) failed, ignored: %s",
tcp_options->sz_rcv_buffer, strerror(errno));
}
}
/* Send Buffer */
if (tcp_options->sz_snd_buffer >= 0)
{
if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, (const void *) &tcp_options->sz_snd_buffer, sizeof(int)) == -1)
{
TFE_STREAM_LOG_ERROR(stream, "setsockopt(SO_SNDBUF, %d) failed, ignored: %s",
tcp_options->sz_snd_buffer, strerror(errno));
}
}
/* Keep-alive */
if (tcp_options->so_keepalive > 0)
{
if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, (const void *) &tcp_options->so_keepalive, sizeof(int)) == -1)
{
TFE_STREAM_LOG_ERROR(stream, "setsockopt(SO_KEEPALIVE, %d) failed, ignored: %s",
tcp_options->so_keepalive, strerror(errno));
}
}
if (tcp_options->tcp_keepcnt > 0)
{
if (setsockopt(fd, SOL_SOCKET, TCP_KEEPCNT, (const void *) &tcp_options->tcp_keepcnt, sizeof(int)) == -1)
{
TFE_STREAM_LOG_ERROR(stream, "setsockopt(TCP_KEEPCNT, %d) failed, ignored: %s",
tcp_options->tcp_keepcnt, strerror(errno));
}
}
if (tcp_options->tcp_keepintvl > 0)
{
if (setsockopt(fd, SOL_SOCKET, TCP_KEEPINTVL, (const void *) &tcp_options->tcp_keepintvl, sizeof(int)) == -1)
{
TFE_STREAM_LOG_ERROR(stream, "setsockopt(TCP_KEEPINTVL, %d) failed, ignored: %s",
tcp_options->tcp_keepintvl, strerror(errno));
}
}
if (tcp_options->tcp_keepidle > 0)
{
if (setsockopt(fd, SOL_SOCKET, TCP_KEEPIDLE, (const void *) &tcp_options->tcp_keepidle, sizeof(int)) == -1)
{
TFE_STREAM_LOG_ERROR(stream, "setsockopt(TCP_KEEPIDLE, %d) failed, ignored: %s",
tcp_options->tcp_keepidle, strerror(errno));
}
}
return;
}
void tfe_stream_init_by_fds(struct tfe_stream * stream, evutil_socket_t fd_downstream, evutil_socket_t fd_upstream)
{
struct tfe_stream_private * _stream = container_of(stream, struct tfe_stream_private, head);
@@ -807,8 +876,8 @@ void tfe_stream_init_by_fds(struct tfe_stream * stream, evutil_socket_t fd_downs
_stream->defer_fd_downstream = fd_downstream;
_stream->defer_fd_upstream = fd_upstream;
evutil_make_socket_nonblocking(fd_downstream);
evutil_make_socket_nonblocking(fd_upstream);
__stream_fd_option_setup(_stream, fd_downstream);
__stream_fd_option_setup(_stream, fd_upstream);
_stream->head.addr = __stream_addr_create_by_fds(stream, fd_downstream);
_stream->str_stream_addr = tfe_stream_addr_to_str(_stream->head.addr);