3 * Copyright 2018 gRPC authors.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #ifndef GRPC_CORE_EXT_FILTERS_CLIENT_CHANNEL_SUBCHANNEL_POOL_INTERFACE_H
20 #define GRPC_CORE_EXT_FILTERS_CLIENT_CHANNEL_SUBCHANNEL_POOL_INTERFACE_H
22 #include <grpc/support/port_platform.h>
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"
33 extern TraceFlag grpc_subchannel_pool_trace;
35 // A key that can uniquely identify a subchannel.
38 explicit SubchannelKey(const grpc_channel_args* args);
42 SubchannelKey(const SubchannelKey& other);
43 SubchannelKey& operator=(const SubchannelKey& other);
45 SubchannelKey(SubchannelKey&&) = delete;
46 SubchannelKey& operator=(SubchannelKey&&) = delete;
48 int Cmp(const SubchannelKey& other) const;
51 // Initializes the subchannel key with the given \a args and the function to
54 const grpc_channel_args* args,
55 grpc_channel_args* (*copy_channel_args)(const grpc_channel_args* args));
57 const grpc_channel_args* args_;
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> {
66 SubchannelPoolInterface() : RefCounted(&grpc_subchannel_pool_trace) {}
67 virtual ~SubchannelPoolInterface() {}
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;
75 // Removes the registered subchannel found by \a key.
76 virtual void UnregisterSubchannel(SubchannelKey* key) GRPC_ABSTRACT;
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;
82 // Creates a channel arg from \a subchannel pool.
83 static grpc_arg CreateChannelArg(SubchannelPoolInterface* subchannel_pool);
85 // Gets the subchannel pool from the channel args.
86 static SubchannelPoolInterface* GetSubchannelPoolFromChannelArgs(
87 const grpc_channel_args* args);
89 GRPC_ABSTRACT_BASE_CLASS
92 } // namespace grpc_core
94 #endif /* GRPC_CORE_EXT_FILTERS_CLIENT_CHANNEL_SUBCHANNEL_POOL_INTERFACE_H */