Built motion from commit 6a09e18b.|2.6.11
[motion2.git] / legacy-libs / grpc-cloned / deps / grpc / src / core / ext / filters / client_channel / subchannel_pool_interface.h
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
22 #include <grpc/support/port_platform.h>
23
24 #include "src/core/lib/avl/avl.h"
25 #include "src/core/lib/channel/channel_args.h"
26 #include "src/core/lib/gprpp/abstract.h"
27 #include "src/core/lib/gprpp/ref_counted.h"
28
29 namespace grpc_core {
30
31 class Subchannel;
32
33 extern TraceFlag grpc_subchannel_pool_trace;
34
35 // A key that can uniquely identify a subchannel.
36 class SubchannelKey {
37  public:
38   explicit SubchannelKey(const grpc_channel_args* args);
39   ~SubchannelKey();
40
41   // Copyable.
42   SubchannelKey(const SubchannelKey& other);
43   SubchannelKey& operator=(const SubchannelKey& other);
44   // Not movable.
45   SubchannelKey(SubchannelKey&&) = delete;
46   SubchannelKey& operator=(SubchannelKey&&) = delete;
47
48   int Cmp(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:
66   SubchannelPoolInterface() : RefCounted(&grpc_subchannel_pool_trace) {}
67   virtual ~SubchannelPoolInterface() {}
68
69   // Registers a subchannel against a key. Returns the subchannel registered
70   // with \a key, which may be different from \a constructed because we reuse
71   // (instead of update) any existing subchannel already registered with \a key.
72   virtual Subchannel* RegisterSubchannel(SubchannelKey* key,
73                                          Subchannel* constructed) GRPC_ABSTRACT;
74
75   // Removes the registered subchannel found by \a key.
76   virtual void UnregisterSubchannel(SubchannelKey* key) GRPC_ABSTRACT;
77
78   // Finds the subchannel registered for the given subchannel key. Returns NULL
79   // if no such channel exists. Thread-safe.
80   virtual Subchannel* FindSubchannel(SubchannelKey* key) GRPC_ABSTRACT;
81
82   // Creates a channel arg from \a subchannel pool.
83   static grpc_arg CreateChannelArg(SubchannelPoolInterface* subchannel_pool);
84
85   // Gets the subchannel pool from the channel args.
86   static SubchannelPoolInterface* GetSubchannelPoolFromChannelArgs(
87       const grpc_channel_args* args);
88
89   GRPC_ABSTRACT_BASE_CLASS
90 };
91
92 }  // namespace grpc_core
93
94 #endif /* GRPC_CORE_EXT_FILTERS_CLIENT_CHANNEL_SUBCHANNEL_POOL_INTERFACE_H */