Debian Codegraph @

src/core/lib/channel/channel_stack_builder_impl.cc

raw
1 /*
2 *
3 * Copyright 2016 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/channel/channel_stack_builder_impl.h"
22
23 #include <string.h>
24
25 #include <algorithm>
26 #include <vector>
27
28 #include "absl/status/status.h"
29
30 #include <grpc/impl/codegen/grpc_types.h>
31 #include <grpc/support/alloc.h>
32
33 #include "src/core/lib/channel/channel_args.h"
34 #include "src/core/lib/channel/channel_fwd.h"
35 #include "src/core/lib/channel/channel_stack.h"
36 #include "src/core/lib/debug/trace.h"
37 #include "src/core/lib/gpr/useful.h"
38 #include "src/core/lib/iomgr/error.h"
39 #include "src/core/lib/surface/call_trace.h"
40 #include "src/core/lib/transport/error_utils.h"
41 #include "src/core/lib/transport/transport.h"
42
43 namespace grpc_core {
44
45 bool ChannelStackBuilderImpl::IsPromising() const {
46 for (const auto* filter : stack()) {
47 if (filter->make_call_promise == nullptr) return false;
48 }
49 return true;
50 }
51
52 absl::StatusOr<RefCountedPtr<grpc_channel_stack>>
53 ChannelStackBuilderImpl::Build() {
54 std::vector<const grpc_channel_filter*> stack;
55 const bool is_promising = IsPromising();
56
57 for (const auto* filter : this->stack()) {
58 if (is_promising && grpc_call_trace.enabled()) {
59 stack.push_back(PromiseTracingFilterFor(filter));
60 }
61 stack.push_back(filter);
62 }
63
64 // calculate the size of the channel stack
65 size_t channel_stack_size =
66 grpc_channel_stack_size(stack.data(), stack.size());
67
68 // allocate memory
69 auto* channel_stack =
70 static_cast<grpc_channel_stack*>(gpr_zalloc(channel_stack_size));
71
72 ChannelArgs final_args = channel_args();
73 if (transport() != nullptr) {
74 static const grpc_arg_pointer_vtable vtable = {
75 // copy
76 [](void* p) { return p; },
77 // destroy
78 [](void*) {},
79 // cmp
80 [](void* a, void* b) { return QsortCompare(a, b); },
81 };
82 final_args = final_args.Set(GRPC_ARG_TRANSPORT,
83 ChannelArgs::Pointer(transport(), &vtable));
84 }
85
86 // and initialize it
87 grpc_error_handle error = grpc_channel_stack_init(
88 1,
89 [](void* p, grpc_error_handle) {
90 auto* stk = static_cast<grpc_channel_stack*>(p);
91 grpc_channel_stack_destroy(stk);
92 gpr_free(stk);
93 },
94 channel_stack, stack.data(), stack.size(), final_args, name(),
95 channel_stack);
96
97 if (!error.ok()) {
98 grpc_channel_stack_destroy(channel_stack);
99 gpr_free(channel_stack);
100 auto status = grpc_error_to_absl_status(error);
101 return status;
102 }
103
104 // run post-initialization functions
105 for (size_t i = 0; i < stack.size(); i++) {
106 auto* elem = grpc_channel_stack_element(channel_stack, i);
107 elem->filter->post_init_channel_elem(channel_stack, elem);
108 }
109
110 return RefCountedPtr<grpc_channel_stack>(channel_stack);
111 }
112
113 } // namespace grpc_core