• 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_SRC_CORE_CLIENT_CHANNEL_SUBCHANNEL_POOL_INTERFACE_H
20 #define GRPC_SRC_CORE_CLIENT_CHANNEL_SUBCHANNEL_POOL_INTERFACE_H
21 
22 #include <grpc/support/port_platform.h>
23 
24 #include <string>
25 
26 #include "absl/strings/string_view.h"
27 #include "src/core/lib/channel/channel_args.h"
28 #include "src/core/lib/debug/trace.h"
29 #include "src/core/lib/iomgr/resolved_address.h"
30 #include "src/core/util/ref_counted.h"
31 #include "src/core/util/ref_counted_ptr.h"
32 #include "src/core/util/useful.h"
33 
34 namespace grpc_core {
35 
36 class Subchannel;
37 
38 // A key that can uniquely identify a subchannel.
39 class SubchannelKey final {
40  public:
41   SubchannelKey(const grpc_resolved_address& address, const ChannelArgs& args);
42 
43   SubchannelKey(const SubchannelKey& other) = default;
44   SubchannelKey& operator=(const SubchannelKey& other) = default;
45   SubchannelKey(SubchannelKey&& other) noexcept = default;
46   SubchannelKey& operator=(SubchannelKey&& other) noexcept = default;
47 
48   bool operator<(const SubchannelKey& other) const;
49 
address()50   const grpc_resolved_address& address() const { return address_; }
args()51   const ChannelArgs& args() const { return args_; }
52 
53   // Human-readable string suitable for logging.
54   std::string ToString() const;
55 
56  private:
57   grpc_resolved_address address_;
58   ChannelArgs args_;
59 };
60 
61 // Interface for subchannel pool.
62 // TODO(juanlishen): This refcounting mechanism may lead to memory leak.
63 // To solve that, we should force polling to flush any pending callbacks, then
64 // shut down safely. See https://github.com/grpc/grpc/issues/12560.
65 class SubchannelPoolInterface : public RefCounted<SubchannelPoolInterface> {
66  public:
SubchannelPoolInterface()67   SubchannelPoolInterface()
68       : RefCounted(GRPC_TRACE_FLAG_ENABLED(subchannel_pool)
69                        ? "SubchannelPoolInterface"
70                        : nullptr) {}
~SubchannelPoolInterface()71   ~SubchannelPoolInterface() override {}
72 
73   static absl::string_view ChannelArgName();
ChannelArgsCompare(const SubchannelPoolInterface * a,const SubchannelPoolInterface * b)74   static int ChannelArgsCompare(const SubchannelPoolInterface* a,
75                                 const SubchannelPoolInterface* b) {
76     return QsortCompare(a, b);
77   }
78 
79   // Registers a subchannel against a key. Returns the subchannel registered
80   // with \a key, which may be different from \a constructed because we reuse
81   // (instead of update) any existing subchannel already registered with \a key.
82   virtual RefCountedPtr<Subchannel> RegisterSubchannel(
83       const SubchannelKey& key, RefCountedPtr<Subchannel> constructed) = 0;
84 
85   // Removes the registered subchannel found by \a key.
86   virtual void UnregisterSubchannel(const SubchannelKey& key,
87                                     Subchannel* subchannel) = 0;
88 
89   // Finds the subchannel registered for the given subchannel key. Returns NULL
90   // if no such channel exists. Thread-safe.
91   virtual RefCountedPtr<Subchannel> FindSubchannel(
92       const SubchannelKey& key) = 0;
93 };
94 
95 }  // namespace grpc_core
96 
97 #endif  // GRPC_SRC_CORE_CLIENT_CHANNEL_SUBCHANNEL_POOL_INTERFACE_H
98