[C++ BUILD] Internal changes for base context propagation.

PiperOrigin-RevId: 663460841
pull/37312/head
Nana Pang 3 months ago committed by Copybara-Service
parent 910953e7ef
commit 8b4a8ebdf9
  1. 21
      BUILD
  2. 13
      CMakeLists.txt
  3. 13
      build_autogenerated.yaml
  4. 2
      gRPC-C++.podspec
  5. 26
      include/grpcpp/support/callback_common.h
  6. 58
      include/grpcpp/support/global_callback_hook.h
  7. 36
      src/cpp/client/global_callback_hook.cc
  8. 1
      tools/doxygen/Doxyfile.c++
  9. 2
      tools/doxygen/Doxyfile.c++.internal

21
BUILD

@ -907,6 +907,7 @@ grpc_cc_library(
], ],
visibility = ["@grpc:grpc++_public_hdrs"], visibility = ["@grpc:grpc++_public_hdrs"],
deps = [ deps = [
"global_callback_hook",
"grpc_public_hdrs", "grpc_public_hdrs",
"//src/core:gpr_atm", "//src/core:gpr_atm",
], ],
@ -951,6 +952,7 @@ grpc_cc_library(
tags = ["nofixdeps"], tags = ["nofixdeps"],
visibility = ["@grpc:public"], visibility = ["@grpc:public"],
deps = [ deps = [
"global_callback_hook",
"grpc++_base", "grpc++_base",
"//src/core:gpr_atm", "//src/core:gpr_atm",
"//src/core:slice", "//src/core:slice",
@ -1260,6 +1262,7 @@ grpc_cc_library(
deps = [ deps = [
"channel_arg_names", "channel_arg_names",
"generic_stub_internal", "generic_stub_internal",
"global_callback_hook",
"gpr", "gpr",
"grpc++_base_unsecure", "grpc++_base_unsecure",
"grpc++_codegen_proto", "grpc++_codegen_proto",
@ -2455,6 +2458,7 @@ grpc_cc_library(
"config", "config",
"exec_ctx", "exec_ctx",
"generic_stub_internal", "generic_stub_internal",
"global_callback_hook",
"gpr", "gpr",
"grpc", "grpc",
"grpc++_codegen_proto", "grpc++_codegen_proto",
@ -2544,6 +2548,7 @@ grpc_cc_library(
"config", "config",
"exec_ctx", "exec_ctx",
"generic_stub_internal", "generic_stub_internal",
"global_callback_hook",
"gpr", "gpr",
"grpc_base", "grpc_base",
"grpc_core_credentials_header", "grpc_core_credentials_header",
@ -4913,6 +4918,22 @@ grpc_cc_library(
], ],
) )
grpc_cc_library(
name = "global_callback_hook",
srcs = [
"src/cpp/client/global_callback_hook.cc",
],
hdrs = [
"include/grpcpp/support/global_callback_hook.h",
],
external_deps = [
"absl/base:no_destructor",
"absl/log:check",
"absl/functional:function_ref",
],
language = "c++",
)
# TODO(yashykt): Remove the UPB definitions from here once they are no longer needed # TODO(yashykt): Remove the UPB definitions from here once they are no longer needed
### UPB Targets ### UPB Targets

13
CMakeLists.txt generated

@ -4182,6 +4182,7 @@ add_library(grpc++
src/cpp/client/create_channel.cc src/cpp/client/create_channel.cc
src/cpp/client/create_channel_internal.cc src/cpp/client/create_channel_internal.cc
src/cpp/client/create_channel_posix.cc src/cpp/client/create_channel_posix.cc
src/cpp/client/global_callback_hook.cc
src/cpp/client/insecure_credentials.cc src/cpp/client/insecure_credentials.cc
src/cpp/client/secure_credentials.cc src/cpp/client/secure_credentials.cc
src/cpp/client/xds_credentials.cc src/cpp/client/xds_credentials.cc
@ -4465,6 +4466,7 @@ foreach(_hdr
include/grpcpp/support/client_callback.h include/grpcpp/support/client_callback.h
include/grpcpp/support/client_interceptor.h include/grpcpp/support/client_interceptor.h
include/grpcpp/support/config.h include/grpcpp/support/config.h
include/grpcpp/support/global_callback_hook.h
include/grpcpp/support/interceptor.h include/grpcpp/support/interceptor.h
include/grpcpp/support/message_allocator.h include/grpcpp/support/message_allocator.h
include/grpcpp/support/method_handler.h include/grpcpp/support/method_handler.h
@ -4938,6 +4940,7 @@ add_library(grpc++_unsecure
src/cpp/client/create_channel.cc src/cpp/client/create_channel.cc
src/cpp/client/create_channel_internal.cc src/cpp/client/create_channel_internal.cc
src/cpp/client/create_channel_posix.cc src/cpp/client/create_channel_posix.cc
src/cpp/client/global_callback_hook.cc
src/cpp/client/insecure_credentials.cc src/cpp/client/insecure_credentials.cc
src/cpp/common/alarm.cc src/cpp/common/alarm.cc
src/cpp/common/channel_arguments.cc src/cpp/common/channel_arguments.cc
@ -5209,6 +5212,7 @@ foreach(_hdr
include/grpcpp/support/client_callback.h include/grpcpp/support/client_callback.h
include/grpcpp/support/client_interceptor.h include/grpcpp/support/client_interceptor.h
include/grpcpp/support/config.h include/grpcpp/support/config.h
include/grpcpp/support/global_callback_hook.h
include/grpcpp/support/interceptor.h include/grpcpp/support/interceptor.h
include/grpcpp/support/message_allocator.h include/grpcpp/support/message_allocator.h
include/grpcpp/support/method_handler.h include/grpcpp/support/method_handler.h
@ -8266,6 +8270,7 @@ add_executable(binder_transport_test
src/cpp/client/create_channel.cc src/cpp/client/create_channel.cc
src/cpp/client/create_channel_internal.cc src/cpp/client/create_channel_internal.cc
src/cpp/client/create_channel_posix.cc src/cpp/client/create_channel_posix.cc
src/cpp/client/global_callback_hook.cc
src/cpp/client/insecure_credentials.cc src/cpp/client/insecure_credentials.cc
src/cpp/client/secure_credentials.cc src/cpp/client/secure_credentials.cc
src/cpp/common/alarm.cc src/cpp/common/alarm.cc
@ -13405,6 +13410,7 @@ add_executable(endpoint_binder_pool_test
src/cpp/client/create_channel.cc src/cpp/client/create_channel.cc
src/cpp/client/create_channel_internal.cc src/cpp/client/create_channel_internal.cc
src/cpp/client/create_channel_posix.cc src/cpp/client/create_channel_posix.cc
src/cpp/client/global_callback_hook.cc
src/cpp/client/insecure_credentials.cc src/cpp/client/insecure_credentials.cc
src/cpp/client/secure_credentials.cc src/cpp/client/secure_credentials.cc
src/cpp/common/alarm.cc src/cpp/common/alarm.cc
@ -14260,6 +14266,7 @@ add_executable(fake_binder_test
src/cpp/client/create_channel.cc src/cpp/client/create_channel.cc
src/cpp/client/create_channel_internal.cc src/cpp/client/create_channel_internal.cc
src/cpp/client/create_channel_posix.cc src/cpp/client/create_channel_posix.cc
src/cpp/client/global_callback_hook.cc
src/cpp/client/insecure_credentials.cc src/cpp/client/insecure_credentials.cc
src/cpp/client/secure_credentials.cc src/cpp/client/secure_credentials.cc
src/cpp/common/alarm.cc src/cpp/common/alarm.cc
@ -32271,6 +32278,7 @@ add_executable(transport_stream_receiver_test
src/cpp/client/create_channel.cc src/cpp/client/create_channel.cc
src/cpp/client/create_channel_internal.cc src/cpp/client/create_channel_internal.cc
src/cpp/client/create_channel_posix.cc src/cpp/client/create_channel_posix.cc
src/cpp/client/global_callback_hook.cc
src/cpp/client/insecure_credentials.cc src/cpp/client/insecure_credentials.cc
src/cpp/client/secure_credentials.cc src/cpp/client/secure_credentials.cc
src/cpp/common/alarm.cc src/cpp/common/alarm.cc
@ -33157,6 +33165,7 @@ add_executable(wire_reader_test
src/cpp/client/create_channel.cc src/cpp/client/create_channel.cc
src/cpp/client/create_channel_internal.cc src/cpp/client/create_channel_internal.cc
src/cpp/client/create_channel_posix.cc src/cpp/client/create_channel_posix.cc
src/cpp/client/global_callback_hook.cc
src/cpp/client/insecure_credentials.cc src/cpp/client/insecure_credentials.cc
src/cpp/client/secure_credentials.cc src/cpp/client/secure_credentials.cc
src/cpp/common/alarm.cc src/cpp/common/alarm.cc
@ -33267,6 +33276,7 @@ add_executable(wire_writer_test
src/cpp/client/create_channel.cc src/cpp/client/create_channel.cc
src/cpp/client/create_channel_internal.cc src/cpp/client/create_channel_internal.cc
src/cpp/client/create_channel_posix.cc src/cpp/client/create_channel_posix.cc
src/cpp/client/global_callback_hook.cc
src/cpp/client/insecure_credentials.cc src/cpp/client/insecure_credentials.cc
src/cpp/client/secure_credentials.cc src/cpp/client/secure_credentials.cc
src/cpp/common/alarm.cc src/cpp/common/alarm.cc
@ -33847,6 +33857,7 @@ add_executable(xds_client_test
${_gRPC_PROTO_GENS_DIR}/src/proto/grpc/testing/xds/v3/percent.grpc.pb.cc ${_gRPC_PROTO_GENS_DIR}/src/proto/grpc/testing/xds/v3/percent.grpc.pb.cc
${_gRPC_PROTO_GENS_DIR}/src/proto/grpc/testing/xds/v3/percent.pb.h ${_gRPC_PROTO_GENS_DIR}/src/proto/grpc/testing/xds/v3/percent.pb.h
${_gRPC_PROTO_GENS_DIR}/src/proto/grpc/testing/xds/v3/percent.grpc.pb.h ${_gRPC_PROTO_GENS_DIR}/src/proto/grpc/testing/xds/v3/percent.grpc.pb.h
src/cpp/client/global_callback_hook.cc
src/cpp/util/status.cc src/cpp/util/status.cc
test/core/xds/xds_client_test.cc test/core/xds/xds_client_test.cc
test/core/xds/xds_transport_fake.cc test/core/xds/xds_transport_fake.cc
@ -34142,6 +34153,7 @@ add_executable(xds_cluster_resource_type_test
${_gRPC_PROTO_GENS_DIR}/src/proto/grpc/testing/xds/v3/wrr_locality.grpc.pb.cc ${_gRPC_PROTO_GENS_DIR}/src/proto/grpc/testing/xds/v3/wrr_locality.grpc.pb.cc
${_gRPC_PROTO_GENS_DIR}/src/proto/grpc/testing/xds/v3/wrr_locality.pb.h ${_gRPC_PROTO_GENS_DIR}/src/proto/grpc/testing/xds/v3/wrr_locality.pb.h
${_gRPC_PROTO_GENS_DIR}/src/proto/grpc/testing/xds/v3/wrr_locality.grpc.pb.h ${_gRPC_PROTO_GENS_DIR}/src/proto/grpc/testing/xds/v3/wrr_locality.grpc.pb.h
src/cpp/client/global_callback_hook.cc
src/cpp/util/status.cc src/cpp/util/status.cc
test/core/xds/xds_cluster_resource_type_test.cc test/core/xds/xds_cluster_resource_type_test.cc
) )
@ -35138,6 +35150,7 @@ add_executable(xds_endpoint_resource_type_test
${_gRPC_PROTO_GENS_DIR}/src/proto/grpc/testing/xds/v3/percent.grpc.pb.cc ${_gRPC_PROTO_GENS_DIR}/src/proto/grpc/testing/xds/v3/percent.grpc.pb.cc
${_gRPC_PROTO_GENS_DIR}/src/proto/grpc/testing/xds/v3/percent.pb.h ${_gRPC_PROTO_GENS_DIR}/src/proto/grpc/testing/xds/v3/percent.pb.h
${_gRPC_PROTO_GENS_DIR}/src/proto/grpc/testing/xds/v3/percent.grpc.pb.h ${_gRPC_PROTO_GENS_DIR}/src/proto/grpc/testing/xds/v3/percent.grpc.pb.h
src/cpp/client/global_callback_hook.cc
src/cpp/util/status.cc src/cpp/util/status.cc
test/core/xds/xds_endpoint_resource_type_test.cc test/core/xds/xds_endpoint_resource_type_test.cc
) )

@ -3836,6 +3836,7 @@ libs:
- include/grpcpp/support/client_callback.h - include/grpcpp/support/client_callback.h
- include/grpcpp/support/client_interceptor.h - include/grpcpp/support/client_interceptor.h
- include/grpcpp/support/config.h - include/grpcpp/support/config.h
- include/grpcpp/support/global_callback_hook.h
- include/grpcpp/support/interceptor.h - include/grpcpp/support/interceptor.h
- include/grpcpp/support/message_allocator.h - include/grpcpp/support/message_allocator.h
- include/grpcpp/support/method_handler.h - include/grpcpp/support/method_handler.h
@ -3915,6 +3916,7 @@ libs:
- src/cpp/client/create_channel.cc - src/cpp/client/create_channel.cc
- src/cpp/client/create_channel_internal.cc - src/cpp/client/create_channel_internal.cc
- src/cpp/client/create_channel_posix.cc - src/cpp/client/create_channel_posix.cc
- src/cpp/client/global_callback_hook.cc
- src/cpp/client/insecure_credentials.cc - src/cpp/client/insecure_credentials.cc
- src/cpp/client/secure_credentials.cc - src/cpp/client/secure_credentials.cc
- src/cpp/client/xds_credentials.cc - src/cpp/client/xds_credentials.cc
@ -4267,6 +4269,7 @@ libs:
- include/grpcpp/support/client_callback.h - include/grpcpp/support/client_callback.h
- include/grpcpp/support/client_interceptor.h - include/grpcpp/support/client_interceptor.h
- include/grpcpp/support/config.h - include/grpcpp/support/config.h
- include/grpcpp/support/global_callback_hook.h
- include/grpcpp/support/interceptor.h - include/grpcpp/support/interceptor.h
- include/grpcpp/support/message_allocator.h - include/grpcpp/support/message_allocator.h
- include/grpcpp/support/method_handler.h - include/grpcpp/support/method_handler.h
@ -4303,6 +4306,7 @@ libs:
- src/cpp/client/create_channel.cc - src/cpp/client/create_channel.cc
- src/cpp/client/create_channel_internal.cc - src/cpp/client/create_channel_internal.cc
- src/cpp/client/create_channel_posix.cc - src/cpp/client/create_channel_posix.cc
- src/cpp/client/global_callback_hook.cc
- src/cpp/client/insecure_credentials.cc - src/cpp/client/insecure_credentials.cc
- src/cpp/common/alarm.cc - src/cpp/common/alarm.cc
- src/cpp/common/channel_arguments.cc - src/cpp/common/channel_arguments.cc
@ -6182,6 +6186,7 @@ targets:
- src/cpp/client/create_channel.cc - src/cpp/client/create_channel.cc
- src/cpp/client/create_channel_internal.cc - src/cpp/client/create_channel_internal.cc
- src/cpp/client/create_channel_posix.cc - src/cpp/client/create_channel_posix.cc
- src/cpp/client/global_callback_hook.cc
- src/cpp/client/insecure_credentials.cc - src/cpp/client/insecure_credentials.cc
- src/cpp/client/secure_credentials.cc - src/cpp/client/secure_credentials.cc
- src/cpp/common/alarm.cc - src/cpp/common/alarm.cc
@ -9668,6 +9673,7 @@ targets:
- src/cpp/client/create_channel.cc - src/cpp/client/create_channel.cc
- src/cpp/client/create_channel_internal.cc - src/cpp/client/create_channel_internal.cc
- src/cpp/client/create_channel_posix.cc - src/cpp/client/create_channel_posix.cc
- src/cpp/client/global_callback_hook.cc
- src/cpp/client/insecure_credentials.cc - src/cpp/client/insecure_credentials.cc
- src/cpp/client/secure_credentials.cc - src/cpp/client/secure_credentials.cc
- src/cpp/common/alarm.cc - src/cpp/common/alarm.cc
@ -10143,6 +10149,7 @@ targets:
- src/cpp/client/create_channel.cc - src/cpp/client/create_channel.cc
- src/cpp/client/create_channel_internal.cc - src/cpp/client/create_channel_internal.cc
- src/cpp/client/create_channel_posix.cc - src/cpp/client/create_channel_posix.cc
- src/cpp/client/global_callback_hook.cc
- src/cpp/client/insecure_credentials.cc - src/cpp/client/insecure_credentials.cc
- src/cpp/client/secure_credentials.cc - src/cpp/client/secure_credentials.cc
- src/cpp/common/alarm.cc - src/cpp/common/alarm.cc
@ -20379,6 +20386,7 @@ targets:
- src/cpp/client/create_channel.cc - src/cpp/client/create_channel.cc
- src/cpp/client/create_channel_internal.cc - src/cpp/client/create_channel_internal.cc
- src/cpp/client/create_channel_posix.cc - src/cpp/client/create_channel_posix.cc
- src/cpp/client/global_callback_hook.cc
- src/cpp/client/insecure_credentials.cc - src/cpp/client/insecure_credentials.cc
- src/cpp/client/secure_credentials.cc - src/cpp/client/secure_credentials.cc
- src/cpp/common/alarm.cc - src/cpp/common/alarm.cc
@ -20798,6 +20806,7 @@ targets:
- src/cpp/client/create_channel.cc - src/cpp/client/create_channel.cc
- src/cpp/client/create_channel_internal.cc - src/cpp/client/create_channel_internal.cc
- src/cpp/client/create_channel_posix.cc - src/cpp/client/create_channel_posix.cc
- src/cpp/client/global_callback_hook.cc
- src/cpp/client/insecure_credentials.cc - src/cpp/client/insecure_credentials.cc
- src/cpp/client/secure_credentials.cc - src/cpp/client/secure_credentials.cc
- src/cpp/common/alarm.cc - src/cpp/common/alarm.cc
@ -20909,6 +20918,7 @@ targets:
- src/cpp/client/create_channel.cc - src/cpp/client/create_channel.cc
- src/cpp/client/create_channel_internal.cc - src/cpp/client/create_channel_internal.cc
- src/cpp/client/create_channel_posix.cc - src/cpp/client/create_channel_posix.cc
- src/cpp/client/global_callback_hook.cc
- src/cpp/client/insecure_credentials.cc - src/cpp/client/insecure_credentials.cc
- src/cpp/client/secure_credentials.cc - src/cpp/client/secure_credentials.cc
- src/cpp/common/alarm.cc - src/cpp/common/alarm.cc
@ -21232,6 +21242,7 @@ targets:
- src/proto/grpc/testing/xds/v3/base.proto - src/proto/grpc/testing/xds/v3/base.proto
- src/proto/grpc/testing/xds/v3/discovery.proto - src/proto/grpc/testing/xds/v3/discovery.proto
- src/proto/grpc/testing/xds/v3/percent.proto - src/proto/grpc/testing/xds/v3/percent.proto
- src/cpp/client/global_callback_hook.cc
- src/cpp/util/status.cc - src/cpp/util/status.cc
- test/core/xds/xds_client_test.cc - test/core/xds/xds_client_test.cc
- test/core/xds/xds_transport_fake.cc - test/core/xds/xds_transport_fake.cc
@ -21329,6 +21340,7 @@ targets:
- src/proto/grpc/testing/xds/v3/tls.proto - src/proto/grpc/testing/xds/v3/tls.proto
- src/proto/grpc/testing/xds/v3/typed_struct.proto - src/proto/grpc/testing/xds/v3/typed_struct.proto
- src/proto/grpc/testing/xds/v3/wrr_locality.proto - src/proto/grpc/testing/xds/v3/wrr_locality.proto
- src/cpp/client/global_callback_hook.cc
- src/cpp/util/status.cc - src/cpp/util/status.cc
- test/core/xds/xds_cluster_resource_type_test.cc - test/core/xds/xds_cluster_resource_type_test.cc
deps: deps:
@ -21676,6 +21688,7 @@ targets:
- src/proto/grpc/testing/xds/v3/endpoint.proto - src/proto/grpc/testing/xds/v3/endpoint.proto
- src/proto/grpc/testing/xds/v3/health_check.proto - src/proto/grpc/testing/xds/v3/health_check.proto
- src/proto/grpc/testing/xds/v3/percent.proto - src/proto/grpc/testing/xds/v3/percent.proto
- src/cpp/client/global_callback_hook.cc
- src/cpp/util/status.cc - src/cpp/util/status.cc
- test/core/xds/xds_endpoint_resource_type_test.cc - test/core/xds/xds_endpoint_resource_type_test.cc
deps: deps:

2
gRPC-C++.podspec generated

@ -206,6 +206,7 @@ Pod::Spec.new do |s|
'include/grpcpp/support/client_callback.h', 'include/grpcpp/support/client_callback.h',
'include/grpcpp/support/client_interceptor.h', 'include/grpcpp/support/client_interceptor.h',
'include/grpcpp/support/config.h', 'include/grpcpp/support/config.h',
'include/grpcpp/support/global_callback_hook.h',
'include/grpcpp/support/interceptor.h', 'include/grpcpp/support/interceptor.h',
'include/grpcpp/support/message_allocator.h', 'include/grpcpp/support/message_allocator.h',
'include/grpcpp/support/method_handler.h', 'include/grpcpp/support/method_handler.h',
@ -1378,6 +1379,7 @@ Pod::Spec.new do |s|
'src/cpp/client/create_channel_internal.cc', 'src/cpp/client/create_channel_internal.cc',
'src/cpp/client/create_channel_internal.h', 'src/cpp/client/create_channel_internal.h',
'src/cpp/client/create_channel_posix.cc', 'src/cpp/client/create_channel_posix.cc',
'src/cpp/client/global_callback_hook.cc',
'src/cpp/client/insecure_credentials.cc', 'src/cpp/client/insecure_credentials.cc',
'src/cpp/client/secure_credentials.cc', 'src/cpp/client/secure_credentials.cc',
'src/cpp/client/secure_credentials.h', 'src/cpp/client/secure_credentials.h',

@ -30,6 +30,7 @@
#include <grpcpp/impl/codegen/channel_interface.h> #include <grpcpp/impl/codegen/channel_interface.h>
#include <grpcpp/impl/completion_queue_tag.h> #include <grpcpp/impl/completion_queue_tag.h>
#include <grpcpp/support/config.h> #include <grpcpp/support/config.h>
#include <grpcpp/support/global_callback_hook.h>
#include <grpcpp/support/status.h> #include <grpcpp/support/status.h>
namespace grpc { namespace grpc {
@ -127,7 +128,18 @@ class CallbackWithStatusTag : public grpc_completion_queue_functor {
auto status = std::move(status_); auto status = std::move(status_);
func_ = nullptr; // reset to clear this out for sure func_ = nullptr; // reset to clear this out for sure
status_ = Status(); // reset to clear this out for sure status_ = Status(); // reset to clear this out for sure
CatchingCallback(std::move(func), std::move(status)); GetGlobalCallbackHook()->RunCallback(
call_, [func = std::move(func), status = std::move(status)]() {
#if GRPC_ALLOW_EXCEPTIONS
try {
func(status);
} catch (...) {
// nothing to return or change here, just don't crash the library
}
#else // GRPC_ALLOW_EXCEPTIONS
func(status);
#endif // GRPC_ALLOW_EXCEPTIONS
});
grpc_call_unref(call_); grpc_call_unref(call_);
} }
}; };
@ -214,7 +226,17 @@ class CallbackWithSuccessTag : public grpc_completion_queue_functor {
#endif #endif
if (do_callback) { if (do_callback) {
CatchingCallback(func_, ok); GetGlobalCallbackHook()->RunCallback(call_, [this, ok]() {
#if GRPC_ALLOW_EXCEPTIONS
try {
func_(ok);
} catch (...) {
// nothing to return or change here, just don't crash the library
}
#else // GRPC_ALLOW_EXCEPTIONS
func_(ok);
#endif // GRPC_ALLOW_EXCEPTIONS
});
} }
} }
}; };

@ -0,0 +1,58 @@
// Copyright 2024 gRPC authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef GRPCPP_SUPPORT_GLOBAL_CALLBACK_HOOK_H
#define GRPCPP_SUPPORT_GLOBAL_CALLBACK_HOOK_H
#include "absl/functional/function_ref.h"
struct grpc_call;
namespace grpc {
class GlobalCallbackHook {
public:
virtual ~GlobalCallbackHook() = default;
virtual void RunCallback(grpc_call* call,
absl::FunctionRef<void()> callback) = 0;
protected:
// An exception-safe way of invoking a user-specified callback function.
template <class Func, class... Args>
void CatchingCallback(Func&& func, Args&&... args) {
#if GRPC_ALLOW_EXCEPTIONS
try {
func(std::forward<Args>(args)...);
} catch (...) {
// nothing to return or change here, just don't crash the library
}
#else // GRPC_ALLOW_EXCEPTIONS
func(std::forward<Args>(args)...);
#endif // GRPC_ALLOW_EXCEPTIONS
}
};
class DefaultGlobalCallbackHook final : public GlobalCallbackHook {
public:
void RunCallback(grpc_call* call,
absl::FunctionRef<void()> callback) override {
CatchingCallback(callback);
}
};
std::shared_ptr<GlobalCallbackHook> GetGlobalCallbackHook();
void SetGlobalCallbackHook(GlobalCallbackHook* hook);
} // namespace grpc
#endif // GRPCPP_SUPPORT_GLOBAL_CALLBACK_HOOK_H

@ -0,0 +1,36 @@
// Copyright 2024 gRPC authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <memory>
#include "absl/base/no_destructor.h"
#include "absl/log/check.h"
#include <grpcpp/support/global_callback_hook.h>
namespace grpc {
static absl::NoDestructor<std::shared_ptr<GlobalCallbackHook>> g_callback_hook(
std::make_shared<DefaultGlobalCallbackHook>());
std::shared_ptr<GlobalCallbackHook> GetGlobalCallbackHook() {
return *g_callback_hook;
}
void SetGlobalCallbackHook(GlobalCallbackHook* hook) {
CHECK(hook != nullptr);
CHECK(hook != (*g_callback_hook).get());
*g_callback_hook = std::shared_ptr<GlobalCallbackHook>(hook);
}
} // namespace grpc

@ -1073,6 +1073,7 @@ include/grpcpp/support/channel_arguments.h \
include/grpcpp/support/client_callback.h \ include/grpcpp/support/client_callback.h \
include/grpcpp/support/client_interceptor.h \ include/grpcpp/support/client_interceptor.h \
include/grpcpp/support/config.h \ include/grpcpp/support/config.h \
include/grpcpp/support/global_callback_hook.h \
include/grpcpp/support/interceptor.h \ include/grpcpp/support/interceptor.h \
include/grpcpp/support/message_allocator.h \ include/grpcpp/support/message_allocator.h \
include/grpcpp/support/method_handler.h \ include/grpcpp/support/method_handler.h \

@ -1073,6 +1073,7 @@ include/grpcpp/support/channel_arguments.h \
include/grpcpp/support/client_callback.h \ include/grpcpp/support/client_callback.h \
include/grpcpp/support/client_interceptor.h \ include/grpcpp/support/client_interceptor.h \
include/grpcpp/support/config.h \ include/grpcpp/support/config.h \
include/grpcpp/support/global_callback_hook.h \
include/grpcpp/support/interceptor.h \ include/grpcpp/support/interceptor.h \
include/grpcpp/support/message_allocator.h \ include/grpcpp/support/message_allocator.h \
include/grpcpp/support/method_handler.h \ include/grpcpp/support/method_handler.h \
@ -3046,6 +3047,7 @@ src/cpp/client/create_channel.cc \
src/cpp/client/create_channel_internal.cc \ src/cpp/client/create_channel_internal.cc \
src/cpp/client/create_channel_internal.h \ src/cpp/client/create_channel_internal.h \
src/cpp/client/create_channel_posix.cc \ src/cpp/client/create_channel_posix.cc \
src/cpp/client/global_callback_hook.cc \
src/cpp/client/insecure_credentials.cc \ src/cpp/client/insecure_credentials.cc \
src/cpp/client/secure_credentials.cc \ src/cpp/client/secure_credentials.cc \
src/cpp/client/secure_credentials.h \ src/cpp/client/secure_credentials.h \

Loading…
Cancel
Save