• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // Copyright 2015 gRPC authors.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //     http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 
17 #include "src/core/lib/surface/channel_create.h"
18 
19 #include <grpc/grpc.h>
20 #include <grpc/impl/channel_arg_names.h>
21 #include <grpc/support/port_platform.h>
22 
23 #include "absl/log/check.h"
24 #include "src/core/channelz/channelz.h"
25 #include "src/core/client_channel/client_channel.h"
26 #include "src/core/client_channel/direct_channel.h"
27 #include "src/core/config/core_configuration.h"
28 #include "src/core/lib/channel/channel_args.h"
29 #include "src/core/lib/experiments/experiments.h"
30 #include "src/core/lib/surface/channel.h"
31 #include "src/core/lib/surface/channel_stack_type.h"
32 #include "src/core/lib/surface/lame_client.h"
33 #include "src/core/lib/surface/legacy_channel.h"
34 #include "src/core/telemetry/stats.h"
35 #include "src/core/telemetry/stats_data.h"
36 
37 namespace grpc_core {
38 
ChannelCreate(std::string target,ChannelArgs args,grpc_channel_stack_type channel_stack_type,Transport * optional_transport)39 absl::StatusOr<RefCountedPtr<Channel>> ChannelCreate(
40     std::string target, ChannelArgs args,
41     grpc_channel_stack_type channel_stack_type, Transport* optional_transport) {
42   global_stats().IncrementClientChannelsCreated();
43   // For client channels, canonify target string and add channel arg.
44   // Note: We don't do this for direct channels or lame channels.
45   if (channel_stack_type == GRPC_CLIENT_CHANNEL) {
46     target =
47         CoreConfiguration::Get().resolver_registry().AddDefaultPrefixIfNeeded(
48             target);
49     args = args.Set(GRPC_ARG_SERVER_URI, target);
50   }
51   // Set default authority if needed.
52   if (!args.GetString(GRPC_ARG_DEFAULT_AUTHORITY).has_value()) {
53     auto ssl_override = args.GetString(GRPC_SSL_TARGET_NAME_OVERRIDE_ARG);
54     if (ssl_override.has_value()) {
55       args = args.Set(GRPC_ARG_DEFAULT_AUTHORITY,
56                       std::string(ssl_override.value()));
57     }
58   }
59   // Check whether channelz is enabled.
60   if (args.GetBool(GRPC_ARG_ENABLE_CHANNELZ)
61           .value_or(GRPC_ENABLE_CHANNELZ_DEFAULT)) {
62     // Get parameters needed to create the channelz node.
63     const size_t channel_tracer_max_memory = std::max(
64         0, args.GetInt(GRPC_ARG_MAX_CHANNEL_TRACE_EVENT_MEMORY_PER_NODE)
65                .value_or(GRPC_MAX_CHANNEL_TRACE_EVENT_MEMORY_PER_NODE_DEFAULT));
66     const bool is_internal_channel =
67         args.GetBool(GRPC_ARG_CHANNELZ_IS_INTERNAL_CHANNEL).value_or(false);
68     // Create the channelz node.
69     std::string channelz_node_target{target.empty() ? "unknown" : target};
70     auto channelz_node = MakeRefCounted<channelz::ChannelNode>(
71         channelz_node_target, channel_tracer_max_memory, is_internal_channel);
72     channelz_node->AddTraceEvent(
73         channelz::ChannelTrace::Severity::Info,
74         grpc_slice_from_static_string("Channel created"));
75     // Add channelz node to channel args.
76     // We remove the is_internal_channel arg, since we no longer need it.
77     args = args.Remove(GRPC_ARG_CHANNELZ_IS_INTERNAL_CHANNEL)
78                .SetObject(std::move(channelz_node));
79   }
80   // Add transport to args.
81   if (optional_transport != nullptr) {
82     args = args.SetObject(optional_transport);
83   }
84   // Delegate to appropriate channel impl.
85   if (!args.GetBool(GRPC_ARG_USE_V3_STACK).value_or(false)) {
86     return LegacyChannel::Create(std::move(target), std::move(args),
87                                  channel_stack_type);
88   }
89   switch (channel_stack_type) {
90     case GRPC_CLIENT_CHANNEL:
91       return ClientChannel::Create(std::move(target), std::move(args));
92     case GRPC_CLIENT_DIRECT_CHANNEL:
93       return DirectChannel::Create(std::move(target), args);
94     default:
95       Crash(absl::StrCat("Invalid channel stack type for ChannelCreate: ",
96                          grpc_channel_stack_type_string(channel_stack_type)));
97   }
98 }
99 
100 }  // namespace grpc_core
101 
grpc_lame_client_channel_create(const char * target,grpc_status_code error_code,const char * error_message)102 grpc_channel* grpc_lame_client_channel_create(const char* target,
103                                               grpc_status_code error_code,
104                                               const char* error_message) {
105   grpc_core::ExecCtx exec_ctx;
106   GRPC_TRACE_LOG(api, INFO)
107       << "grpc_lame_client_channel_create(target=" << target
108       << ", error_code=" << (int)error_code
109       << ", error_message=" << error_message << ")";
110   if (error_code == GRPC_STATUS_OK) error_code = GRPC_STATUS_UNKNOWN;
111   grpc_core::ChannelArgs args =
112       grpc_core::CoreConfiguration::Get()
113           .channel_args_preconditioning()
114           .PreconditionChannelArgs(nullptr)
115           .Set(GRPC_ARG_LAME_FILTER_ERROR,
116                grpc_core::ChannelArgs::Pointer(
117                    new absl::Status(static_cast<absl::StatusCode>(error_code),
118                                     error_message),
119                    &grpc_core::kLameFilterErrorArgVtable));
120   auto channel =
121       grpc_core::ChannelCreate(target == nullptr ? "" : target, std::move(args),
122                                GRPC_CLIENT_LAME_CHANNEL, nullptr);
123   CHECK(channel.ok());
124   return channel->release()->c_ptr();
125 }
126