@@ -74,27 +74,30 @@ template <typename T>
7474class DynamicChannelPool
7575 : public std::enable_shared_from_this<DynamicChannelPool<T>> {
7676 public:
77- using StubFactoryFn = std::function<std::shared_ptr<T>()>;
77+ using StubFactoryFn = std::function<std::shared_ptr<T>(int id )>;
7878 struct SizingPolicy {
7979 // To avoid channel churn, the pool will not add or remove channels more
8080 // frequently that this period.
81- std::chrono::milliseconds resize_cooldown_interval;
81+ std::chrono::milliseconds resize_cooldown_interval =
82+ std::chrono::milliseconds (60 * 1000 );
8283
8384 // If the average number of outstanding RPCs is below this threshold,
8485 // the pool size will be decreased.
85- std::size_t minimum_average_outstanding_rpcs_per_channel;
86+ std::size_t minimum_average_outstanding_rpcs_per_channel = 20 ;
8687 // If the average number of outstanding RPCs is above this threshold,
8788 // the pool size will be increased.
88- std::size_t maximum_average_outstanding_rpcs_per_channel;
89+ std::size_t maximum_average_outstanding_rpcs_per_channel = 80 ;
8990
9091 // When channels are removed from the pool, we have to wait until all
9192 // outstanding RPCs on that channel are completed before destroying it.
92- std::chrono::milliseconds decommissioned_channel_polling_interval;
93+ std::chrono::milliseconds decommissioned_channel_polling_interval =
94+ std::chrono::milliseconds (30 * 1000 );
9395 };
9496
9597 static std::shared_ptr<DynamicChannelPool> Create (
9698 CompletionQueue cq, std::size_t initial_size, StubFactoryFn factory_fn,
9799 SizingPolicy sizing_policy = {}) {
100+ std::cout << __PRETTY_FUNCTION__ << std::endl;
98101 std::vector<std::shared_ptr<StubWrapper<T>>> initial_wrapped_channels;
99102 for (std::size_t i = 0 ; i < initial_size; ++i) {
100103 initial_wrapped_channels.emplace_back (factory_fn ());
@@ -103,9 +106,11 @@ class DynamicChannelPool
103106 std::move (cq), std::move (initial_wrapped_channels),
104107 std::move (factory_fn), std::move (sizing_policy)));
105108 }
109+
106110 static std::shared_ptr<DynamicChannelPool> Create (
107111 CompletionQueue cq, std::vector<std::shared_ptr<T>> initial_channels,
108112 StubFactoryFn factory_fn, SizingPolicy sizing_policy = {}) {
113+ std::cout << __PRETTY_FUNCTION__ << std::endl;
109114 auto pool = std::shared_ptr<DynamicChannelPool>(new DynamicChannelPool (
110115 std::move (cq), std::move (initial_channels), std::move (factory_fn),
111116 std::move (sizing_policy)));
@@ -132,16 +137,20 @@ class DynamicChannelPool
132137 // }
133138
134139 std::shared_ptr<StubWrapper<T>> GetChannelRandomTwoLeastUsed () {
140+ std::cout << __PRETTY_FUNCTION__ << std::endl;
135141 std::unique_lock<std::mutex> lk (mu_);
136142
143+ std::cout << __PRETTY_FUNCTION__ << " : channels_size()=" << channels_.size ()
144+ << std::endl;
137145 // TODO: check if resize is needed.
138146
139147 std::vector<std::size_t > indices (channels_.size ());
140148 // TODO(sdhart): Maybe use iota on iterators instead of indices
141149 std::iota (indices.begin (), indices.end (), 0 );
142150 std::shuffle (indices.begin (), indices.end (), rng_);
143- auto channel_1 = channels_[indices[0 ]];
144- auto channel_2 = channels_[indices[1 ]];
151+
152+ std::shared_ptr<StubWrapper<T>> channel_1 = channels_[indices[0 ]];
153+ std::shared_ptr<StubWrapper<T>> channel_2 = channels_[indices[1 ]];
145154
146155 return channel_1->outstanding_rpcs (lk) < channel_2->outstanding_rpcs (lk)
147156 ? channel_1
@@ -156,18 +165,26 @@ class DynamicChannelPool
156165 : cq_(std::move(cq)),
157166 factory_fn_ (std::move(factory_fn)),
158167 channels_(std::move(initial_wrapped_channels)),
159- sizing_policy_(std::move(sizing_policy)) {}
168+ sizing_policy_(std::move(sizing_policy)),
169+ next_channel_id_(channels_.size()) {}
160170
161171 DynamicChannelPool (CompletionQueue cq,
162172 std::vector<std::shared_ptr<T>> initial_channels,
163173 StubFactoryFn factory_fn, SizingPolicy sizing_policy)
164174 : cq_(std::move(cq)),
165175 factory_fn_(std::move(factory_fn)),
166- channels_(initial_channels.size()),
167- sizing_policy_(std::move(sizing_policy)) {
176+ channels_(),
177+ sizing_policy_(std::move(sizing_policy)),
178+ next_channel_id_(initial_channels.size()) {
179+ std::cout << __PRETTY_FUNCTION__ << " : wrap initial_channels" << std::endl;
180+ channels_.reserve (initial_channels.size ());
168181 for (auto & channel : initial_channels) {
169- channels_.push_back (std::make_shared<StubWrapper<T>>(channel));
182+ channels_.push_back (std::make_shared<StubWrapper<T>>(std::move ( channel) ));
170183 }
184+ // for (auto i = 0; i < channels_.size(); ++i) {
185+ // std::cout << __PRETTY_FUNCTION__ << ": channels_[" << i <<
186+ // "].get()=" << channels_[i].get() << std::endl;
187+ // }
171188 }
172189
173190 void ScheduleAddChannel () {}
@@ -234,6 +251,7 @@ class DynamicChannelPool
234251 SizingPolicy sizing_policy_;
235252 std::vector<std::shared_ptr<StubWrapper<T>>> decommissioned_channels_;
236253 future<StatusOr<std::chrono::system_clock::time_point>> decommission_timer_;
254+ int next_channel_id_;
237255};
238256
239257} // namespace internal
0 commit comments