diff options
| author | Tatsuhiro Tsujikawa <tatsuhiro.t@gmail.com> | 2011-06-28 00:18:53 +0900 |
|---|---|---|
| committer | Tatsuhiro Tsujikawa <tatsuhiro.t@gmail.com> | 2011-06-28 00:21:29 +0900 |
| commit | 2906484345007c734c3ff75a6080fbab2d8f1a05 (patch) | |
| tree | 080529237cc23b060de04e4c29d2c9e246736861 /src/FileEntry.cc | |
| parent | ba6ab142352be06f518b46a489ec7a7809613e75 (diff) | |
Use ServerStat to find faster server.
Diffstat (limited to 'src/FileEntry.cc')
| -rw-r--r-- | src/FileEntry.cc | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/src/FileEntry.cc b/src/FileEntry.cc index 262ae890..f69466b8 100644 --- a/src/FileEntry.cc +++ b/src/FileEntry.cc @@ -46,6 +46,8 @@ #include "uri.h" #include "PeerStat.h" #include "fmt.h" +#include "ServerStatMan.h" +#include "ServerStat.h" namespace aria2 { @@ -221,6 +223,57 @@ FileEntry::findFasterRequest(const SharedHandle<Request>& base) return SharedHandle<Request>(); } +SharedHandle<Request> +FileEntry::findFasterRequest +(const SharedHandle<Request>& base, + const std::vector<std::pair<size_t, std::string> >& usedHosts, + const SharedHandle<ServerStatMan>& serverStatMan) +{ + const int startupIdleTime = 10; + const unsigned int SPEED_THRESHOLD = 20*1024; + if(lastFasterReplace_.difference(global::wallclock) < startupIdleTime) { + return SharedHandle<Request>(); + } + const SharedHandle<PeerStat>& basestat = base->getPeerStat(); + A2_LOG_DEBUG("Search faster server using ServerStat."); + // Use first 10 good URIs to introduce some randomness. + const size_t NUM_URI = 10; + std::vector<std::pair<SharedHandle<ServerStat>, std::string> > fastCands; + std::vector<std::string> normCands; + for(std::deque<std::string>::const_iterator i = uris_.begin(), + eoi = uris_.end(); i != eoi && fastCands.size() < NUM_URI; ++i) { + uri::UriStruct us; + if(!uri::parse(us, *i)) { + continue; + } + if(findSecond(usedHosts.begin(), usedHosts.end(), us.host) != + usedHosts.end()) { + A2_LOG_DEBUG(fmt("%s is in usedHosts, not considered", (*i).c_str())); + continue; + } + SharedHandle<ServerStat> ss = serverStatMan->find(us.host, us.protocol); + if(ss && ss->isOK()) { + if((basestat && + ss->getDownloadSpeed() > basestat->calculateDownloadSpeed()*1.5) || + (!basestat && ss->getDownloadSpeed() > SPEED_THRESHOLD)) { + fastCands.push_back(std::make_pair(ss, *i)); + } + } + } + if(!fastCands.empty()) { + A2_LOG_DEBUG("Selected from fastCands"); + std::sort(fastCands.begin(), fastCands.end(), ServerStatFaster()); + SharedHandle<Request> fastestRequest(new Request()); + fastestRequest->setUri(fastCands.front().second); + fastestRequest->setReferer(base->getReferer()); + inFlightRequests_.push_back(fastestRequest); + lastFasterReplace_ = global::wallclock; + return fastestRequest; + } + A2_LOG_DEBUG("No faster server found."); + return SharedHandle<Request>(); +} + namespace { class RequestFaster { public: |
