From 8c2314093b33458c2cc1f003524c5f09306fae82 Mon Sep 17 00:00:00 2001 From: Dan Zhang Date: Thu, 12 Apr 2018 11:15:57 -0400 Subject: Allow udp_server to create multiple listeners for each port via SO_REUSEPORT --- src/core/lib/iomgr/tcp_server_posix.cc | 27 +++------------------------ 1 file changed, 3 insertions(+), 24 deletions(-) (limited to 'src/core/lib/iomgr/tcp_server_posix.cc') diff --git a/src/core/lib/iomgr/tcp_server_posix.cc b/src/core/lib/iomgr/tcp_server_posix.cc index 4e1d90e86a..f11b82f7ab 100644 --- a/src/core/lib/iomgr/tcp_server_posix.cc +++ b/src/core/lib/iomgr/tcp_server_posix.cc @@ -55,39 +55,18 @@ #include "src/core/lib/iomgr/tcp_server_utils_posix.h" #include "src/core/lib/iomgr/unix_sockets_posix.h" -static gpr_once check_init = GPR_ONCE_INIT; -static bool has_so_reuseport = false; - -static void init(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) { - has_so_reuseport = GRPC_LOG_IF_ERROR("check for SO_REUSEPORT", - grpc_set_socket_reuse_port(s, 1)); - close(s); - } -#endif -} - static grpc_error* tcp_server_create(grpc_closure* shutdown_complete, const grpc_channel_args* args, grpc_tcp_server** server) { - gpr_once_init(&check_init, init); - grpc_tcp_server* s = static_cast(gpr_zalloc(sizeof(grpc_tcp_server))); - s->so_reuseport = has_so_reuseport; + s->so_reuseport = grpc_is_socket_reuse_port_supported(); s->expand_wildcard_addrs = false; for (size_t i = 0; i < (args == nullptr ? 0 : args->num_args); i++) { if (0 == strcmp(GRPC_ARG_ALLOW_REUSEPORT, args->args[i].key)) { if (args->args[i].type == GRPC_ARG_INTEGER) { - s->so_reuseport = - has_so_reuseport && (args->args[i].value.integer != 0); + s->so_reuseport = grpc_is_socket_reuse_port_supported() && + (args->args[i].value.integer != 0); } else { gpr_free(s); return GRPC_ERROR_CREATE_FROM_STATIC_STRING(GRPC_ARG_ALLOW_REUSEPORT -- cgit v1.2.3