From 54435a73b848ee16d78dfca1817f7f33fe6f82f0 Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Sun, 20 Mar 2011 07:43:57 +0000 Subject: [PATCH] optimization for kick-starting sockets to download again after having been blocked by the disk --- src/session_impl.cpp | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/session_impl.cpp b/src/session_impl.cpp index 026a4a502..4473ae1fa 100644 --- a/src/session_impl.cpp +++ b/src/session_impl.cpp @@ -2457,12 +2457,20 @@ namespace aux { conns.push_back(p); } - for (std::vector::iterator i = conns.begin() - , end(conns.end()); i != end; ++i) + // pick a random peer to start with, to evenly distribute + // the disk bandwidth + std::vector::iterator peer = conns.begin() + (rand() % conns.size()); + for (int i = 0; i < conns.size(); ++i) { + // if we can't write to disk anymore, no need + // to keep iterating + if (!can_write_to_disk()) break; + // setup_receive() may disconnect the connection // and clear it out from the m_connections list - (*i)->on_disk(); + (*peer)->on_disk(); + ++peer; + if (peer == conns.end()) peer = conns.begin(); } }