GRPC Core  18.0.0
subchannel_pool_interface.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_SUBCHANNEL_POOL_INTERFACE_H
20 #define GRPC_CORE_EXT_FILTERS_CLIENT_CHANNEL_SUBCHANNEL_POOL_INTERFACE_H
21 
23 
24 #include "src/core/lib/avl/avl.h"
28 
29 namespace grpc_core {
30 
31 class Subchannel;
32 
34 
35 // A key that can uniquely identify a subchannel.
37  public:
38  explicit SubchannelKey(const grpc_channel_args* args);
40 
41  // Copyable.
42  SubchannelKey(const SubchannelKey& other);
43  SubchannelKey& operator=(const SubchannelKey& other);
44  // Movable
45  SubchannelKey(SubchannelKey&&) noexcept;
47 
48  bool operator<(const SubchannelKey& other) const;
49 
50  private:
51  // Initializes the subchannel key with the given \a args and the function to
52  // copy channel args.
53  void Init(
54  const grpc_channel_args* args,
55  grpc_channel_args* (*copy_channel_args)(const grpc_channel_args* args));
56 
57  const grpc_channel_args* args_;
58 };
59 
60 // Interface for subchannel pool.
61 // TODO(juanlishen): This refcounting mechanism may lead to memory leak.
62 // To solve that, we should force polling to flush any pending callbacks, then
63 // shut down safely. See https://github.com/grpc/grpc/issues/12560.
64 class SubchannelPoolInterface : public RefCounted<SubchannelPoolInterface> {
65  public:
68  ? "SubchannelPoolInterface"
69  : nullptr) {}
71 
72  // Registers a subchannel against a key. Returns the subchannel registered
73  // with \a key, which may be different from \a constructed because we reuse
74  // (instead of update) any existing subchannel already registered with \a key.
76  const SubchannelKey& key, RefCountedPtr<Subchannel> constructed) = 0;
77 
78  // Removes the registered subchannel found by \a key.
79  virtual void UnregisterSubchannel(const SubchannelKey& key,
80  Subchannel* subchannel) = 0;
81 
82  // Finds the subchannel registered for the given subchannel key. Returns NULL
83  // if no such channel exists. Thread-safe.
85  const SubchannelKey& key) = 0;
86 
87  // Creates a channel arg from \a subchannel pool.
88  static grpc_arg CreateChannelArg(SubchannelPoolInterface* subchannel_pool);
89 
90  // Gets the subchannel pool from the channel args.
92  const grpc_channel_args* args);
93 };
94 
95 } // namespace grpc_core
96 
97 #endif /* GRPC_CORE_EXT_FILTERS_CLIENT_CHANNEL_SUBCHANNEL_POOL_INTERFACE_H */
Definition: ref_counted.h:282
Definition: ref_counted_ptr.h:35
Definition: subchannel.h:148
Definition: subchannel_pool_interface.h:36
SubchannelKey & operator=(const SubchannelKey &other)
Definition: subchannel_pool_interface.cc:46
SubchannelKey(const grpc_channel_args *args)
Definition: subchannel_pool_interface.cc:34
bool operator<(const SubchannelKey &other) const
Definition: subchannel_pool_interface.cc:66
~SubchannelKey()
Definition: subchannel_pool_interface.cc:38
Definition: subchannel_pool_interface.h:64
virtual RefCountedPtr< Subchannel > RegisterSubchannel(const SubchannelKey &key, RefCountedPtr< Subchannel > constructed)=0
static SubchannelPoolInterface * GetSubchannelPoolFromChannelArgs(const grpc_channel_args *args)
Definition: subchannel_pool_interface.cc:104
~SubchannelPoolInterface() override
Definition: subchannel_pool_interface.h:70
virtual void UnregisterSubchannel(const SubchannelKey &key, Subchannel *subchannel)=0
static grpc_arg CreateChannelArg(SubchannelPoolInterface *subchannel_pool)
Definition: subchannel_pool_interface.cc:96
virtual RefCountedPtr< Subchannel > FindSubchannel(const SubchannelKey &key)=0
SubchannelPoolInterface()
Definition: subchannel_pool_interface.h:66
Definition: trace.h:61
Round Robin Policy.
Definition: backend_metric.cc:26
TraceFlag grpc_subchannel_pool_trace(false, "subchannel_pool")
Definition: subchannel_pool_interface.h:31
RefCountedPtr< SubchannelInterface > subchannel
Definition: ring_hash.cc:213
A single argument...
Definition: grpc_types.h:103
An array of arguments that can be passed around.
Definition: grpc_types.h:132
#define GRPC_TRACE_FLAG_ENABLED(f)
Definition: trace.h:112