From 656f8cf9013b2668d3a8682bb2ba9f092d9a9732 Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Mon, 7 Feb 2011 00:22:06 +0900 Subject: [PATCH] Don't calculate maxPeerListSize based on --bt-max-peers. Always use MAX_PEER_LIST_SIZE by default. --- src/DefaultPeerStorage.cc | 29 +++++++++-------------------- src/DefaultPeerStorage.h | 6 ++++++ test/DefaultPeerStorageTest.cc | 2 +- 3 files changed, 16 insertions(+), 21 deletions(-) diff --git a/src/DefaultPeerStorage.cc b/src/DefaultPeerStorage.cc index a67e75b6..1501288d 100644 --- a/src/DefaultPeerStorage.cc +++ b/src/DefaultPeerStorage.cc @@ -55,7 +55,8 @@ const int MAX_PEER_LIST_SIZE = 1024; } // namespace DefaultPeerStorage::DefaultPeerStorage() - : removedPeerSessionDownloadLength_(0), + : maxPeerListSize_(MAX_PEER_LIST_SIZE), + removedPeerSessionDownloadLength_(0), removedPeerSessionUploadLength_(0), seederStateChoke_(new BtSeederStateChoke()), leecherStateChoke_(new BtLeecherStateChoke()), @@ -89,18 +90,6 @@ bool DefaultPeerStorage::isPeerAlreadyAdded(const SharedHandle& peer) FindIdenticalPeer(peer)) != peers_.end(); } -namespace { -size_t calculateMaxPeerListSize(const SharedHandle& btRuntime) -{ - if(!btRuntime) { - return MAX_PEER_LIST_SIZE; - } - return btRuntime->getMaxPeers() == 0 ? - MAX_PEER_LIST_SIZE : - btRuntime->getMaxPeers()+(btRuntime->getMaxPeers() >> 2); -} -} // namespace - bool DefaultPeerStorage::addPeer(const SharedHandle& peer) { if(isPeerAlreadyAdded(peer)) { A2_LOG_DEBUG(fmt("Adding %s:%u is rejected because it has been already" @@ -108,9 +97,9 @@ bool DefaultPeerStorage::addPeer(const SharedHandle& peer) { peer->getIPAddress().c_str(), peer->getPort())); return false; } - size_t maxPeerListSize = calculateMaxPeerListSize(btRuntime_); - if(peers_.size() >= maxPeerListSize) { - deleteUnusedPeer(peers_.size()-maxPeerListSize+1); + const size_t peerListSize = peers_.size(); + if(peerListSize >= maxPeerListSize_) { + deleteUnusedPeer(peerListSize-maxPeerListSize_+1); } peers_.push_front(peer); A2_LOG_DEBUG(fmt("Now peer list contains %lu peers", @@ -120,10 +109,9 @@ bool DefaultPeerStorage::addPeer(const SharedHandle& peer) { void DefaultPeerStorage::addPeer(const std::vector >& peers) { - size_t maxPeerListSize = calculateMaxPeerListSize(btRuntime_); size_t added = 0; for(std::vector >::const_iterator itr = peers.begin(), - eoi = peers.end(); itr != eoi && added < maxPeerListSize; ++itr) { + eoi = peers.end(); itr != eoi && added < maxPeerListSize_; ++itr) { const SharedHandle& peer = *itr; if(isPeerAlreadyAdded(peer)) { A2_LOG_DEBUG(fmt("Adding %s:%u is rejected because it has been already" @@ -137,8 +125,9 @@ void DefaultPeerStorage::addPeer(const std::vector >& peers) peers_.push_front(peer); ++added; } - if(peers_.size() >= maxPeerListSize) { - deleteUnusedPeer(peers_.size()-maxPeerListSize); + const size_t peerListSize = peers_.size(); + if(peerListSize >= maxPeerListSize_) { + deleteUnusedPeer(peerListSize-maxPeerListSize_); } A2_LOG_DEBUG(fmt("Now peer list contains %lu peers", static_cast(peers_.size()))); diff --git a/src/DefaultPeerStorage.h b/src/DefaultPeerStorage.h index bbcf2966..44f5c6d4 100644 --- a/src/DefaultPeerStorage.h +++ b/src/DefaultPeerStorage.h @@ -52,6 +52,7 @@ class DefaultPeerStorage : public PeerStorage { private: SharedHandle btRuntime_; SharedHandle pieceStorage_; + size_t maxPeerListSize_; std::deque > peers_; std::deque > droppedPeers_; uint64_t removedPeerSessionDownloadLength_; @@ -113,6 +114,11 @@ public: void setPieceStorage(const SharedHandle& pieceStorage); void setBtRuntime(const SharedHandle& btRuntime); + + void setMaxPeerListSize(size_t maxPeerListSize) + { + maxPeerListSize_ = maxPeerListSize; + } }; } // namespace aria2 diff --git a/test/DefaultPeerStorageTest.cc b/test/DefaultPeerStorageTest.cc index f12bf48d..b38cf5cd 100644 --- a/test/DefaultPeerStorageTest.cc +++ b/test/DefaultPeerStorageTest.cc @@ -98,7 +98,7 @@ void DefaultPeerStorageTest::testDeleteUnusedPeer() { void DefaultPeerStorageTest::testAddPeer() { DefaultPeerStorage ps; SharedHandle btRuntime(new BtRuntime()); - btRuntime->setMaxPeers(3); + ps.setMaxPeerListSize(3); ps.setBtRuntime(btRuntime); SharedHandle peer1(new Peer("192.168.0.1", 6889));