server_address.cc
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2018 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 <string.h>
24 
25 #include <algorithm>
26 #include <memory>
27 #include <string>
28 #include <utility>
29 #include <vector>
30 
31 #include "absl/status/status.h"
32 #include "absl/status/statusor.h"
33 #include "absl/strings/str_cat.h"
34 #include "absl/strings/str_format.h"
35 #include "absl/strings/str_join.h"
36 
38 
39 // IWYU pragma: no_include <sys/socket.h>
40 
41 namespace grpc_core {
42 
43 //
44 // ServerAddressWeightAttribute
45 //
47  "server_address_weight";
48 
49 //
50 // ServerAddress
51 //
52 
55  std::map<const char*, std::unique_ptr<AttributeInterface>> attributes)
56  : address_(address), args_(args), attributes_(std::move(attributes)) {}
57 
59  const void* address, size_t address_len, grpc_channel_args* args,
60  std::map<const char*, std::unique_ptr<AttributeInterface>> attributes)
61  : args_(args), attributes_(std::move(attributes)) {
62  memcpy(address_.addr, address, address_len);
63  address_.len = static_cast<socklen_t>(address_len);
64 }
65 
68  for (const auto& p : other.attributes_) {
69  attributes_[p.first] = p.second->Copy();
70  }
71 }
73  if (&other == this) {
74  return *this;
75  }
76  address_ = other.address_;
79  attributes_.clear();
80  for (const auto& p : other.attributes_) {
81  attributes_[p.first] = p.second->Copy();
82  }
83  return *this;
84 }
85 
87  : address_(other.address_),
88  args_(other.args_),
89  attributes_(std::move(other.attributes_)) {
90  other.args_ = nullptr;
91 }
92 
94  address_ = other.address_;
96  args_ = other.args_;
97  other.args_ = nullptr;
98  attributes_ = std::move(other.attributes_);
99  return *this;
100 }
101 
102 namespace {
103 
104 int CompareAttributes(
105  const std::map<const char*,
106  std::unique_ptr<ServerAddress::AttributeInterface>>&
107  attributes1,
108  const std::map<const char*,
109  std::unique_ptr<ServerAddress::AttributeInterface>>&
110  attributes2) {
111  auto it2 = attributes2.begin();
112  for (auto it1 = attributes1.begin(); it1 != attributes1.end(); ++it1) {
113  // attributes2 has fewer elements than attributes1
114  if (it2 == attributes2.end()) return -1;
115  // compare keys
116  int retval = strcmp(it1->first, it2->first);
117  if (retval != 0) return retval;
118  // compare values
119  retval = it1->second->Cmp(it2->second.get());
120  if (retval != 0) return retval;
121  ++it2;
122  }
123  // attributes1 has fewer elements than attributes2
124  if (it2 != attributes2.end()) return 1;
125  // equal
126  return 0;
127 }
128 
129 } // namespace
130 
131 int ServerAddress::Cmp(const ServerAddress& other) const {
132  if (address_.len > other.address_.len) return 1;
133  if (address_.len < other.address_.len) return -1;
134  int retval = memcmp(address_.addr, other.address_.addr, address_.len);
135  if (retval != 0) return retval;
136  retval = grpc_channel_args_compare(args_, other.args_);
137  if (retval != 0) return retval;
138  return CompareAttributes(attributes_, other.attributes_);
139 }
140 
142  const char* key) const {
143  auto it = attributes_.find(key);
144  if (it == attributes_.end()) return nullptr;
145  return it->second.get();
146 }
147 
148 // Returns a copy of the address with a modified attribute.
149 // If the new value is null, the attribute is removed.
151  const char* key, std::unique_ptr<AttributeInterface> value) const {
152  ServerAddress address = *this;
153  if (value == nullptr) {
154  address.attributes_.erase(key);
155  } else {
156  address.attributes_[key] = std::move(value);
157  }
158  return address;
159 }
160 
162  auto addr_str = grpc_sockaddr_to_string(&address_, false);
163  std::vector<std::string> parts = {
164  addr_str.ok() ? addr_str.value() : addr_str.status().ToString(),
165  };
166  if (args_ != nullptr) {
167  parts.emplace_back(absl::StrCat("args=", grpc_channel_args_string(args_)));
168  }
169  if (!attributes_.empty()) {
170  std::vector<std::string> attrs;
171  for (const auto& p : attributes_) {
172  attrs.emplace_back(absl::StrCat(p.first, "=", p.second->ToString()));
173  }
174  parts.emplace_back(
175  absl::StrCat("attributes={", absl::StrJoin(attrs, ", "), "}"));
176  }
177  return absl::StrJoin(parts, " ");
178 }
179 
181  return absl::StrFormat("%d", weight_);
182 }
183 
184 } // namespace grpc_core
gen_stats_data.attrs
attrs
Definition: gen_stats_data.py:28
regen-readme.it
it
Definition: regen-readme.py:15
grpc_core::ServerAddress::operator=
ServerAddress & operator=(const ServerAddress &other)
Definition: server_address.cc:72
sockaddr_utils.h
absl::StrCat
std::string StrCat(const AlphaNum &a, const AlphaNum &b)
Definition: abseil-cpp/absl/strings/str_cat.cc:98
absl::StrFormat
ABSL_MUST_USE_RESULT std::string StrFormat(const FormatSpec< Args... > &format, const Args &... args)
Definition: abseil-cpp/absl/strings/str_format.h:338
grpc_core
Definition: call_metric_recorder.h:31
string.h
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
address_
ServerAddress address_
Definition: ring_hash.cc:194
grpc_core::ServerAddress
Definition: server_address.h:49
grpc_resolved_address
Definition: resolved_address.h:34
grpc_core::ServerAddressWeightAttribute::weight_
uint32_t weight_
Definition: server_address.h:146
args_
grpc_channel_args * args_
Definition: grpclb.cc:513
grpc_sockaddr_to_string
absl::StatusOr< std::string > grpc_sockaddr_to_string(const grpc_resolved_address *resolved_addr, bool normalize)
Definition: sockaddr_utils.cc:194
map
zval * map
Definition: php/ext/google/protobuf/encode_decode.c:480
grpc_channel_args
Definition: grpc_types.h:132
grpc_core::ServerAddress::address
const grpc_resolved_address & address() const
Definition: server_address.h:95
memcpy
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))
asyncio_get_stats.args
args
Definition: asyncio_get_stats.py:40
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
grpc_channel_args_compare
int grpc_channel_args_compare(const grpc_channel_args *a, const grpc_channel_args *b)
Definition: channel_args.cc:380
grpc_channel_args_destroy
void grpc_channel_args_destroy(grpc_channel_args *a)
Definition: channel_args.cc:360
grpc_core::ServerAddress::WithAttribute
ServerAddress WithAttribute(const char *key, std::unique_ptr< AttributeInterface > value) const
Definition: server_address.cc:150
server_address.h
grpc_channel_args_copy
grpc_channel_args * grpc_channel_args_copy(const grpc_channel_args *src)
Definition: channel_args.cc:285
grpc_resolved_address::len
socklen_t len
Definition: resolved_address.h:36
grpc_core::ServerAddress::ServerAddress
ServerAddress(const grpc_resolved_address &address, grpc_channel_args *args, std::map< const char *, std::unique_ptr< AttributeInterface >> attributes={})
Definition: server_address.cc:53
grpc_core::ServerAddressWeightAttribute::kServerAddressWeightAttributeKey
static const char * kServerAddressWeightAttributeKey
Definition: server_address.h:127
value
const char * value
Definition: hpack_parser_table.cc:165
key
const char * key
Definition: hpack_parser_table.cc:164
grpc_core::ServerAddress::Cmp
int Cmp(const ServerAddress &other) const
Definition: server_address.cc:131
grpc_core::ServerAddress::AttributeInterface
Definition: server_address.h:58
grpc_core::ServerAddress::ToString
std::string ToString() const
Definition: server_address.cc:161
std
Definition: grpcpp/impl/codegen/async_unary_call.h:407
grpc_core::ServerAddress::attributes_
std::map< const char *, std::unique_ptr< AttributeInterface > > attributes_
Definition: server_address.h:113
grpc_core::ServerAddressWeightAttribute::ToString
std::string ToString() const override
Definition: server_address.cc:180
grpc_core::ServerAddress::args_
grpc_channel_args * args_
Definition: server_address.h:112
grpc_core::ServerAddress::GetAttribute
const AttributeInterface * GetAttribute(const char *key) const
Definition: server_address.cc:141
grpc_core::ServerAddress::address_
grpc_resolved_address address_
Definition: server_address.h:111
grpc_resolved_address::addr
char addr[GRPC_MAX_SOCKADDR_SIZE]
Definition: resolved_address.h:35
grpc_channel_args_string
std::string grpc_channel_args_string(const grpc_channel_args *args)
Definition: channel_args.cc:502
port_platform.h


grpc
Author(s):
autogenerated on Fri May 16 2025 03:00:11