channel_cc.cc 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. *
  3. * Copyright 2015 gRPC authors.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. #include <grpcpp/channel.h>
  19. #include <chrono>
  20. #include <condition_variable>
  21. #include <memory>
  22. #include <mutex>
  23. #include <grpc/grpc.h>
  24. #include <grpc/slice.h>
  25. #include <grpc/support/alloc.h>
  26. #include <grpc/support/log.h>
  27. #include <grpc/support/sync.h>
  28. #include <grpc/support/time.h>
  29. #include <grpcpp/client_context.h>
  30. #include <grpcpp/completion_queue.h>
  31. #include <grpcpp/impl/call.h>
  32. #include <grpcpp/impl/codegen/completion_queue_tag.h>
  33. #include <grpcpp/impl/grpc_library.h>
  34. #include <grpcpp/impl/rpc_method.h>
  35. #include <grpcpp/security/credentials.h>
  36. #include <grpcpp/support/channel_arguments.h>
  37. #include <grpcpp/support/config.h>
  38. #include <grpcpp/support/status.h>
  39. #include <grpcpp/support/time.h>
  40. #include "src/core/lib/gpr/env.h"
  41. #include "src/core/lib/gpr/string.h"
  42. #include "src/core/lib/gprpp/thd.h"
  43. #include "src/core/lib/profiling/timers.h"
  44. namespace grpc {
  45. static internal::GrpcLibraryInitializer g_gli_initializer;
  46. Channel::Channel(const grpc::string& host, grpc_channel* channel)
  47. : host_(host), c_channel_(channel) {
  48. g_gli_initializer.summon();
  49. }
  50. Channel::~Channel() { grpc_channel_destroy(c_channel_); }
  51. namespace {
  52. grpc::string GetChannelInfoField(grpc_channel* channel,
  53. grpc_channel_info* channel_info,
  54. char*** channel_info_field) {
  55. char* value = nullptr;
  56. memset(channel_info, 0, sizeof(*channel_info));
  57. *channel_info_field = &value;
  58. grpc_channel_get_info(channel, channel_info);
  59. if (value == nullptr) return "";
  60. grpc::string result = value;
  61. gpr_free(value);
  62. return result;
  63. }
  64. } // namespace
  65. grpc::string Channel::GetLoadBalancingPolicyName() const {
  66. grpc_channel_info channel_info;
  67. return GetChannelInfoField(c_channel_, &channel_info,
  68. &channel_info.lb_policy_name);
  69. }
  70. grpc::string Channel::GetServiceConfigJSON() const {
  71. grpc_channel_info channel_info;
  72. return GetChannelInfoField(c_channel_, &channel_info,
  73. &channel_info.service_config_json);
  74. }
  75. internal::Call Channel::CreateCall(const internal::RpcMethod& method,
  76. ClientContext* context,
  77. CompletionQueue* cq) {
  78. const bool kRegistered = method.channel_tag() && context->authority().empty();
  79. grpc_call* c_call = nullptr;
  80. if (kRegistered) {
  81. c_call = grpc_channel_create_registered_call(
  82. c_channel_, context->propagate_from_call_,
  83. context->propagation_options_.c_bitmask(), cq->cq(),
  84. method.channel_tag(), context->raw_deadline(), nullptr);
  85. } else {
  86. const char* host_str = nullptr;
  87. if (!context->authority().empty()) {
  88. host_str = context->authority_.c_str();
  89. } else if (!host_.empty()) {
  90. host_str = host_.c_str();
  91. }
  92. grpc_slice method_slice = SliceFromCopiedString(method.name());
  93. grpc_slice host_slice;
  94. if (host_str != nullptr) {
  95. host_slice = SliceFromCopiedString(host_str);
  96. }
  97. c_call = grpc_channel_create_call(
  98. c_channel_, context->propagate_from_call_,
  99. context->propagation_options_.c_bitmask(), cq->cq(), method_slice,
  100. host_str == nullptr ? nullptr : &host_slice, context->raw_deadline(),
  101. nullptr);
  102. grpc_slice_unref(method_slice);
  103. if (host_str != nullptr) {
  104. grpc_slice_unref(host_slice);
  105. }
  106. }
  107. grpc_census_call_set_context(c_call, context->census_context());
  108. context->set_call(c_call, shared_from_this());
  109. return internal::Call(c_call, this, cq);
  110. }
  111. void Channel::PerformOpsOnCall(internal::CallOpSetInterface* ops,
  112. internal::Call* call) {
  113. static const size_t MAX_OPS = 8;
  114. size_t nops = 0;
  115. grpc_op cops[MAX_OPS];
  116. ops->FillOps(call->call(), cops, &nops);
  117. GPR_ASSERT(GRPC_CALL_OK ==
  118. grpc_call_start_batch(call->call(), cops, nops, ops, nullptr));
  119. }
  120. void* Channel::RegisterMethod(const char* method) {
  121. return grpc_channel_register_call(
  122. c_channel_, method, host_.empty() ? nullptr : host_.c_str(), nullptr);
  123. }
  124. grpc_connectivity_state Channel::GetState(bool try_to_connect) {
  125. return grpc_channel_check_connectivity_state(c_channel_, try_to_connect);
  126. }
  127. namespace {
  128. class TagSaver final : public internal::CompletionQueueTag {
  129. public:
  130. explicit TagSaver(void* tag) : tag_(tag) {}
  131. ~TagSaver() override {}
  132. bool FinalizeResult(void** tag, bool* status) override {
  133. *tag = tag_;
  134. delete this;
  135. return true;
  136. }
  137. private:
  138. void* tag_;
  139. };
  140. } // namespace
  141. void Channel::NotifyOnStateChangeImpl(grpc_connectivity_state last_observed,
  142. gpr_timespec deadline,
  143. CompletionQueue* cq, void* tag) {
  144. TagSaver* tag_saver = new TagSaver(tag);
  145. grpc_channel_watch_connectivity_state(c_channel_, last_observed, deadline,
  146. cq->cq(), tag_saver);
  147. }
  148. bool Channel::WaitForStateChangeImpl(grpc_connectivity_state last_observed,
  149. gpr_timespec deadline) {
  150. CompletionQueue cq;
  151. bool ok = false;
  152. void* tag = nullptr;
  153. NotifyOnStateChangeImpl(last_observed, deadline, &cq, nullptr);
  154. cq.Next(&tag, &ok);
  155. GPR_ASSERT(tag == nullptr);
  156. return ok;
  157. }
  158. } // namespace grpc