From 7831fb4464a2fe82ae8232e182ca26d2cd32e79d Mon Sep 17 00:00:00 2001 From: Yash Tibrewal Date: Wed, 23 Jun 2021 12:33:15 -0700 Subject: [PATCH] Initialize tcp_posix for CFStream when needed (#26530) --- src/core/lib/iomgr/iomgr_posix_cfstream.cc | 50 +++++++++++++++++----- 1 file changed, 40 insertions(+), 10 deletions(-) diff --git a/src/core/lib/iomgr/iomgr_posix_cfstream.cc b/src/core/lib/iomgr/iomgr_posix_cfstream.cc index 78b2985142a..27af38e4b0c 100644 --- a/src/core/lib/iomgr/iomgr_posix_cfstream.cc +++ b/src/core/lib/iomgr/iomgr_posix_cfstream.cc @@ -84,7 +84,43 @@ static grpc_iomgr_platform_vtable apple_vtable = { apple_iomgr_platform_is_any_background_poller_thread, apple_iomgr_platform_add_closure_to_background_poller}; +namespace { +struct CFStreamEnv { + bool enable_cfstream; + bool enable_cfstream_run_loop; +}; + +// Parses environment variables for CFStream specific settings +CFStreamEnv ParseEnvForCFStream() { + CFStreamEnv env; + char* enable_cfstream_str = getenv(grpc_cfstream_env_var); + env.enable_cfstream = + enable_cfstream_str == nullptr || enable_cfstream_str[0] != '0'; + char* enable_cfstream_run_loop_str = getenv(grpc_cfstream_run_loop_env_var); + // CFStream run-loop is disabled by default. The user has to enable it + // explicitly with environment variable. + env.enable_cfstream_run_loop = enable_cfstream_run_loop_str != nullptr && + enable_cfstream_run_loop_str[0] == '1'; + return env; +} + +void MaybeInitializeTcpPosix(void) { + CFStreamEnv env = ParseEnvForCFStream(); + if (!env.enable_cfstream || !env.enable_cfstream_run_loop) { + grpc_tcp_posix_init(); + } +} + +void MaybeShutdownTcpPosix(void) { + CFStreamEnv env = ParseEnvForCFStream(); + if (!env.enable_cfstream || !env.enable_cfstream_run_loop) { + grpc_tcp_posix_shutdown(); + } +} +} // namespace + static void iomgr_platform_init(void) { + MaybeInitializeTcpPosix(); grpc_wakeup_fd_global_init(); grpc_event_engine_init(); } @@ -94,6 +130,7 @@ static void iomgr_platform_flush(void) {} static void iomgr_platform_shutdown(void) { grpc_event_engine_shutdown(); grpc_wakeup_fd_global_destroy(); + MaybeShutdownTcpPosix(); } static void iomgr_platform_shutdown_background_closure(void) { @@ -118,22 +155,15 @@ static grpc_iomgr_platform_vtable vtable = { iomgr_platform_add_closure_to_background_poller}; void grpc_set_default_iomgr_platform() { - char* enable_cfstream_str = getenv(grpc_cfstream_env_var); - bool enable_cfstream = - enable_cfstream_str == nullptr || enable_cfstream_str[0] != '0'; - char* enable_cfstream_run_loop_str = getenv(grpc_cfstream_run_loop_env_var); - // CFStream run-loop is disabled by default. The user has to enable it - // explicitly with environment variable. - bool enable_cfstream_run_loop = enable_cfstream_run_loop_str != nullptr && - enable_cfstream_run_loop_str[0] == '1'; - if (!enable_cfstream) { + CFStreamEnv env = ParseEnvForCFStream(); + if (!env.enable_cfstream) { // Use POSIX sockets for both client and server grpc_set_tcp_client_impl(&grpc_posix_tcp_client_vtable); grpc_set_tcp_server_impl(&grpc_posix_tcp_server_vtable); grpc_set_pollset_vtable(&grpc_posix_pollset_vtable); grpc_set_pollset_set_vtable(&grpc_posix_pollset_set_vtable); grpc_set_iomgr_platform_vtable(&vtable); - } else if (enable_cfstream && !enable_cfstream_run_loop) { + } else if (env.enable_cfstream && !env.enable_cfstream_run_loop) { // Use CFStream with dispatch queue for client; use POSIX sockets for server grpc_set_tcp_client_impl(&grpc_cfstream_client_vtable); grpc_set_tcp_server_impl(&grpc_posix_tcp_server_vtable);