diff options
author | Dan Zhang <danzh@google.com> | 2018-04-12 11:15:57 -0400 |
---|---|---|
committer | Dan Zhang <danzh@google.com> | 2018-04-12 11:15:57 -0400 |
commit | 8c2314093b33458c2cc1f003524c5f09306fae82 (patch) | |
tree | e20cfa39dbb88d428502d4c43612dfb7a665e7a7 /src/core/lib/iomgr/socket_utils_common_posix.cc | |
parent | 074439e7d72837247e1300433fd7fddd97f99b67 (diff) |
Allow udp_server to create multiple listeners for each port via SO_REUSEPORT
Diffstat (limited to 'src/core/lib/iomgr/socket_utils_common_posix.cc')
-rw-r--r-- | src/core/lib/iomgr/socket_utils_common_posix.cc | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/src/core/lib/iomgr/socket_utils_common_posix.cc b/src/core/lib/iomgr/socket_utils_common_posix.cc index c52e237fa8..8ef1f4f4a2 100644 --- a/src/core/lib/iomgr/socket_utils_common_posix.cc +++ b/src/core/lib/iomgr/socket_utils_common_posix.cc @@ -181,6 +181,30 @@ grpc_error* grpc_set_socket_reuse_port(int fd, int reuse) { #endif } +static gpr_once g_probe_so_reuesport_once = GPR_ONCE_INIT; +static int g_support_so_reuseport = false; + +void probe_so_reuseport_once(void) { +#ifndef GPR_MANYLINUX1 + int s = socket(AF_INET, SOCK_STREAM, 0); + if (s < 0) { + /* This might be an ipv6-only environment in which case 'socket(AF_INET,..)' + call would fail. Try creating IPv6 socket in that case */ + s = socket(AF_INET6, SOCK_STREAM, 0); + } + if (s >= 0) { + g_support_so_reuseport = GRPC_LOG_IF_ERROR("check for SO_REUSEPORT", + grpc_set_socket_reuse_port(s, 1)); + close(s); + } +#endif +} + +bool grpc_is_socket_reuse_port_supported() { + gpr_once_init(&g_probe_so_reuesport_once, probe_so_reuseport_once); + return g_support_so_reuseport; +} + /* disable nagle */ grpc_error* grpc_set_socket_low_latency(int fd, int low_latency) { int val = (low_latency != 0); |