address_filtering.cc
Go to the documentation of this file.
1 //
2 // Copyright 2020 gRPC authors.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 
18 
20 
21 #include <stddef.h>
22 
23 #include <algorithm>
24 #include <utility>
25 
26 #include "absl/memory/memory.h"
27 #include "absl/strings/str_cat.h"
28 #include "absl/strings/str_join.h"
29 
30 #define GRPC_ARG_HIERARCHICAL_PATH "grpc.internal.address.hierarchical_path"
31 
32 namespace grpc_core {
33 
34 const char* kHierarchicalPathAttributeKey = "hierarchical_path";
35 
36 namespace {
37 
38 class HierarchicalPathAttribute : public ServerAddress::AttributeInterface {
39  public:
40  explicit HierarchicalPathAttribute(std::vector<std::string> path)
41  : path_(std::move(path)) {}
42 
43  std::unique_ptr<AttributeInterface> Copy() const override {
44  return absl::make_unique<HierarchicalPathAttribute>(path_);
45  }
46 
47  int Cmp(const AttributeInterface* other) const override {
48  const std::vector<std::string>& other_path =
49  static_cast<const HierarchicalPathAttribute*>(other)->path_;
50  for (size_t i = 0; i < path_.size(); ++i) {
51  if (other_path.size() == i) return 1;
52  int r = path_[i].compare(other_path[i]);
53  if (r != 0) return r;
54  }
55  if (other_path.size() > path_.size()) return -1;
56  return 0;
57  }
58 
59  std::string ToString() const override {
60  return absl::StrCat("[", absl::StrJoin(path_, ", "), "]");
61  }
62 
63  const std::vector<std::string>& path() const { return path_; }
64 
65  private:
66  std::vector<std::string> path_;
67 };
68 
69 } // namespace
70 
71 std::unique_ptr<ServerAddress::AttributeInterface>
72 MakeHierarchicalPathAttribute(std::vector<std::string> path) {
73  return absl::make_unique<HierarchicalPathAttribute>(std::move(path));
74 }
75 
77  const absl::StatusOr<ServerAddressList>& addresses) {
78  if (!addresses.ok()) return addresses.status();
80  for (const ServerAddress& address : *addresses) {
81  const HierarchicalPathAttribute* path_attribute =
82  static_cast<const HierarchicalPathAttribute*>(
83  address.GetAttribute(kHierarchicalPathAttributeKey));
84  if (path_attribute == nullptr) continue;
85  const std::vector<std::string>& path = path_attribute->path();
86  auto it = path.begin();
87  ServerAddressList& target_list = result[*it];
88  std::unique_ptr<HierarchicalPathAttribute> new_attribute;
89  ++it;
90  if (it != path.end()) {
91  std::vector<std::string> remaining_path(it, path.end());
92  new_attribute = absl::make_unique<HierarchicalPathAttribute>(
93  std::move(remaining_path));
94  }
95  target_list.emplace_back(address.WithAttribute(
96  kHierarchicalPathAttributeKey, std::move(new_attribute)));
97  }
98  return result;
99 }
100 
101 } // namespace grpc_core
_gevent_test_main.result
result
Definition: _gevent_test_main.py:96
address_filtering.h
regen-readme.it
it
Definition: regen-readme.py:15
absl::StrCat
std::string StrCat(const AlphaNum &a, const AlphaNum &b)
Definition: abseil-cpp/absl/strings/str_cat.cc:98
grpc_core
Definition: call_metric_recorder.h:31
grpc_core::kHierarchicalPathAttributeKey
const char * kHierarchicalPathAttributeKey
Definition: address_filtering.cc:34
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
grpc_core::ServerAddress
Definition: server_address.h:49
check_documentation.path
path
Definition: check_documentation.py:57
grpc_core::MakeHierarchicalAddressMap
absl::StatusOr< HierarchicalAddressMap > MakeHierarchicalAddressMap(const absl::StatusOr< ServerAddressList > &addresses)
Definition: address_filtering.cc:76
grpc_core::HierarchicalAddressMap
std::map< std::string, ServerAddressList > HierarchicalAddressMap
Definition: address_filtering.h:95
grpc_core::MakeHierarchicalPathAttribute
std::unique_ptr< ServerAddress::AttributeInterface > MakeHierarchicalPathAttribute(std::vector< std::string > path)
Definition: address_filtering.cc:72
path_
std::vector< std::string > path_
Definition: address_filtering.cc:66
absl::move
constexpr absl::remove_reference_t< T > && move(T &&t) noexcept
Definition: abseil-cpp/absl/utility/utility.h:221
absl::StrJoin
std::string StrJoin(Iterator start, Iterator end, absl::string_view sep, Formatter &&fmt)
Definition: abseil-cpp/absl/strings/str_join.h:239
ToString
std::string ToString(const grpc::string_ref &r)
Definition: string_ref_helper.cc:24
grpc_core::ServerAddressList
std::vector< ServerAddress > ServerAddressList
Definition: server_address.h:120
absl::StatusOr::ok
ABSL_MUST_USE_RESULT bool ok() const
Definition: abseil-cpp/absl/status/statusor.h:491
grpc_core::ServerAddress::AttributeInterface
Definition: server_address.h:58
Copy
@ Copy
Definition: upb/benchmarks/benchmark.cc:200
fix_build_deps.r
r
Definition: fix_build_deps.py:491
absl::StatusOr
Definition: abseil-cpp/absl/status/statusor.h:187
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230
absl::StatusOr::status
const Status & status() const &
Definition: abseil-cpp/absl/status/statusor.h:678
port_platform.h


grpc
Author(s):
autogenerated on Fri May 16 2025 02:57:40