GRPC C++  1.39.1
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_EXT_FILTERS_CLIENT_CHANNEL_SERVER_ADDRESS_H
20 #define GRPC_CORE_EXT_FILTERS_CLIENT_CHANNEL_SERVER_ADDRESS_H
21 
23 
24 #include <map>
25 #include <memory>
26 
27 #include "absl/container/inlined_vector.h"
28 #include "absl/strings/str_format.h"
29 
33 
34 namespace grpc_core {
35 
36 //
37 // ServerAddress
38 //
39 
40 // A server address is a grpc_resolved_address with an associated set of
41 // channel args. Any args present here will be merged into the channel
42 // args when a subchannel is created for this address.
44  public:
45  // Base class for resolver-supplied attributes.
46  // Unlike channel args, these attributes don't affect subchannel
47  // uniqueness or behavior. They are for use by LB policies only.
48  //
49  // Attributes are keyed by a C string that is unique by address, not
50  // by value. All attributes added with the same key must be of the
51  // same type.
53  public:
54  virtual ~AttributeInterface() = default;
55 
56  // Creates a copy of the attribute.
57  virtual std::unique_ptr<AttributeInterface> Copy() const = 0;
58 
59  // Compares this attribute with another.
60  virtual int Cmp(const AttributeInterface* other) const = 0;
61 
62  // Returns a human-readable representation of the attribute.
63  virtual std::string ToString() const = 0;
64  };
65 
66  // Takes ownership of args.
68  std::map<const char*, std::unique_ptr<AttributeInterface>>
69  attributes = {});
70  ServerAddress(const void* address, size_t address_len,
72  std::map<const char*, std::unique_ptr<AttributeInterface>>
73  attributes = {});
74 
76 
77  // Copyable.
78  ServerAddress(const ServerAddress& other);
79  ServerAddress& operator=(const ServerAddress& other);
80 
81  // Movable.
82  ServerAddress(ServerAddress&& other) noexcept;
83  ServerAddress& operator=(ServerAddress&& other) noexcept;
84 
85  bool operator==(const ServerAddress& other) const { return Cmp(other) == 0; }
86 
87  int Cmp(const ServerAddress& other) const;
88 
89  const grpc_resolved_address& address() const { return address_; }
90  const grpc_channel_args* args() const { return args_; }
91 
92  const AttributeInterface* GetAttribute(const char* key) const;
93 
94  // Returns a copy of the address with a modified attribute.
95  // If the new value is null, the attribute is removed.
96  ServerAddress WithAttribute(const char* key,
97  std::unique_ptr<AttributeInterface> value) const;
98 
99  std::string ToString() const;
100 
101  private:
102  grpc_resolved_address address_;
103  grpc_channel_args* args_;
104  std::map<const char*, std::unique_ptr<AttributeInterface>> attributes_;
105 };
106 
107 //
108 // ServerAddressList
109 //
110 
111 typedef absl::InlinedVector<ServerAddress, 1> ServerAddressList;
112 
113 //
114 // ServerAddressWeightAttribute
115 //
117  public:
119 
120  explicit ServerAddressWeightAttribute(uint32_t weight) : weight_(weight) {}
121 
122  uint32_t weight() const { return weight_; }
123 
124  std::unique_ptr<AttributeInterface> Copy() const override {
125  return absl::make_unique<ServerAddressWeightAttribute>(weight_);
126  }
127 
128  int Cmp(const AttributeInterface* other) const override {
129  const auto* other_locality_attr =
130  static_cast<const ServerAddressWeightAttribute*>(other);
131  return GPR_ICMP(weight_, other_locality_attr->weight_);
132  }
133 
134  std::string ToString() const override {
135  return absl::StrFormat("%d", weight_);
136  }
137 
138  private:
139  uint32_t weight_;
140 };
141 
142 } // namespace grpc_core
143 
144 #endif /* GRPC_CORE_EXT_FILTERS_CLIENT_CHANNEL_SERVER_ADDRESS_H */
void grpc_channel_args_destroy(grpc_channel_args *a)
Destroy arguments created by grpc_channel_args_copy.
Definition: channel_args.cc:203
Definition: server_address.h:52
virtual std::string ToString() const =0
virtual std::unique_ptr< AttributeInterface > Copy() const =0
virtual int Cmp(const AttributeInterface *other) const =0
Definition: server_address.h:43
~ServerAddress()
Definition: server_address.h:75
const AttributeInterface * GetAttribute(const char *key) const
Definition: server_address.cc:131
int Cmp(const ServerAddress &other) const
Definition: server_address.cc:121
ServerAddress(const grpc_resolved_address &address, grpc_channel_args *args, std::map< const char *, std::unique_ptr< AttributeInterface >> attributes={})
Definition: server_address.cc:44
const grpc_resolved_address & address() const
Definition: server_address.h:89
const grpc_channel_args * args() const
Definition: server_address.h:90
std::string ToString() const
Definition: server_address.cc:151
ServerAddress WithAttribute(const char *key, std::unique_ptr< AttributeInterface > value) const
Definition: server_address.cc:140
ServerAddress & operator=(const ServerAddress &other)
Definition: server_address.cc:63
bool operator==(const ServerAddress &other) const
Definition: server_address.h:85
Definition: server_address.h:116
ServerAddressWeightAttribute(uint32_t weight)
Definition: server_address.h:120
int Cmp(const AttributeInterface *other) const override
Definition: server_address.h:128
std::unique_ptr< AttributeInterface > Copy() const override
Definition: server_address.h:124
uint32_t weight() const
Definition: server_address.h:122
std::string ToString() const override
Definition: server_address.h:134
static const char * kServerAddressWeightAttributeKey
Definition: server_address.h:118
Round Robin Policy.
Definition: backend_metric.cc:26
absl::InlinedVector< ServerAddress, 1 > ServerAddressList
Definition: server_address.h:111
An array of arguments that can be passed around.
Definition: grpc_types.h:132
Definition: resolve_address.h:44
#define GPR_ICMP(a, b)
Definition: useful.h:60