Global channel args mutator when creating a client channel

pull/20347/head
yang-g 5 years ago
parent 1d6dfd0994
commit 72b9890f1a
  1. 16
      src/core/lib/channel/channel_args.cc
  2. 14
      src/core/lib/channel/channel_args.h
  3. 7
      src/core/lib/surface/channel.cc

@ -16,6 +16,8 @@
* *
*/ */
#include <grpc/impl/codegen/grpc_types.h>
#include <grpc/impl/codegen/log.h>
#include <grpc/support/port_platform.h> #include <grpc/support/port_platform.h>
#include <limits.h> #include <limits.h>
@ -361,3 +363,17 @@ char* grpc_channel_args_string(const grpc_channel_args* args) {
gpr_strvec_destroy(&v); gpr_strvec_destroy(&v);
return result; 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;
}

@ -23,6 +23,8 @@
#include <grpc/grpc.h> #include <grpc/grpc.h>
#include "src/core/lib/surface/channel_stack_type.h"
// Channel args are intentionally immutable, to avoid the need for locking. // Channel args are intentionally immutable, to avoid the need for locking.
/** Copy the arguments in \a src into a new instance */ /** 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. // Callers takes ownership of result.
char* grpc_channel_args_string(const grpc_channel_args* args); 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 */ #endif /* GRPC_CORE_LIB_CHANNEL_CHANNEL_ARGS_H */

@ -257,6 +257,13 @@ grpc_channel* grpc_channel_create(const char* target,
get_default_authority(input_args); get_default_authority(input_args);
grpc_channel_args* args = grpc_channel_args* args =
build_channel_args(input_args, default_authority.get()); 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_stack_builder_set_channel_arguments(builder, args);
grpc_channel_args_destroy(args); grpc_channel_args_destroy(args);
grpc_channel_stack_builder_set_target(builder, target); grpc_channel_stack_builder_set_target(builder, target);

Loading…
Cancel
Save