@@ -34,6 +34,7 @@ template <typename T>
3434class ChannelUsageWrapper
3535 : public std::enable_shared_from_this<ChannelUsageWrapper<T>> {
3636 public:
37+ ChannelUsageWrapper () = default ;
3738 explicit ChannelUsageWrapper (std::shared_ptr<T> stub)
3839 : stub_(std::move(stub)) {}
3940
@@ -55,6 +56,11 @@ class ChannelUsageWrapper
5556 last_refresh_status_ = std::move (s);
5657 }
5758
59+ ChannelUsageWrapper& set_channel (std::shared_ptr<T> channel) {
60+ stub_ = std::move (channel);
61+ return *this ;
62+ }
63+
5864 std::weak_ptr<ChannelUsageWrapper<T>> MakeWeak () {
5965 return this ->shared_from_this ();
6066 }
@@ -81,7 +87,8 @@ template <typename T>
8187class DynamicChannelPool
8288 : public std::enable_shared_from_this<DynamicChannelPool<T>> {
8389 public:
84- using StubFactoryFn = std::function<std::shared_ptr<T>(int id)>;
90+ using StubFactoryFn =
91+ std::function<std::shared_ptr<ChannelUsageWrapper<T>>(std::uint32_t id)>;
8592 struct SizingPolicy {
8693 // To avoid channel churn, the pool will not add or remove channels more
8794 // frequently that this period.
@@ -117,26 +124,8 @@ class DynamicChannelPool
117124 };
118125
119126 static std::shared_ptr<DynamicChannelPool> Create (
120- CompletionQueue cq, std::size_t initial_size,
121- StubFactoryFn stub_factory_fn,
122- std::shared_ptr<ConnectionRefreshState> refresh_state,
123- SizingPolicy sizing_policy = {}) {
124- std::cout << __PRETTY_FUNCTION__ << " : enter" << std::endl;
125- std::vector<std::shared_ptr<ChannelUsageWrapper<T>>>
126- initial_wrapped_channels;
127- for (std::size_t i = 0 ; i < initial_size; ++i) {
128- initial_wrapped_channels.emplace_back (stub_factory_fn ());
129- }
130- auto pool = std::shared_ptr<DynamicChannelPool>(new DynamicChannelPool (
131- std::move (cq), std::move (initial_wrapped_channels),
132- std::move (refresh_state), std::move (stub_factory_fn),
133- std::move (sizing_policy)));
134- std::cout << __PRETTY_FUNCTION__ << " : return pool" << std::endl;
135- return pool;
136- }
137-
138- static std::shared_ptr<DynamicChannelPool> Create (
139- CompletionQueue cq, std::vector<std::shared_ptr<T>> initial_channels,
127+ CompletionQueue cq,
128+ std::vector<std::shared_ptr<ChannelUsageWrapper<T>>> initial_channels,
140129 std::shared_ptr<ConnectionRefreshState> refresh_state,
141130 StubFactoryFn stub_factory_fn, SizingPolicy sizing_policy = {}) {
142131 std::cout << __PRETTY_FUNCTION__ << " : enter" << std::endl;
@@ -193,26 +182,22 @@ class DynamicChannelPool
193182 std::iota (iterators.begin (), iterators.end (), channels_.begin ());
194183 std::shuffle (iterators.begin (), iterators.end (), rng_);
195184
196- // std::cout << __PRETTY_FUNCTION__ << ": shuffled iterators" <<
197- // std::endl;
198- // std::vector<
199- // typename
200- // std::vector<std::shared_ptr<ChannelUsageWrapper<T>>>::iterator>
201- // iterators;
202- typename std::vector<typename std::vector<
203- std::shared_ptr<ChannelUsageWrapper<T>>>::iterator>::iterator
204- shuffle_iter = iterators.begin ();
205- typename std::vector<std::shared_ptr<ChannelUsageWrapper<T>>>::iterator
206- channel_1 = *shuffle_iter;
185+ // typename std::vector<typename std::vector<
186+ // std::shared_ptr<ChannelUsageWrapper<T>>>::iterator>::iterator
187+ auto shuffle_iter = iterators.begin ();
188+ // typename
189+ // std::vector<std::shared_ptr<ChannelUsageWrapper<T>>>::iterator
190+ auto channel_1 = *shuffle_iter;
207191 std::shared_ptr<ChannelUsageWrapper<T>> c = *channel_1;
208192 // std::cout << __PRETTY_FUNCTION__
209193 // << ": check channel 1=" << c.get() << std::endl;
210194 auto channel_1_rpcs = shuffle_iter != iterators.end ()
211195 ? (*channel_1)->outstanding_rpcs ()
212196 : Status{StatusCode::kNotFound , " " };
213197 ++shuffle_iter;
214- typename std::vector<std::shared_ptr<ChannelUsageWrapper<T>>>::iterator
215- channel_2 = *shuffle_iter;
198+ // typename
199+ // std::vector<std::shared_ptr<ChannelUsageWrapper<T>>>::iterator
200+ auto channel_2 = *shuffle_iter;
216201 // We want to snapshot these outstanding_rpcs values.
217202 // std::cout << __PRETTY_FUNCTION__
218203 // << ": check channel 2=" << (channel_2)->get() << std::endl;
@@ -264,7 +249,7 @@ class DynamicChannelPool
264249 return *channel_2;
265250 }
266251
267- // TODO: we have no usable channels in the entire pool; this is bad.
252+ // TODO(sdhart) : we have no usable channels in the entire pool; this is bad.
268253 std::cout << __PRETTY_FUNCTION__ << " : NO USABLE CHANNELS" << std::endl;
269254 return nullptr ;
270255 }
@@ -280,26 +265,7 @@ class DynamicChannelPool
280265 stub_factory_fn_(std::move(stub_factory_fn)),
281266 channels_(std::move(initial_wrapped_channels)),
282267 sizing_policy_(std::move(sizing_policy)),
283- next_channel_id_(channels_.size()) {
284- sizing_policy_.minimum_channel_pool_size = channels_.size ();
285- }
286-
287- DynamicChannelPool (CompletionQueue cq,
288- std::vector<std::shared_ptr<T>> initial_channels,
289- std::shared_ptr<ConnectionRefreshState> refresh_state,
290- StubFactoryFn stub_factory_fn, SizingPolicy sizing_policy)
291- : cq_(std::move(cq)),
292- refresh_state_(std::move(refresh_state)),
293- stub_factory_fn_(std::move(stub_factory_fn)),
294- channels_(),
295- sizing_policy_(std::move(sizing_policy)),
296- next_channel_id_(static_cast <int >(initial_channels.size())) {
297- std::cout << __PRETTY_FUNCTION__ << " : wrap initial_channels" << std::endl;
298- channels_.reserve (initial_channels.size ());
299- for (auto & channel : initial_channels) {
300- channels_.push_back (
301- std::make_shared<ChannelUsageWrapper<T>>(std::move (channel)));
302- }
268+ next_channel_id_(static_cast <std::uint32_t >(channels_.size())) {
303269 sizing_policy_.minimum_channel_pool_size = channels_.size ();
304270 }
305271
@@ -348,8 +314,8 @@ class DynamicChannelPool
348314 std::vector<std::shared_ptr<ChannelUsageWrapper<T>>> new_stubs;
349315 new_stubs.reserve (new_channel_ids.size ());
350316 for (auto const & id : new_channel_ids) {
351- new_stubs.push_back (
352- std::make_shared<ChannelUsageWrapper<T>>(stub_factory_fn_ (id)));
317+ new_stubs.push_back (stub_factory_fn_ (id));
318+ // std::make_shared<ChannelUsageWrapper<T>>(stub_factory_fn_(id)));
353319 }
354320 std::unique_lock<std::mutex> lk (mu_);
355321 channels_.insert (channels_.end (),
@@ -391,7 +357,8 @@ class DynamicChannelPool
391357 }
392358 draining_channels_.pop_back ();
393359 }
394- // TODO: If iterators becomes a member variable perhaps add logic to call
360+ // TODO(sdhart): If iterators becomes a member variable perhaps add logic to
361+ // call
395362 // shrink_to_fit on iterators_ if there's a large
396363 // difference between iterators_.capacity and channels_.size
397364 }
@@ -446,7 +413,7 @@ class DynamicChannelPool
446413 future<void > remove_channel_poll_timer_;
447414 absl::optional<future<StatusOr<std::chrono::system_clock::time_point>>>
448415 pool_resize_cooldown_timer_ = absl::nullopt ;
449- int next_channel_id_;
416+ std:: uint32_t next_channel_id_;
450417 // std::vector<
451418 // typename
452419 // std::vector<std::shared_ptr<ChannelUsageWrapper<T>>>::iterator>
0 commit comments