blob: 08420817c87b215e56b9e3971608cd6a78e2c0e3 [file] [log] [blame]
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001/*
2 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02003 * Copyright 2015 gRPC authors.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08004 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02005 * 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 Nobleb7ebd3b2014-11-26 16:33:03 -08008 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02009 * http://www.apache.org/licenses/LICENSE-2.0
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080010 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +020011 * 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 Nobleb7ebd3b2014-11-26 16:33:03 -080016 *
17 */
18
yang-g8c2be9f2015-08-19 16:28:09 -070019#include <grpc++/channel.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080020
Yuchen Zeng5150cbd2017-08-29 00:51:49 -070021#include <chrono>
22#include <condition_variable>
yangg59dfc902014-12-19 14:00:14 -080023#include <memory>
Yuchen Zeng5150cbd2017-08-29 00:51:49 -070024#include <mutex>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080025
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080026#include <grpc++/client_context.h>
Craig Tiller50950712015-02-09 10:38:38 -080027#include <grpc++/completion_queue.h>
Craig Tiller20f4af22015-02-10 09:52:15 -080028#include <grpc++/impl/call.h>
David Garcia Quintas08a0a332016-01-21 01:04:36 -080029#include <grpc++/impl/codegen/completion_queue_tag.h>
David Garcia Quintase1300de2016-01-27 18:41:26 -080030#include <grpc++/impl/grpc_library.h>
31#include <grpc++/impl/rpc_method.h>
32#include <grpc++/security/credentials.h>
yang-g9e2f90c2015-08-21 15:35:03 -070033#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 Quintase1300de2016-01-27 18:41:26 -080037#include <grpc/grpc.h>
Craig Tillerb37d53e2016-10-26 16:16:35 -070038#include <grpc/slice.h>
Mark D. Roth4bbdda42016-11-09 14:40:22 -080039#include <grpc/support/alloc.h>
Craig Tiller28b72422016-10-26 21:15:29 -070040#include <grpc/support/log.h>
Yuchen Zengbfb4e062017-08-23 23:34:32 -070041#include <grpc/support/sync.h>
Yuchen Zengbe9b8142017-08-04 10:42:03 -070042#include <grpc/support/thd.h>
Yuchen Zengbfb4e062017-08-23 23:34:32 -070043#include <grpc/support/time.h>
Yuchen Zengf1d50982017-08-24 12:08:03 -070044#include <grpc/support/useful.h>
Craig Tiller9533d042016-03-25 17:11:06 -070045#include "src/core/lib/profiling/timers.h"
Yuchen Zengf1d50982017-08-24 12:08:03 -070046#include "src/core/lib/support/env.h"
Yuchen Zengddaef3e2017-08-24 15:34:52 -070047#include "src/core/lib/support/string.h"
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080048
49namespace grpc {
50
David Garcia Quintasd79ef3a2016-01-28 00:21:27 -080051static internal::GrpcLibraryInitializer g_gli_initializer;
yang-gd556da92015-07-31 15:59:04 -070052Channel::Channel(const grpc::string& host, grpc_channel* channel)
Yuchen Zengb4481a92017-08-24 03:07:01 -070053 : host_(host), c_channel_(channel) {
David Garcia Quintasd79ef3a2016-01-28 00:21:27 -080054 g_gli_initializer.summon();
David Garcia Quintase1300de2016-01-27 18:41:26 -080055}
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080056
Yuchen Zeng6a48dc52017-09-26 19:49:23 -070057Channel::~Channel() { grpc_channel_destroy(c_channel_); }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080058
Mark D. Roth4bbdda42016-11-09 14:40:22 -080059namespace {
60
61grpc::string GetChannelInfoField(grpc_channel* channel,
62 grpc_channel_info* channel_info,
63 char*** channel_info_field) {
Craig Tiller4ac2b8e2017-11-10 14:14:17 -080064 char* value = nullptr;
Mark D. Roth4bbdda42016-11-09 14:40:22 -080065 memset(channel_info, 0, sizeof(*channel_info));
66 *channel_info_field = &value;
67 grpc_channel_get_info(channel, channel_info);
Craig Tiller4ac2b8e2017-11-10 14:14:17 -080068 if (value == nullptr) return "";
Mark D. Roth4bbdda42016-11-09 14:40:22 -080069 grpc::string result = value;
70 gpr_free(value);
71 return result;
72}
73
74} // namespace
75
76grpc::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
82grpc::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 Pai06e174a2017-10-20 05:51:12 -070088internal::Call Channel::CreateCall(const internal::RpcMethod& method,
89 ClientContext* context,
90 CompletionQueue* cq) {
yang-g37ce0342015-08-12 11:46:26 -070091 const bool kRegistered = method.channel_tag() && context->authority().empty();
Craig Tiller4ac2b8e2017-11-10 14:14:17 -080092 grpc_call* c_call = nullptr;
yang-g37ce0342015-08-12 11:46:26 -070093 if (kRegistered) {
yang-g0c034a02015-08-11 11:46:32 -070094 c_call = grpc_channel_create_registered_call(
95 c_channel_, context->propagate_from_call_,
96 context->propagation_options_.c_bitmask(), cq->cq(),
Nicolas "Pixel" Nobled53b3892015-08-13 19:20:39 +020097 method.channel_tag(), context->raw_deadline(), nullptr);
yang-g0c034a02015-08-11 11:46:32 -070098 } else {
Craig Tiller4ac2b8e2017-11-10 14:14:17 -080099 const char* host_str = nullptr;
yang-g0c034a02015-08-11 11:46:32 -0700100 if (!context->authority().empty()) {
yang-g2f543f22015-08-19 15:14:17 -0700101 host_str = context->authority_.c_str();
yang-g0c034a02015-08-11 11:46:32 -0700102 } else if (!host_.empty()) {
103 host_str = host_.c_str();
104 }
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800105 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-g0c034a02015-08-11 11:46:32 -0700119 }
yang-g9a009f22015-07-29 11:38:18 -0700120 grpc_census_call_set_context(c_call, context->census_context());
Craig Tiller1fb99552015-04-27 08:55:08 -0700121 context->set_call(c_call, shared_from_this());
Vijay Pai06e174a2017-10-20 05:51:12 -0700122 return internal::Call(c_call, this, cq);
Craig Tiller50950712015-02-09 10:38:38 -0800123}
124
Vijay Pai06e174a2017-10-20 05:51:12 -0700125void Channel::PerformOpsOnCall(internal::CallOpSetInterface* ops,
126 internal::Call* call) {
Craig Tiller50950712015-02-09 10:38:38 -0800127 static const size_t MAX_OPS = 8;
Craig Tiller50a7a682015-06-04 12:53:40 -0700128 size_t nops = 0;
129 grpc_op cops[MAX_OPS];
Craig Tiller66051c62017-03-31 09:16:35 -0700130 ops->FillOps(call->call(), cops, &nops);
Craig Tiller50950712015-02-09 10:38:38 -0800131 GPR_ASSERT(GRPC_CALL_OK ==
Nicolas "Pixel" Nobleebb51402015-07-23 02:41:33 +0200132 grpc_call_start_batch(call->call(), cops, nops, ops, nullptr));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800133}
134
Craig Tiller277d3cf2015-04-14 14:04:51 -0700135void* Channel::RegisterMethod(const char* method) {
Craig Tillerd6c98df2015-08-18 09:33:44 -0700136 return grpc_channel_register_call(
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800137 c_channel_, method, host_.empty() ? nullptr : host_.c_str(), nullptr);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800138}
139
yang-ga73dc1c2015-08-05 14:24:00 -0700140grpc_connectivity_state Channel::GetState(bool try_to_connect) {
141 return grpc_channel_check_connectivity_state(c_channel_, try_to_connect);
142}
143
Yuchen Zeng6a48dc52017-09-26 19:49:23 -0700144namespace {
145
Yuchen Zengb16df752017-10-30 12:09:16 -0700146class TagSaver final : public internal::CompletionQueueTag {
Yuchen Zeng6a48dc52017-09-26 19:49:23 -0700147 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-gc8abca82015-08-06 22:50:16 -0700162void Channel::NotifyOnStateChangeImpl(grpc_connectivity_state last_observed,
163 gpr_timespec deadline,
164 CompletionQueue* cq, void* tag) {
yang-g36f59652015-08-05 15:15:18 -0700165 TagSaver* tag_saver = new TagSaver(tag);
yang-gc8abca82015-08-06 22:50:16 -0700166 grpc_channel_watch_connectivity_state(c_channel_, last_observed, deadline,
167 cq->cq(), tag_saver);
yang-g36f59652015-08-05 15:15:18 -0700168}
169
yang-gc8abca82015-08-06 22:50:16 -0700170bool Channel::WaitForStateChangeImpl(grpc_connectivity_state last_observed,
171 gpr_timespec deadline) {
yang-g36f59652015-08-05 15:15:18 -0700172 CompletionQueue cq;
173 bool ok = false;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800174 void* tag = nullptr;
175 NotifyOnStateChangeImpl(last_observed, deadline, &cq, nullptr);
yang-g36f59652015-08-05 15:15:18 -0700176 cq.Next(&tag, &ok);
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800177 GPR_ASSERT(tag == nullptr);
yang-g36f59652015-08-05 15:15:18 -0700178 return ok;
179}
180
Craig Tiller190d3602015-02-18 09:23:38 -0800181} // namespace grpc