Initialize tcp_posix for CFStream when needed (#26530)

pull/26543/head
Yash Tibrewal 4 years ago committed by GitHub
parent da10b795e1
commit 7831fb4464
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 50
      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);

Loading…
Cancel
Save