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/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
Create(const ChannelArgs & args,ChannelFilter::Args)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
LameClientFilter(absl::Status error)69 LameClientFilter::LameClientFilter(absl::Status error)
70 : error_(std::move(error)), state_(std::make_unique<State>()) {}
71
State()72 LameClientFilter::State::State()
73 : state_tracker("lame_client", GRPC_CHANNEL_SHUTDOWN) {}
74
MakeCallPromise(CallArgs args,NextPromiseFactory)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.server_to_client_messages != nullptr) {
80 args.server_to_client_messages->Close();
81 }
82 args.client_initial_metadata_outstanding.Complete(true);
83 return Immediate(ServerMetadataFromStatus(error_));
84 }
85
GetChannelInfo(const grpc_channel_info *)86 bool LameClientFilter::GetChannelInfo(const grpc_channel_info*) { return true; }
87
StartTransportOp(grpc_transport_op * op)88 bool LameClientFilter::StartTransportOp(grpc_transport_op* op) {
89 {
90 MutexLock lock(&state_->mu);
91 if (op->start_connectivity_watch != nullptr) {
92 state_->state_tracker.AddWatcher(op->start_connectivity_watch_state,
93 std::move(op->start_connectivity_watch));
94 }
95 if (op->stop_connectivity_watch != nullptr) {
96 state_->state_tracker.RemoveWatcher(op->stop_connectivity_watch);
97 }
98 }
99 if (op->send_ping.on_initiate != nullptr) {
100 ExecCtx::Run(DEBUG_LOCATION, op->send_ping.on_initiate,
101 GRPC_ERROR_CREATE("lame client channel"));
102 }
103 if (op->send_ping.on_ack != nullptr) {
104 ExecCtx::Run(DEBUG_LOCATION, op->send_ping.on_ack,
105 GRPC_ERROR_CREATE("lame client channel"));
106 }
107 if (op->on_consumed != nullptr) {
108 ExecCtx::Run(DEBUG_LOCATION, op->on_consumed, absl::OkStatus());
109 }
110 return true;
111 }
112
113 namespace {
114
115 // Channel arg vtable for a grpc_error_handle.
ErrorCopy(void * p)116 void* ErrorCopy(void* p) {
117 return new absl::Status(*static_cast<absl::Status*>(p));
118 }
ErrorDestroy(void * p)119 void ErrorDestroy(void* p) { delete static_cast<absl::Status*>(p); }
ErrorCompare(void * p,void * q)120 int ErrorCompare(void* p, void* q) { return QsortCompare(p, q); }
121
122 const grpc_arg_pointer_vtable kLameFilterErrorArgVtable = {
123 ErrorCopy, ErrorDestroy, ErrorCompare};
124
125 } // namespace
126
MakeLameClientErrorArg(grpc_error_handle * error)127 grpc_arg MakeLameClientErrorArg(grpc_error_handle* error) {
128 return grpc_channel_arg_pointer_create(
129 const_cast<char*>(GRPC_ARG_LAME_FILTER_ERROR), error,
130 &kLameFilterErrorArgVtable);
131 }
132
133 } // namespace grpc_core
134
grpc_lame_client_channel_create(const char * target,grpc_status_code error_code,const char * error_message)135 grpc_channel* grpc_lame_client_channel_create(const char* target,
136 grpc_status_code error_code,
137 const char* error_message) {
138 grpc_core::ExecCtx exec_ctx;
139 GRPC_API_TRACE(
140 "grpc_lame_client_channel_create(target=%s, error_code=%d, "
141 "error_message=%s)",
142 3, (target, (int)error_code, error_message));
143 if (error_code == GRPC_STATUS_OK) error_code = GRPC_STATUS_UNKNOWN;
144 grpc_core::ChannelArgs args =
145 grpc_core::CoreConfiguration::Get()
146 .channel_args_preconditioning()
147 .PreconditionChannelArgs(nullptr)
148 .Set(GRPC_ARG_LAME_FILTER_ERROR,
149 grpc_core::ChannelArgs::Pointer(
150 new absl::Status(static_cast<absl::StatusCode>(error_code),
151 error_message),
152 &grpc_core::kLameFilterErrorArgVtable));
153 auto channel = grpc_core::Channel::Create(target, std::move(args),
154 GRPC_CLIENT_LAME_CHANNEL, nullptr);
155 GPR_ASSERT(channel.ok());
156 return channel->release()->c_ptr();
157 }
158