xref: /aosp_15_r20/external/grpc-grpc/src/core/lib/surface/channel_create.cc (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
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 <grpc/support/port_platform.h>
18 
19 #include <grpc/grpc.h>
20 #include <grpc/impl/channel_arg_names.h>
21 #include <grpc/support/log.h>
22 
23 #include "src/core/lib/channel/channel_args.h"
24 #include "src/core/lib/channel/channelz.h"
25 #include "src/core/lib/config/core_configuration.h"
26 #include "src/core/lib/debug/stats.h"
27 #include "src/core/lib/debug/stats_data.h"
28 #include "src/core/lib/surface/channel.h"
29 #include "src/core/lib/surface/lame_client.h"
30 #include "src/core/lib/surface/legacy_channel.h"
31 
32 namespace grpc_core {
33 
ChannelCreate(std::string target,ChannelArgs args,grpc_channel_stack_type channel_stack_type,Transport * optional_transport)34 absl::StatusOr<OrphanablePtr<Channel>> ChannelCreate(
35     std::string target, ChannelArgs args,
36     grpc_channel_stack_type channel_stack_type, Transport* optional_transport) {
37   global_stats().IncrementClientChannelsCreated();
38   // For client channels, canonify target string and add channel arg.
39   // Note: We don't do this for direct channels or lame channels.
40   if (channel_stack_type == GRPC_CLIENT_CHANNEL) {
41     target =
42         CoreConfiguration::Get().resolver_registry().AddDefaultPrefixIfNeeded(
43             target);
44     args = args.Set(GRPC_ARG_SERVER_URI, target);
45   }
46   // Set default authority if needed.
47   if (!args.GetString(GRPC_ARG_DEFAULT_AUTHORITY).has_value()) {
48     auto ssl_override = args.GetString(GRPC_SSL_TARGET_NAME_OVERRIDE_ARG);
49     if (ssl_override.has_value()) {
50       args = args.Set(GRPC_ARG_DEFAULT_AUTHORITY,
51                       std::string(ssl_override.value()));
52     }
53   }
54   // Check whether channelz is enabled.
55   if (args.GetBool(GRPC_ARG_ENABLE_CHANNELZ)
56           .value_or(GRPC_ENABLE_CHANNELZ_DEFAULT)) {
57     // Get parameters needed to create the channelz node.
58     const size_t channel_tracer_max_memory = std::max(
59         0, args.GetInt(GRPC_ARG_MAX_CHANNEL_TRACE_EVENT_MEMORY_PER_NODE)
60                .value_or(GRPC_MAX_CHANNEL_TRACE_EVENT_MEMORY_PER_NODE_DEFAULT));
61     const bool is_internal_channel =
62         args.GetBool(GRPC_ARG_CHANNELZ_IS_INTERNAL_CHANNEL).value_or(false);
63     // Create the channelz node.
64     std::string channelz_node_target{target.empty() ? "unknown" : target};
65     auto channelz_node = MakeRefCounted<channelz::ChannelNode>(
66         channelz_node_target, channel_tracer_max_memory, is_internal_channel);
67     channelz_node->AddTraceEvent(
68         channelz::ChannelTrace::Severity::Info,
69         grpc_slice_from_static_string("Channel created"));
70     // Add channelz node to channel args.
71     // We remove the is_internal_channel arg, since we no longer need it.
72     args = args.Remove(GRPC_ARG_CHANNELZ_IS_INTERNAL_CHANNEL)
73                .SetObject(std::move(channelz_node));
74   }
75   // Add transport to args.
76   if (optional_transport != nullptr) {
77     args = args.SetObject(optional_transport);
78   }
79   // Delegate to legacy channel impl.
80   return LegacyChannel::Create(std::move(target), std::move(args),
81                                channel_stack_type);
82 }
83 
84 }  // namespace grpc_core
85 
grpc_lame_client_channel_create(const char * target,grpc_status_code error_code,const char * error_message)86 grpc_channel* grpc_lame_client_channel_create(const char* target,
87                                               grpc_status_code error_code,
88                                               const char* error_message) {
89   grpc_core::ExecCtx exec_ctx;
90   GRPC_API_TRACE(
91       "grpc_lame_client_channel_create(target=%s, error_code=%d, "
92       "error_message=%s)",
93       3, (target, (int)error_code, error_message));
94   if (error_code == GRPC_STATUS_OK) error_code = GRPC_STATUS_UNKNOWN;
95   grpc_core::ChannelArgs args =
96       grpc_core::CoreConfiguration::Get()
97           .channel_args_preconditioning()
98           .PreconditionChannelArgs(nullptr)
99           .Set(GRPC_ARG_LAME_FILTER_ERROR,
100                grpc_core::ChannelArgs::Pointer(
101                    new absl::Status(static_cast<absl::StatusCode>(error_code),
102                                     error_message),
103                    &grpc_core::kLameFilterErrorArgVtable));
104   auto channel =
105       grpc_core::ChannelCreate(target == nullptr ? "" : target, std::move(args),
106                                GRPC_CLIENT_LAME_CHANNEL, nullptr);
107   GPR_ASSERT(channel.ok());
108   return channel->release()->c_ptr();
109 }
110