• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/status.h"
27 #include "absl/status/statusor.h"
28 
29 #include <grpc/grpc.h>
30 #include <grpc/impl/connectivity_state.h>
31 #include <grpc/status.h>
32 #include <grpc/support/log.h>
33 
34 #include "src/core/lib/channel/channel_args.h"
35 #include "src/core/lib/channel/channel_args_preconditioning.h"
36 #include "src/core/lib/channel/channel_stack.h"
37 #include "src/core/lib/channel/promise_based_filter.h"
38 #include "src/core/lib/config/core_configuration.h"
39 #include "src/core/lib/debug/trace.h"
40 #include "src/core/lib/gpr/useful.h"
41 #include "src/core/lib/gprpp/debug_location.h"
42 #include "src/core/lib/gprpp/ref_counted_ptr.h"
43 #include "src/core/lib/gprpp/sync.h"
44 #include "src/core/lib/iomgr/exec_ctx.h"
45 #include "src/core/lib/promise/pipe.h"
46 #include "src/core/lib/promise/promise.h"
47 #include "src/core/lib/surface/api_trace.h"
48 #include "src/core/lib/surface/channel.h"
49 #include "src/core/lib/surface/channel_stack_type.h"
50 #include "src/core/lib/transport/connectivity_state.h"
51 #include "src/core/lib/transport/metadata_batch.h"
52 #include "src/core/lib/transport/transport.h"
53 
54 // Avoid some IWYU confusion:
55 // IWYU pragma: no_include "src/core/lib/gprpp/orphanable.h"
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->CloseWithError();
81   }
82   if (args.client_to_server_messages != nullptr) {
83     args.client_to_server_messages->CloseWithError();
84   }
85   if (args.server_initial_metadata != nullptr) {
86     args.server_initial_metadata->CloseWithError();
87   }
88   args.client_initial_metadata_outstanding.Complete(true);
89   return Immediate(ServerMetadataFromStatus(error_));
90 }
91 
GetChannelInfo(const grpc_channel_info *)92 bool LameClientFilter::GetChannelInfo(const grpc_channel_info*) { return true; }
93 
StartTransportOp(grpc_transport_op * op)94 bool LameClientFilter::StartTransportOp(grpc_transport_op* op) {
95   {
96     MutexLock lock(&state_->mu);
97     if (op->start_connectivity_watch != nullptr) {
98       state_->state_tracker.AddWatcher(op->start_connectivity_watch_state,
99                                        std::move(op->start_connectivity_watch));
100     }
101     if (op->stop_connectivity_watch != nullptr) {
102       state_->state_tracker.RemoveWatcher(op->stop_connectivity_watch);
103     }
104   }
105   if (op->send_ping.on_initiate != nullptr) {
106     ExecCtx::Run(DEBUG_LOCATION, op->send_ping.on_initiate,
107                  GRPC_ERROR_CREATE("lame client channel"));
108   }
109   if (op->send_ping.on_ack != nullptr) {
110     ExecCtx::Run(DEBUG_LOCATION, op->send_ping.on_ack,
111                  GRPC_ERROR_CREATE("lame client channel"));
112   }
113   if (op->on_consumed != nullptr) {
114     ExecCtx::Run(DEBUG_LOCATION, op->on_consumed, absl::OkStatus());
115   }
116   return true;
117 }
118 
119 namespace {
120 
121 // Channel arg vtable for a grpc_error_handle.
ErrorCopy(void * p)122 void* ErrorCopy(void* p) {
123   return new absl::Status(*static_cast<absl::Status*>(p));
124 }
ErrorDestroy(void * p)125 void ErrorDestroy(void* p) { delete static_cast<absl::Status*>(p); }
ErrorCompare(void * p,void * q)126 int ErrorCompare(void* p, void* q) { return QsortCompare(p, q); }
127 
128 }  // namespace
129 
130 const grpc_arg_pointer_vtable kLameFilterErrorArgVtable = {
131     ErrorCopy, ErrorDestroy, ErrorCompare};
132 
MakeLameClientErrorArg(grpc_error_handle * error)133 grpc_arg MakeLameClientErrorArg(grpc_error_handle* error) {
134   return grpc_channel_arg_pointer_create(
135       const_cast<char*>(GRPC_ARG_LAME_FILTER_ERROR), error,
136       &kLameFilterErrorArgVtable);
137 }
138 
139 }  // namespace grpc_core
140