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 "src/core/lib/surface/lame_client.h"
20
21 #include <grpc/grpc.h>
22 #include <grpc/impl/connectivity_state.h>
23 #include <grpc/status.h>
24 #include <grpc/support/port_platform.h>
25
26 #include <memory>
27 #include <utility>
28
29 #include "absl/status/status.h"
30 #include "absl/status/statusor.h"
31 #include "src/core/config/core_configuration.h"
32 #include "src/core/lib/channel/channel_args.h"
33 #include "src/core/lib/channel/channel_args_preconditioning.h"
34 #include "src/core/lib/channel/channel_stack.h"
35 #include "src/core/lib/channel/promise_based_filter.h"
36 #include "src/core/lib/debug/trace.h"
37 #include "src/core/lib/iomgr/exec_ctx.h"
38 #include "src/core/lib/promise/pipe.h"
39 #include "src/core/lib/promise/promise.h"
40 #include "src/core/lib/surface/channel.h"
41 #include "src/core/lib/surface/channel_stack_type.h"
42 #include "src/core/lib/transport/connectivity_state.h"
43 #include "src/core/lib/transport/metadata_batch.h"
44 #include "src/core/lib/transport/transport.h"
45 #include "src/core/util/debug_location.h"
46 #include "src/core/util/ref_counted_ptr.h"
47 #include "src/core/util/sync.h"
48 #include "src/core/util/useful.h"
49
50 // Avoid some IWYU confusion:
51 // IWYU pragma: no_include "src/core/util/orphanable.h"
52
53 namespace grpc_core {
54
55 const grpc_channel_filter LameClientFilter::kFilter =
56 MakePromiseBasedFilter<LameClientFilter, FilterEndpoint::kClient,
57 kFilterIsLast>();
58
Create(const ChannelArgs & args,ChannelFilter::Args)59 absl::StatusOr<std::unique_ptr<LameClientFilter>> LameClientFilter::Create(
60 const ChannelArgs& args, ChannelFilter::Args) {
61 return std::make_unique<LameClientFilter>(
62 *args.GetPointer<absl::Status>(GRPC_ARG_LAME_FILTER_ERROR));
63 }
64
LameClientFilter(absl::Status error)65 LameClientFilter::LameClientFilter(absl::Status error)
66 : error_(std::move(error)),
67 state_tracker_("lame_client", GRPC_CHANNEL_SHUTDOWN) {}
68
MakeCallPromise(CallArgs args,NextPromiseFactory)69 ArenaPromise<ServerMetadataHandle> LameClientFilter::MakeCallPromise(
70 CallArgs args, NextPromiseFactory) {
71 // TODO(ctiller): remove if check once promise_based_filter is removed (Close
72 // is still needed)
73 if (args.server_to_client_messages != nullptr) {
74 args.server_to_client_messages->CloseWithError();
75 }
76 if (args.client_to_server_messages != nullptr) {
77 args.client_to_server_messages->CloseWithError();
78 }
79 if (args.server_initial_metadata != nullptr) {
80 args.server_initial_metadata->CloseWithError();
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(&mu_);
91 if (op->start_connectivity_watch != nullptr) {
92 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_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 } // namespace
123
124 const grpc_arg_pointer_vtable kLameFilterErrorArgVtable = {
125 ErrorCopy, ErrorDestroy, ErrorCompare};
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