• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
22 #include <grpc/support/port_platform.h>
23 
24 #include <map>
25 #include <memory>
26 
27 #include "absl/container/inlined_vector.h"
28 #include "absl/strings/str_format.h"
29 
30 #include "src/core/lib/channel/channel_args.h"
31 #include "src/core/lib/gpr/useful.h"
32 #include "src/core/lib/iomgr/resolve_address.h"
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.
43 class ServerAddress {
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.
52   class AttributeInterface {
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.
67   ServerAddress(const grpc_resolved_address& address, grpc_channel_args* args,
68                 std::map<const char*, std::unique_ptr<AttributeInterface>>
69                     attributes = {});
70   ServerAddress(const void* address, size_t address_len,
71                 grpc_channel_args* args,
72                 std::map<const char*, std::unique_ptr<AttributeInterface>>
73                     attributes = {});
74 
~ServerAddress()75   ~ServerAddress() { grpc_channel_args_destroy(args_); }
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 
address()89   const grpc_resolved_address& address() const { return address_; }
args()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 //
116 class ServerAddressWeightAttribute : public ServerAddress::AttributeInterface {
117  public:
118   static const char* kServerAddressWeightAttributeKey;
119 
ServerAddressWeightAttribute(uint32_t weight)120   explicit ServerAddressWeightAttribute(uint32_t weight) : weight_(weight) {}
121 
weight()122   uint32_t weight() const { return weight_; }
123 
Copy()124   std::unique_ptr<AttributeInterface> Copy() const override {
125     return absl::make_unique<ServerAddressWeightAttribute>(weight_);
126   }
127 
Cmp(const AttributeInterface * other)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 
ToString()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 */
145