server_address.h
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 
19 #ifndef GRPC_CORE_LIB_RESOLVER_SERVER_ADDRESS_H
20 #define GRPC_CORE_LIB_RESOLVER_SERVER_ADDRESS_H
21 
23 
24 #include <stddef.h>
25 #include <stdint.h>
26 
27 #include <map>
28 #include <memory>
29 #include <string>
30 #include <vector>
31 
32 #include "absl/memory/memory.h"
33 
35 
39 
40 namespace grpc_core {
41 
42 //
43 // ServerAddress
44 //
45 
46 // A server address is a grpc_resolved_address with an associated set of
47 // channel args. Any args present here will be merged into the channel
48 // args when a subchannel is created for this address.
50  public:
51  // Base class for resolver-supplied attributes.
52  // Unlike channel args, these attributes don't affect subchannel
53  // uniqueness or behavior. They are for use by LB policies only.
54  //
55  // Attributes are keyed by a C string that is unique by address, not
56  // by value. All attributes added with the same key must be of the
57  // same type.
59  public:
60  virtual ~AttributeInterface() = default;
61 
62  // Creates a copy of the attribute.
63  virtual std::unique_ptr<AttributeInterface> Copy() const = 0;
64 
65  // Compares this attribute with another.
66  virtual int Cmp(const AttributeInterface* other) const = 0;
67 
68  // Returns a human-readable representation of the attribute.
69  virtual std::string ToString() const = 0;
70  };
71 
72  // Takes ownership of args.
74  std::map<const char*, std::unique_ptr<AttributeInterface>>
75  attributes = {});
76  ServerAddress(const void* address, size_t address_len,
78  std::map<const char*, std::unique_ptr<AttributeInterface>>
79  attributes = {});
80 
82 
83  // Copyable.
84  ServerAddress(const ServerAddress& other);
85  ServerAddress& operator=(const ServerAddress& other);
86 
87  // Movable.
88  ServerAddress(ServerAddress&& other) noexcept;
89  ServerAddress& operator=(ServerAddress&& other) noexcept;
90 
91  bool operator==(const ServerAddress& other) const { return Cmp(other) == 0; }
92 
93  int Cmp(const ServerAddress& other) const;
94 
95  const grpc_resolved_address& address() const { return address_; }
96  const grpc_channel_args* args() const { return args_; }
97 
98  const AttributeInterface* GetAttribute(const char* key) const;
99 
100  // Returns a copy of the address with a modified attribute.
101  // If the new value is null, the attribute is removed.
102  ServerAddress WithAttribute(const char* key,
103  std::unique_ptr<AttributeInterface> value) const;
104 
105  // TODO(ctiller): Prior to making this a public API we should ensure that the
106  // channel args are not part of the generated string, lest we make that debug
107  // format load-bearing via Hyrum's law.
108  std::string ToString() const;
109 
110  private:
113  std::map<const char*, std::unique_ptr<AttributeInterface>> attributes_;
114 };
115 
116 //
117 // ServerAddressList
118 //
119 
120 using ServerAddressList = std::vector<ServerAddress>;
121 
122 //
123 // ServerAddressWeightAttribute
124 //
126  public:
128 
130 
131  uint32_t weight() const { return weight_; }
132 
133  std::unique_ptr<AttributeInterface> Copy() const override {
134  return absl::make_unique<ServerAddressWeightAttribute>(weight_);
135  }
136 
137  int Cmp(const AttributeInterface* other) const override {
138  const auto* other_locality_attr =
139  static_cast<const ServerAddressWeightAttribute*>(other);
140  return QsortCompare(weight_, other_locality_attr->weight_);
141  }
142 
143  std::string ToString() const override;
144 
145  private:
147 };
148 
149 } // namespace grpc_core
150 
151 #endif /* GRPC_CORE_LIB_RESOLVER_SERVER_ADDRESS_H */
grpc_core::ServerAddress::operator=
ServerAddress & operator=(const ServerAddress &other)
Definition: server_address.cc:72
grpc_core
Definition: call_metric_recorder.h:31
grpc_core::ServerAddress::operator==
bool operator==(const ServerAddress &other) const
Definition: server_address.h:91
useful.h
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
grpc_resolved_address
Definition: resolved_address.h:34
grpc_core::ServerAddressWeightAttribute::weight_
uint32_t weight_
Definition: server_address.h:146
grpc_core::ServerAddress::AttributeInterface::Copy
virtual std::unique_ptr< AttributeInterface > Copy() const =0
resolved_address.h
map
zval * map
Definition: php/ext/google/protobuf/encode_decode.c:480
grpc_channel_args
Definition: grpc_types.h:132
grpc_core::ServerAddressWeightAttribute::ServerAddressWeightAttribute
ServerAddressWeightAttribute(uint32_t weight)
Definition: server_address.h:129
grpc_types.h
uint32_t
unsigned int uint32_t
Definition: stdint-msvc2008.h:80
grpc_core::ServerAddress::address
const grpc_resolved_address & address() const
Definition: server_address.h:95
grpc_core::ServerAddressWeightAttribute::weight
uint32_t weight() const
Definition: server_address.h:131
grpc_core::ServerAddress::AttributeInterface::Cmp
virtual int Cmp(const AttributeInterface *other) const =0
grpc_core::ServerAddress::args
const grpc_channel_args * args() const
Definition: server_address.h:96
grpc_core::ServerAddressWeightAttribute::Cmp
int Cmp(const AttributeInterface *other) const override
Definition: server_address.h:137
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
grpc_core::ServerAddressWeightAttribute::Copy
std::unique_ptr< AttributeInterface > Copy() const override
Definition: server_address.h:133
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::ServerAddressList
std::vector< ServerAddress > ServerAddressList
Definition: server_address.h:120
grpc_core::ServerAddressWeightAttribute::kServerAddressWeightAttributeKey
static const char * kServerAddressWeightAttributeKey
Definition: server_address.h:127
stdint.h
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::QsortCompare
int QsortCompare(const T &a, const T &b)
Definition: useful.h:95
grpc_core::ServerAddress::AttributeInterface
Definition: server_address.h:58
grpc_core::ServerAddressWeightAttribute
Definition: server_address.h:125
grpc_core::ServerAddress::~ServerAddress
~ServerAddress()
Definition: server_address.h:81
grpc_core::ServerAddress::ToString
std::string ToString() const
Definition: server_address.cc:161
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
channel_args.h
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_core::ServerAddress::AttributeInterface::ToString
virtual std::string ToString() const =0
grpc_core::ServerAddress::AttributeInterface::~AttributeInterface
virtual ~AttributeInterface()=default
port_platform.h


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