Set TCP_USER_TIMEOUT socket option

pull/16419/head
Yash Tibrewal 6 years ago
parent ccc6ee3db0
commit 2f8e60a773
  1. 24
      src/core/lib/iomgr/socket_utils_common_posix.cc
  2. 3
      src/core/lib/iomgr/socket_utils_posix.h
  3. 3
      src/core/lib/iomgr/tcp_client_posix.cc
  4. 2
      src/core/lib/iomgr/tcp_server_utils_posix_common.cc

@ -222,6 +222,30 @@ grpc_error* grpc_set_socket_low_latency(int fd, int low_latency) {
return GRPC_ERROR_NONE; return GRPC_ERROR_NONE;
} }
#define DEFAULT_TCP_USER_TIMEOUT 20000 /* 20 seconds */
/* Set TCP_USER_TIMEOUT */
grpc_error* grpc_set_socket_tcp_user_timeout(int fd, int val) {
#ifdef GPR_LINUX
int newval;
socklen_t len;
if (val == 0) {
val = DEFAULT_TCP_USER_TIMEOUT;
}
if (0 != setsockopt(fd, IPPROTO_TCP, TCP_USER_TIMEOUT, &val, sizeof(val))) {
return GRPC_OS_ERROR(errno, "setsockopt(TCP_USER_TIMEOUT)");
}
if (0 != getsockopt(fd, IPPROTO_TCP, TCP_USER_TIMEOUT, &newval, &len)) {
return GRPC_OS_ERROR(errno, "getsockopt(TCP_USER_TIMEOUT)");
}
if (newval != val) {
return GRPC_ERROR_CREATE_FROM_STATIC_STRING(
"Failed to set TCP_USER_TIMEOUT");
}
#endif /* GPR_LINUX */
return GRPC_ERROR_NONE;
}
/* set a socket using a grpc_socket_mutator */ /* set a socket using a grpc_socket_mutator */
grpc_error* grpc_set_socket_with_mutator(int fd, grpc_socket_mutator* mutator) { grpc_error* grpc_set_socket_with_mutator(int fd, grpc_socket_mutator* mutator) {
GPR_ASSERT(mutator); GPR_ASSERT(mutator);

@ -53,6 +53,9 @@ grpc_error* grpc_set_socket_low_latency(int fd, int low_latency);
/* set SO_REUSEPORT */ /* set SO_REUSEPORT */
grpc_error* grpc_set_socket_reuse_port(int fd, int reuse); grpc_error* grpc_set_socket_reuse_port(int fd, int reuse);
/* Set TCP_USER_TIMEOUT to val, or the default value if val is 0. */
grpc_error* grpc_set_socket_tcp_user_timeout(int fd, int val);
/* Returns true if this system can create AF_INET6 sockets bound to ::1. /* Returns true if this system can create AF_INET6 sockets bound to ::1.
The value is probed once, and cached for the life of the process. The value is probed once, and cached for the life of the process.

@ -76,8 +76,11 @@ static grpc_error* prepare_socket(const grpc_resolved_address* addr, int fd,
if (!grpc_is_unix_socket(addr)) { if (!grpc_is_unix_socket(addr)) {
err = grpc_set_socket_low_latency(fd, 1); err = grpc_set_socket_low_latency(fd, 1);
if (err != GRPC_ERROR_NONE) goto error; if (err != GRPC_ERROR_NONE) goto error;
err = grpc_set_socket_tcp_user_timeout(fd, 0 /* set to gRPC default */);
if (err != GRPC_ERROR_NONE) goto error;
} }
err = grpc_set_socket_no_sigpipe_if_possible(fd); err = grpc_set_socket_no_sigpipe_if_possible(fd);
if (err != GRPC_ERROR_NONE) goto error; if (err != GRPC_ERROR_NONE) goto error;
if (channel_args) { if (channel_args) {
for (size_t i = 0; i < channel_args->num_args; i++) { for (size_t i = 0; i < channel_args->num_args; i++) {

@ -166,6 +166,8 @@ grpc_error* grpc_tcp_server_prepare_socket(grpc_tcp_server* s, int fd,
if (err != GRPC_ERROR_NONE) goto error; if (err != GRPC_ERROR_NONE) goto error;
err = grpc_set_socket_reuse_addr(fd, 1); err = grpc_set_socket_reuse_addr(fd, 1);
if (err != GRPC_ERROR_NONE) goto error; if (err != GRPC_ERROR_NONE) goto error;
err = grpc_set_socket_tcp_user_timeout(fd, 0);
if (err != GRPC_ERROR_NONE) goto error;
} }
err = grpc_set_socket_no_sigpipe_if_possible(fd); err = grpc_set_socket_no_sigpipe_if_possible(fd);
if (err != GRPC_ERROR_NONE) goto error; if (err != GRPC_ERROR_NONE) goto error;

Loading…
Cancel
Save