From 72b9890f1a15af9eeb73476ae334969bd226c1a8 Mon Sep 17 00:00:00 2001 From: yang-g Date: Tue, 24 Sep 2019 09:25:51 -0700 Subject: [PATCH] Global channel args mutator when creating a client channel --- src/core/lib/channel/channel_args.cc | 16 ++++++++++++++++ src/core/lib/channel/channel_args.h | 14 ++++++++++++++ src/core/lib/surface/channel.cc | 7 +++++++ 3 files changed, 37 insertions(+) diff --git a/src/core/lib/channel/channel_args.cc b/src/core/lib/channel/channel_args.cc index 60248f9d8fc..211b79d57f5 100644 --- a/src/core/lib/channel/channel_args.cc +++ b/src/core/lib/channel/channel_args.cc @@ -16,6 +16,8 @@ * */ +#include +#include #include #include @@ -361,3 +363,17 @@ char* grpc_channel_args_string(const grpc_channel_args* args) { gpr_strvec_destroy(&v); return result; } + +namespace { +grpc_channel_args_client_channel_creation_mutator g_mutator = nullptr; +} // namespace + +void grpc_channel_args_set_client_channel_creation_mutator( + grpc_channel_args_client_channel_creation_mutator cb) { + GPR_DEBUG_ASSERT(g_mutator == nullptr); + g_mutator = cb; +} +grpc_channel_args_client_channel_creation_mutator +grpc_channel_args_get_client_channel_creation_mutator() { + return g_mutator; +} diff --git a/src/core/lib/channel/channel_args.h b/src/core/lib/channel/channel_args.h index 5928802f288..f6b4ace2c87 100644 --- a/src/core/lib/channel/channel_args.h +++ b/src/core/lib/channel/channel_args.h @@ -23,6 +23,8 @@ #include +#include "src/core/lib/surface/channel_stack_type.h" + // Channel args are intentionally immutable, to avoid the need for locking. /** Copy the arguments in \a src into a new instance */ @@ -108,4 +110,16 @@ grpc_arg grpc_channel_arg_pointer_create(char* name, void* value, // Callers takes ownership of result. char* grpc_channel_args_string(const grpc_channel_args* args); +// Takes ownership of the old_args +typedef grpc_channel_args* (*grpc_channel_args_client_channel_creation_mutator)( + const char* target, grpc_channel_args* old_args, + grpc_channel_stack_type type); + +// Should be called only once globaly before grpc is init'ed. +void grpc_channel_args_set_client_channel_creation_mutator( + grpc_channel_args_client_channel_creation_mutator cb); +// This will be called at the creation of each channel. +grpc_channel_args_client_channel_creation_mutator +grpc_channel_args_get_client_channel_creation_mutator(); + #endif /* GRPC_CORE_LIB_CHANNEL_CHANNEL_ARGS_H */ diff --git a/src/core/lib/surface/channel.cc b/src/core/lib/surface/channel.cc index 24813407e28..7f48f258318 100644 --- a/src/core/lib/surface/channel.cc +++ b/src/core/lib/surface/channel.cc @@ -257,6 +257,13 @@ grpc_channel* grpc_channel_create(const char* target, get_default_authority(input_args); grpc_channel_args* args = build_channel_args(input_args, default_authority.get()); + if (grpc_channel_stack_type_is_client(channel_stack_type)) { + auto channel_args_mutator = + grpc_channel_args_get_client_channel_creation_mutator(); + if (channel_args_mutator != nullptr) { + args = channel_args_mutator(target, args, channel_stack_type); + } + } grpc_channel_stack_builder_set_channel_arguments(builder, args); grpc_channel_args_destroy(args); grpc_channel_stack_builder_set_target(builder, target);