Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 3 | * Copyright 2015 gRPC authors. |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 4 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 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 |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 8 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 10 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 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. |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 16 | * |
| 17 | */ |
| 18 | |
yang-g | 8c2be9f | 2015-08-19 16:28:09 -0700 | [diff] [blame] | 19 | #include <grpc++/channel.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 20 | |
Yuchen Zeng | 5150cbd | 2017-08-29 00:51:49 -0700 | [diff] [blame] | 21 | #include <chrono> |
| 22 | #include <condition_variable> |
yangg | 59dfc90 | 2014-12-19 14:00:14 -0800 | [diff] [blame] | 23 | #include <memory> |
Yuchen Zeng | 5150cbd | 2017-08-29 00:51:49 -0700 | [diff] [blame] | 24 | #include <mutex> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 25 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 26 | #include <grpc++/client_context.h> |
Craig Tiller | 5095071 | 2015-02-09 10:38:38 -0800 | [diff] [blame] | 27 | #include <grpc++/completion_queue.h> |
Craig Tiller | 20f4af2 | 2015-02-10 09:52:15 -0800 | [diff] [blame] | 28 | #include <grpc++/impl/call.h> |
David Garcia Quintas | 08a0a33 | 2016-01-21 01:04:36 -0800 | [diff] [blame] | 29 | #include <grpc++/impl/codegen/completion_queue_tag.h> |
David Garcia Quintas | e1300de | 2016-01-27 18:41:26 -0800 | [diff] [blame] | 30 | #include <grpc++/impl/grpc_library.h> |
| 31 | #include <grpc++/impl/rpc_method.h> |
| 32 | #include <grpc++/security/credentials.h> |
yang-g | 9e2f90c | 2015-08-21 15:35:03 -0700 | [diff] [blame] | 33 | #include <grpc++/support/channel_arguments.h> |
| 34 | #include <grpc++/support/config.h> |
| 35 | #include <grpc++/support/status.h> |
| 36 | #include <grpc++/support/time.h> |
David Garcia Quintas | e1300de | 2016-01-27 18:41:26 -0800 | [diff] [blame] | 37 | #include <grpc/grpc.h> |
Craig Tiller | b37d53e | 2016-10-26 16:16:35 -0700 | [diff] [blame] | 38 | #include <grpc/slice.h> |
Mark D. Roth | 4bbdda4 | 2016-11-09 14:40:22 -0800 | [diff] [blame] | 39 | #include <grpc/support/alloc.h> |
Craig Tiller | 28b7242 | 2016-10-26 21:15:29 -0700 | [diff] [blame] | 40 | #include <grpc/support/log.h> |
Yuchen Zeng | bfb4e06 | 2017-08-23 23:34:32 -0700 | [diff] [blame] | 41 | #include <grpc/support/sync.h> |
Yuchen Zeng | be9b814 | 2017-08-04 10:42:03 -0700 | [diff] [blame] | 42 | #include <grpc/support/thd.h> |
Yuchen Zeng | bfb4e06 | 2017-08-23 23:34:32 -0700 | [diff] [blame] | 43 | #include <grpc/support/time.h> |
Yuchen Zeng | f1d5098 | 2017-08-24 12:08:03 -0700 | [diff] [blame] | 44 | #include <grpc/support/useful.h> |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 45 | #include "src/core/lib/profiling/timers.h" |
Yuchen Zeng | f1d5098 | 2017-08-24 12:08:03 -0700 | [diff] [blame] | 46 | #include "src/core/lib/support/env.h" |
Yuchen Zeng | ddaef3e | 2017-08-24 15:34:52 -0700 | [diff] [blame] | 47 | #include "src/core/lib/support/string.h" |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 48 | |
| 49 | namespace grpc { |
| 50 | |
David Garcia Quintas | d79ef3a | 2016-01-28 00:21:27 -0800 | [diff] [blame] | 51 | static internal::GrpcLibraryInitializer g_gli_initializer; |
yang-g | d556da9 | 2015-07-31 15:59:04 -0700 | [diff] [blame] | 52 | Channel::Channel(const grpc::string& host, grpc_channel* channel) |
Yuchen Zeng | b4481a9 | 2017-08-24 03:07:01 -0700 | [diff] [blame] | 53 | : host_(host), c_channel_(channel) { |
David Garcia Quintas | d79ef3a | 2016-01-28 00:21:27 -0800 | [diff] [blame] | 54 | g_gli_initializer.summon(); |
David Garcia Quintas | e1300de | 2016-01-27 18:41:26 -0800 | [diff] [blame] | 55 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 56 | |
Yuchen Zeng | 6a48dc5 | 2017-09-26 19:49:23 -0700 | [diff] [blame] | 57 | Channel::~Channel() { grpc_channel_destroy(c_channel_); } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 58 | |
Mark D. Roth | 4bbdda4 | 2016-11-09 14:40:22 -0800 | [diff] [blame] | 59 | namespace { |
| 60 | |
| 61 | grpc::string GetChannelInfoField(grpc_channel* channel, |
| 62 | grpc_channel_info* channel_info, |
| 63 | char*** channel_info_field) { |
Craig Tiller | 4ac2b8e | 2017-11-10 14:14:17 -0800 | [diff] [blame] | 64 | char* value = nullptr; |
Mark D. Roth | 4bbdda4 | 2016-11-09 14:40:22 -0800 | [diff] [blame] | 65 | memset(channel_info, 0, sizeof(*channel_info)); |
| 66 | *channel_info_field = &value; |
| 67 | grpc_channel_get_info(channel, channel_info); |
Craig Tiller | 4ac2b8e | 2017-11-10 14:14:17 -0800 | [diff] [blame] | 68 | if (value == nullptr) return ""; |
Mark D. Roth | 4bbdda4 | 2016-11-09 14:40:22 -0800 | [diff] [blame] | 69 | grpc::string result = value; |
| 70 | gpr_free(value); |
| 71 | return result; |
| 72 | } |
| 73 | |
| 74 | } // namespace |
| 75 | |
| 76 | grpc::string Channel::GetLoadBalancingPolicyName() const { |
| 77 | grpc_channel_info channel_info; |
| 78 | return GetChannelInfoField(c_channel_, &channel_info, |
| 79 | &channel_info.lb_policy_name); |
| 80 | } |
| 81 | |
| 82 | grpc::string Channel::GetServiceConfigJSON() const { |
| 83 | grpc_channel_info channel_info; |
| 84 | return GetChannelInfoField(c_channel_, &channel_info, |
| 85 | &channel_info.service_config_json); |
| 86 | } |
| 87 | |
Vijay Pai | 06e174a | 2017-10-20 05:51:12 -0700 | [diff] [blame] | 88 | internal::Call Channel::CreateCall(const internal::RpcMethod& method, |
| 89 | ClientContext* context, |
| 90 | CompletionQueue* cq) { |
yang-g | 37ce034 | 2015-08-12 11:46:26 -0700 | [diff] [blame] | 91 | const bool kRegistered = method.channel_tag() && context->authority().empty(); |
Craig Tiller | 4ac2b8e | 2017-11-10 14:14:17 -0800 | [diff] [blame] | 92 | grpc_call* c_call = nullptr; |
yang-g | 37ce034 | 2015-08-12 11:46:26 -0700 | [diff] [blame] | 93 | if (kRegistered) { |
yang-g | 0c034a0 | 2015-08-11 11:46:32 -0700 | [diff] [blame] | 94 | c_call = grpc_channel_create_registered_call( |
| 95 | c_channel_, context->propagate_from_call_, |
| 96 | context->propagation_options_.c_bitmask(), cq->cq(), |
Nicolas "Pixel" Noble | d53b389 | 2015-08-13 19:20:39 +0200 | [diff] [blame] | 97 | method.channel_tag(), context->raw_deadline(), nullptr); |
yang-g | 0c034a0 | 2015-08-11 11:46:32 -0700 | [diff] [blame] | 98 | } else { |
Craig Tiller | 4ac2b8e | 2017-11-10 14:14:17 -0800 | [diff] [blame] | 99 | const char* host_str = nullptr; |
yang-g | 0c034a0 | 2015-08-11 11:46:32 -0700 | [diff] [blame] | 100 | if (!context->authority().empty()) { |
yang-g | 2f543f2 | 2015-08-19 15:14:17 -0700 | [diff] [blame] | 101 | host_str = context->authority_.c_str(); |
yang-g | 0c034a0 | 2015-08-11 11:46:32 -0700 | [diff] [blame] | 102 | } else if (!host_.empty()) { |
| 103 | host_str = host_.c_str(); |
| 104 | } |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 105 | grpc_slice method_slice = SliceFromCopiedString(method.name()); |
| 106 | grpc_slice host_slice; |
| 107 | if (host_str != nullptr) { |
| 108 | host_slice = SliceFromCopiedString(host_str); |
| 109 | } |
| 110 | c_call = grpc_channel_create_call( |
| 111 | c_channel_, context->propagate_from_call_, |
| 112 | context->propagation_options_.c_bitmask(), cq->cq(), method_slice, |
| 113 | host_str == nullptr ? nullptr : &host_slice, context->raw_deadline(), |
| 114 | nullptr); |
| 115 | grpc_slice_unref(method_slice); |
| 116 | if (host_str != nullptr) { |
| 117 | grpc_slice_unref(host_slice); |
| 118 | } |
yang-g | 0c034a0 | 2015-08-11 11:46:32 -0700 | [diff] [blame] | 119 | } |
yang-g | 9a009f2 | 2015-07-29 11:38:18 -0700 | [diff] [blame] | 120 | grpc_census_call_set_context(c_call, context->census_context()); |
Craig Tiller | 1fb9955 | 2015-04-27 08:55:08 -0700 | [diff] [blame] | 121 | context->set_call(c_call, shared_from_this()); |
Vijay Pai | 06e174a | 2017-10-20 05:51:12 -0700 | [diff] [blame] | 122 | return internal::Call(c_call, this, cq); |
Craig Tiller | 5095071 | 2015-02-09 10:38:38 -0800 | [diff] [blame] | 123 | } |
| 124 | |
Vijay Pai | 06e174a | 2017-10-20 05:51:12 -0700 | [diff] [blame] | 125 | void Channel::PerformOpsOnCall(internal::CallOpSetInterface* ops, |
| 126 | internal::Call* call) { |
Craig Tiller | 5095071 | 2015-02-09 10:38:38 -0800 | [diff] [blame] | 127 | static const size_t MAX_OPS = 8; |
Craig Tiller | 50a7a68 | 2015-06-04 12:53:40 -0700 | [diff] [blame] | 128 | size_t nops = 0; |
| 129 | grpc_op cops[MAX_OPS]; |
Craig Tiller | 66051c6 | 2017-03-31 09:16:35 -0700 | [diff] [blame] | 130 | ops->FillOps(call->call(), cops, &nops); |
Craig Tiller | 5095071 | 2015-02-09 10:38:38 -0800 | [diff] [blame] | 131 | GPR_ASSERT(GRPC_CALL_OK == |
Nicolas "Pixel" Noble | ebb5140 | 2015-07-23 02:41:33 +0200 | [diff] [blame] | 132 | grpc_call_start_batch(call->call(), cops, nops, ops, nullptr)); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 133 | } |
| 134 | |
Craig Tiller | 277d3cf | 2015-04-14 14:04:51 -0700 | [diff] [blame] | 135 | void* Channel::RegisterMethod(const char* method) { |
Craig Tiller | d6c98df | 2015-08-18 09:33:44 -0700 | [diff] [blame] | 136 | return grpc_channel_register_call( |
Craig Tiller | 4ac2b8e | 2017-11-10 14:14:17 -0800 | [diff] [blame] | 137 | c_channel_, method, host_.empty() ? nullptr : host_.c_str(), nullptr); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 138 | } |
| 139 | |
yang-g | a73dc1c | 2015-08-05 14:24:00 -0700 | [diff] [blame] | 140 | grpc_connectivity_state Channel::GetState(bool try_to_connect) { |
| 141 | return grpc_channel_check_connectivity_state(c_channel_, try_to_connect); |
| 142 | } |
| 143 | |
Yuchen Zeng | 6a48dc5 | 2017-09-26 19:49:23 -0700 | [diff] [blame] | 144 | namespace { |
| 145 | |
Yuchen Zeng | b16df75 | 2017-10-30 12:09:16 -0700 | [diff] [blame] | 146 | class TagSaver final : public internal::CompletionQueueTag { |
Yuchen Zeng | 6a48dc5 | 2017-09-26 19:49:23 -0700 | [diff] [blame] | 147 | public: |
| 148 | explicit TagSaver(void* tag) : tag_(tag) {} |
| 149 | ~TagSaver() override {} |
| 150 | bool FinalizeResult(void** tag, bool* status) override { |
| 151 | *tag = tag_; |
| 152 | delete this; |
| 153 | return true; |
| 154 | } |
| 155 | |
| 156 | private: |
| 157 | void* tag_; |
| 158 | }; |
| 159 | |
| 160 | } // namespace |
| 161 | |
yang-g | c8abca8 | 2015-08-06 22:50:16 -0700 | [diff] [blame] | 162 | void Channel::NotifyOnStateChangeImpl(grpc_connectivity_state last_observed, |
| 163 | gpr_timespec deadline, |
| 164 | CompletionQueue* cq, void* tag) { |
yang-g | 36f5965 | 2015-08-05 15:15:18 -0700 | [diff] [blame] | 165 | TagSaver* tag_saver = new TagSaver(tag); |
yang-g | c8abca8 | 2015-08-06 22:50:16 -0700 | [diff] [blame] | 166 | grpc_channel_watch_connectivity_state(c_channel_, last_observed, deadline, |
| 167 | cq->cq(), tag_saver); |
yang-g | 36f5965 | 2015-08-05 15:15:18 -0700 | [diff] [blame] | 168 | } |
| 169 | |
yang-g | c8abca8 | 2015-08-06 22:50:16 -0700 | [diff] [blame] | 170 | bool Channel::WaitForStateChangeImpl(grpc_connectivity_state last_observed, |
| 171 | gpr_timespec deadline) { |
yang-g | 36f5965 | 2015-08-05 15:15:18 -0700 | [diff] [blame] | 172 | CompletionQueue cq; |
| 173 | bool ok = false; |
Craig Tiller | 4ac2b8e | 2017-11-10 14:14:17 -0800 | [diff] [blame] | 174 | void* tag = nullptr; |
| 175 | NotifyOnStateChangeImpl(last_observed, deadline, &cq, nullptr); |
yang-g | 36f5965 | 2015-08-05 15:15:18 -0700 | [diff] [blame] | 176 | cq.Next(&tag, &ok); |
Craig Tiller | 4ac2b8e | 2017-11-10 14:14:17 -0800 | [diff] [blame] | 177 | GPR_ASSERT(tag == nullptr); |
yang-g | 36f5965 | 2015-08-05 15:15:18 -0700 | [diff] [blame] | 178 | return ok; |
| 179 | } |
| 180 | |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 181 | } // namespace grpc |