xds_channel_stack_modifier.cc
Go to the documentation of this file.
1 //
2 //
3 // Copyright 2021 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 
20 
22 
23 #include <limits.h>
24 #include <string.h>
25 
26 #include <algorithm>
27 
34 
35 namespace grpc_core {
36 namespace {
37 
38 void* XdsChannelStackModifierArgCopy(void* p) {
39  XdsChannelStackModifier* arg = static_cast<XdsChannelStackModifier*>(p);
40  return arg->Ref().release();
41 }
42 
43 void XdsChannelStackModifierArgDestroy(void* p) {
44  XdsChannelStackModifier* arg = static_cast<XdsChannelStackModifier*>(p);
45  arg->Unref();
46 }
47 
48 int XdsChannelStackModifierArgCmp(void* p, void* q) {
49  return QsortCompare(p, q);
50 }
51 
52 const grpc_arg_pointer_vtable kChannelArgVtable = {
53  XdsChannelStackModifierArgCopy, XdsChannelStackModifierArgDestroy,
54  XdsChannelStackModifierArgCmp};
55 
56 const char* kXdsChannelStackModifierChannelArgName =
57  "grpc.internal.xds_channel_stack_modifier";
58 
59 } // namespace
60 
62  // Insert the filters after the census filter if present.
63  auto it = builder->mutable_stack()->begin();
64  while (it != builder->mutable_stack()->end()) {
65  const char* filter_name_at_it = (*it)->name;
66  if (strcmp("census_server", filter_name_at_it) == 0 ||
67  strcmp("opencensus_server", filter_name_at_it) == 0) {
68  break;
69  }
70  ++it;
71  }
72  if (it == builder->mutable_stack()->end()) {
73  // No census filter found. Reset iterator to the beginning. This will result
74  // in prepending the list of xDS HTTP filters to the current stack. Note
75  // that this stage is run before the stage that adds the top server filter,
76  // resulting in these filters being finally placed after the `server`
77  // filter.
78  it = builder->mutable_stack()->begin();
79  } else {
80  ++it;
81  }
82  for (const grpc_channel_filter* filter : filters_) {
83  it = builder->mutable_stack()->insert(it, filter);
84  ++it;
85  }
86  return true;
87 }
88 
91  const_cast<char*>(kXdsChannelStackModifierChannelArgName),
92  const_cast<XdsChannelStackModifier*>(this), &kChannelArgVtable);
93 }
94 
97  XdsChannelStackModifier* config_selector_provider =
98  grpc_channel_args_find_pointer<XdsChannelStackModifier>(
99  &args, kXdsChannelStackModifierChannelArgName);
100  return config_selector_provider != nullptr ? config_selector_provider->Ref()
101  : nullptr;
102 }
103 
105  builder->channel_init()->RegisterStage(
107  const grpc_channel_args* channel_args = builder->channel_args().ToC();
108  RefCountedPtr<XdsChannelStackModifier> channel_stack_modifier =
110  grpc_channel_args_destroy(channel_args);
111  if (channel_stack_modifier != nullptr) {
112  return channel_stack_modifier->ModifyChannelStack(builder);
113  }
114  return true;
115  });
116 }
117 
118 } // namespace grpc_core
grpc_arg
Definition: grpc_types.h:103
grpc_core::RegisterXdsChannelStackModifier
void RegisterXdsChannelStackModifier(CoreConfiguration::Builder *builder)
Definition: xds_channel_stack_modifier.cc:104
regen-readme.it
it
Definition: regen-readme.py:15
core_configuration.h
grpc_core::XdsChannelStackModifier
Definition: xds_channel_stack_modifier.h:40
grpc_core::XdsChannelStackModifier::MakeChannelArg
grpc_arg MakeChannelArg() const
Definition: xds_channel_stack_modifier.cc:89
grpc_core
Definition: call_metric_recorder.h:31
grpc_core::CoreConfiguration::Builder
Definition: core_configuration.h:41
string.h
useful.h
grpc_core::XdsChannelStackModifier::GetFromChannelArgs
static RefCountedPtr< XdsChannelStackModifier > GetFromChannelArgs(const grpc_channel_args &args)
Definition: xds_channel_stack_modifier.cc:96
xds_manager.p
p
Definition: xds_manager.py:60
grpc_arg_pointer_vtable
Definition: grpc_types.h:85
grpc_channel_args
Definition: grpc_types.h:132
grpc_core::ChannelStackBuilder
Definition: channel_stack_builder.h:41
GRPC_SERVER_CHANNEL
@ GRPC_SERVER_CHANNEL
Definition: channel_stack_type.h:36
profile_analyzer.builder
builder
Definition: profile_analyzer.py:159
channel_init.h
asyncio_get_stats.args
args
Definition: asyncio_get_stats.py:40
grpc_core::RefCountedPtr
Definition: ref_counted_ptr.h:35
channel_stack.h
grpc_channel_args_destroy
void grpc_channel_args_destroy(grpc_channel_args *a)
Definition: channel_args.cc:360
arg
Definition: cmdline.cc:40
xds_channel_stack_modifier.h
grpc_core::XdsChannelStackModifier::filters_
std::vector< const grpc_channel_filter * > filters_
Definition: xds_channel_stack_modifier.h:52
channel_stack_type.h
grpc_channel_filter
Definition: channel_stack.h:111
grpc_core::QsortCompare
int QsortCompare(const T &a, const T &b)
Definition: useful.h:95
channel_args.h
grpc_core::XdsChannelStackModifier::ModifyChannelStack
bool ModifyChannelStack(ChannelStackBuilder *builder)
Definition: xds_channel_stack_modifier.cc:61
grpc_channel_arg_pointer_create
grpc_arg grpc_channel_arg_pointer_create(char *name, void *value, const grpc_arg_pointer_vtable *vtable)
Definition: channel_args.cc:492
grpc_core::RefCounted::Ref
RefCountedPtr< Child > Ref() GRPC_MUST_USE_RESULT
Definition: ref_counted.h:287
port_platform.h


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:01:55