@@ -34,10 +34,12 @@ using namespace bc::config;
3434
3535// TODO: change to network_address bimap hash table with services and age.
3636hosts::hosts (const settings& settings)
37- : buffer_(std::max(settings.host_pool_capacity, 1u )),
37+ : capacity_(std::min(max_address, static_cast <size_t >(
38+ settings.host_pool_capacity))),
39+ buffer_ (std::max(capacity_, static_cast <size_t >(1u ))),
3840 stopped_(true ),
3941 file_path_(settings.hosts_file),
40- disabled_(settings.host_pool_capacity == 0 )
42+ disabled_(capacity_ == 0 )
4143{
4244}
4345
@@ -78,13 +80,45 @@ code hosts::fetch(address& out) const
7880 return error::not_found;
7981
8082 // Randomly select an address from the buffer.
81- const auto random = pseudo_random (0 , buffer_.size () - 1 );
83+ const auto random = pseudo_random::next (0 , buffer_.size () - 1 );
8284 const auto index = static_cast <size_t >(random);
8385 out = buffer_[index];
8486 return error::success;
8587 // /////////////////////////////////////////////////////////////////////////
8688}
8789
90+ code hosts::fetch (address::list& out) const
91+ {
92+ if (disabled_)
93+ return error::not_found;
94+
95+ // Critical Section
96+ // /////////////////////////////////////////////////////////////////////////
97+ {
98+ shared_lock lock (mutex_);
99+
100+ if (stopped_)
101+ return error::service_stopped;
102+
103+ if (buffer_.empty ())
104+ return error::not_found;
105+
106+ const auto out_count = std::min (buffer_.size (), capacity_) /
107+ static_cast <size_t >(pseudo_random::next (1 , 20 ));
108+
109+ if (out_count == 0 )
110+ return error::success;
111+
112+ out.reserve (out_count);
113+ for (size_t index = 0 ; index < out_count; ++index)
114+ out.push_back (buffer_[index]);
115+ }
116+ // /////////////////////////////////////////////////////////////////////////
117+
118+ pseudo_random::shuffle (out);
119+ return error::success;
120+ }
121+
88122// load
89123code hosts::start ()
90124{
@@ -288,7 +322,7 @@ void hosts::store(const address::list& hosts, result_handler handler)
288322 // Accept between 1 and all of this peer's addresses up to capacity.
289323 const auto capacity = buffer_.capacity ();
290324 const auto usable = std::min (hosts.size (), capacity);
291- const auto random = static_cast <size_t >(pseudo_random (1 , usable));
325+ const auto random = static_cast <size_t >(pseudo_random::next (1 , usable));
292326
293327 // But always accept at least the amount we are short if available.
294328 const auto gap = capacity - buffer_.size ();
0 commit comments