Debian Codegraph @

src/core/lib/surface/lame_client.cc

raw
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
19 #include <grpc/support/port_platform.h>
20
21 #include "src/core/lib/surface/lame_client.h"
22
23 #include <memory>
24 #include <utility>
25
26 #include "absl/status/statusor.h"
27
28 #include <grpc/grpc.h>
29 #include <grpc/impl/codegen/connectivity_state.h>
30 #include <grpc/status.h>
31 #include <grpc/support/log.h>
32
33 #include "src/core/lib/channel/channel_args.h"
34 #include "src/core/lib/channel/channel_args_preconditioning.h"
35 #include "src/core/lib/channel/channel_stack.h"
36 #include "src/core/lib/channel/promise_based_filter.h"
37 #include "src/core/lib/config/core_configuration.h"
38 #include "src/core/lib/debug/trace.h"
39 #include "src/core/lib/gpr/useful.h"
40 #include "src/core/lib/gprpp/debug_location.h"
41 #include "src/core/lib/gprpp/ref_counted_ptr.h"
42 #include "src/core/lib/gprpp/sync.h"
43 #include "src/core/lib/iomgr/exec_ctx.h"
44 #include "src/core/lib/promise/pipe.h"
45 #include "src/core/lib/promise/promise.h"
46 #include "src/core/lib/surface/api_trace.h"
47 #include "src/core/lib/surface/channel.h"
48 #include "src/core/lib/surface/channel_stack_type.h"
49 #include "src/core/lib/transport/connectivity_state.h"
50 #include "src/core/lib/transport/transport.h"
51
52 // Avoid some IWYU confusion:
53 // IWYU pragma: no_include "src/core/lib/gprpp/orphanable.h"
54
55 #define GRPC_ARG_LAME_FILTER_ERROR "grpc.lame_filter_error"
56
57 namespace grpc_core {
58
59 const grpc_channel_filter LameClientFilter::kFilter =
60 MakePromiseBasedFilter<LameClientFilter, FilterEndpoint::kClient,
61 kFilterIsLast>("lame-client");
62
63 absl::StatusOr<LameClientFilter> LameClientFilter::Create(
64 const ChannelArgs& args, ChannelFilter::Args) {
65 return LameClientFilter(
66 *args.GetPointer<absl::Status>(GRPC_ARG_LAME_FILTER_ERROR));
67 }
68
69 LameClientFilter::LameClientFilter(absl::Status error)
70 : error_(std::move(error)), state_(std::make_unique<State>()) {}
71
72 LameClientFilter::State::State()
73 : state_tracker("lame_client", GRPC_CHANNEL_SHUTDOWN) {}
74
75 ArenaPromise<ServerMetadataHandle> LameClientFilter::MakeCallPromise(
76 CallArgs args, NextPromiseFactory) {
77 // TODO(ctiller): remove if check once promise_based_filter is removed (Close
78 // is still needed)
79 if (args.incoming_messages != nullptr) args.incoming_messages->Close();
80 return Immediate(ServerMetadataFromStatus(error_));
81 }
82
83 bool LameClientFilter::GetChannelInfo(const grpc_channel_info*) { return true; }
84
85 bool LameClientFilter::StartTransportOp(grpc_transport_op* op) {
86 {
87 MutexLock lock(&state_->mu);
88 if (op->start_connectivity_watch != nullptr) {
89 state_->state_tracker.AddWatcher(op->start_connectivity_watch_state,
90 std::move(op->start_connectivity_watch));
91 }
92 if (op->stop_connectivity_watch != nullptr) {
93 state_->state_tracker.RemoveWatcher(op->stop_connectivity_watch);
94 }
95 }
96 if (op->send_ping.on_initiate != nullptr) {
97 ExecCtx::Run(DEBUG_LOCATION, op->send_ping.on_initiate,
98 GRPC_ERROR_CREATE("lame client channel"));
99 }
100 if (op->send_ping.on_ack != nullptr) {
101 ExecCtx::Run(DEBUG_LOCATION, op->send_ping.on_ack,
102 GRPC_ERROR_CREATE("lame client channel"));
103 }
104 if (op->on_consumed != nullptr) {
105 ExecCtx::Run(DEBUG_LOCATION, op->on_consumed, absl::OkStatus());
106 }
107 return true;
108 }
109
110 namespace {
111
112 // Channel arg vtable for a grpc_error_handle.
113 void* ErrorCopy(void* p) {
114 return new absl::Status(*static_cast<absl::Status*>(p));
115 }
116 void ErrorDestroy(void* p) { delete static_cast<absl::Status*>(p); }
117 int ErrorCompare(void* p, void* q) { return QsortCompare(p, q); }
118
119 const grpc_arg_pointer_vtable kLameFilterErrorArgVtable = {
120 ErrorCopy, ErrorDestroy, ErrorCompare};
121
122 } // namespace
123
124 grpc_arg MakeLameClientErrorArg(grpc_error_handle* error) {
125 return grpc_channel_arg_pointer_create(
126 const_cast<char*>(GRPC_ARG_LAME_FILTER_ERROR), error,
127 &kLameFilterErrorArgVtable);
128 }
129
130 } // namespace grpc_core
131
132 grpc_channel* grpc_lame_client_channel_create(const char* target,
133 grpc_status_code error_code,
134 const char* error_message) {
135 grpc_core::ExecCtx exec_ctx;
136 GRPC_API_TRACE(
137 "grpc_lame_client_channel_create(target=%s, error_code=%d, "
138 "error_message=%s)",
139 3, (target, (int)error_code, error_message));
140 if (error_code == GRPC_STATUS_OK) error_code = GRPC_STATUS_UNKNOWN;
141 grpc_core::ChannelArgs args =
142 grpc_core::CoreConfiguration::Get()
143 .channel_args_preconditioning()
144 .PreconditionChannelArgs(nullptr)
145 .Set(GRPC_ARG_LAME_FILTER_ERROR,
146 grpc_core::ChannelArgs::Pointer(
147 new absl::Status(static_cast<absl::StatusCode>(error_code),
148 error_message),
149 &grpc_core::kLameFilterErrorArgVtable));
150 auto channel = grpc_core::Channel::Create(target, std::move(args),
151 GRPC_CLIENT_LAME_CHANNEL, nullptr);
152 GPR_ASSERT(channel.ok());
153 return channel->release()->c_ptr();
154 }