• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2016 gRPC authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef GRPC_SRC_CORE_LIB_CHANNEL_CHANNEL_STACK_BUILDER_H
16 #define GRPC_SRC_CORE_LIB_CHANNEL_CHANNEL_STACK_BUILDER_H
17 
18 #include <grpc/support/port_platform.h>
19 
20 #include <string>
21 #include <vector>
22 
23 #include "absl/status/statusor.h"
24 #include "absl/strings/string_view.h"
25 #include "src/core/lib/channel/channel_args.h"
26 #include "src/core/lib/channel/channel_fwd.h"
27 #include "src/core/lib/surface/channel_stack_type.h"
28 #include "src/core/util/ref_counted_ptr.h"
29 
30 namespace grpc_core {
31 
32 // Build a channel stack.
33 // Allows interested parties to add filters to the stack, and to query an
34 // in-progress build.
35 // Carries some useful context for the channel stack, such as a target string
36 // and a transport.
37 class ChannelStackBuilder {
38  public:
39   // Initialize with a name.
40   // channel_args *must be* preconditioned already.
41   ChannelStackBuilder(const char* name, grpc_channel_stack_type type,
42                       const ChannelArgs& channel_args);
43 
name()44   const char* name() const { return name_; }
45 
46   // Set the target string.
47   ChannelStackBuilder& SetTarget(const char* target);
48 
49   // Query the target.
target()50   absl::string_view target() const { return target_; }
51 
52   // Query the channel args.
channel_args()53   const ChannelArgs& channel_args() const { return args_; }
54 
55   // Mutable vector of proposed stack entries.
mutable_stack()56   std::vector<const grpc_channel_filter*>* mutable_stack() { return &stack_; }
57 
58   // Immutable vector of proposed stack entries.
stack()59   const std::vector<const grpc_channel_filter*>& stack() const {
60     return stack_;
61   }
62 
63   // The type of channel stack being built.
channel_stack_type()64   grpc_channel_stack_type channel_stack_type() const { return type_; }
65 
66   // TODO(ctiller): re-evaluate the need for AppendFilter, PrependFilter.
67   // Their usefulness is largely zero now that we have ordering constraints in
68   // channel init.
69 
70   // Helper to add a filter to the front of the stack.
71   void PrependFilter(const grpc_channel_filter* filter);
72 
73   // Helper to add a filter to the end of the stack.
74   void AppendFilter(const grpc_channel_filter* filter);
75 
76   // Build the channel stack.
77   // After success, *result holds the new channel stack,
78   // prefix_bytes are allocated before the channel stack,
79   // destroy is as per grpc_channel_stack_init
80   // On failure, *result is nullptr.
81   virtual absl::StatusOr<RefCountedPtr<grpc_channel_stack>> Build() = 0;
82 
83  protected:
84   ~ChannelStackBuilder() = default;
85 
86  private:
unknown_target()87   static std::string unknown_target() { return "unknown"; }
88 
89   // The name of the stack
90   const char* const name_;
91   // The type of stack being built
92   const grpc_channel_stack_type type_;
93   // The target
94   std::string target_{unknown_target()};
95   // Channel args
96   ChannelArgs args_;
97   // The in-progress stack
98   std::vector<const grpc_channel_filter*> stack_;
99 };
100 
101 }  // namespace grpc_core
102 
103 #endif  // GRPC_SRC_CORE_LIB_CHANNEL_CHANNEL_STACK_BUILDER_H
104