diff --git a/src/DownloadEngine.cc b/src/DownloadEngine.cc index 522c1b2e..3bc7b18e 100644 --- a/src/DownloadEngine.cc +++ b/src/DownloadEngine.cc @@ -311,22 +311,25 @@ void DownloadEngine::poolSocket(const std::string& key, A2_LOG_INFO(fmt("Pool socket for %s", key.c_str())); std::multimap::value_type p(key, entry); socketPool_.insert(p); +} - if(lastSocketPoolScan_.difference(global::wallclock()) < 1_min) { +void DownloadEngine::evictSocketPool() +{ + if (socketPool_.empty()) { return; } + std::multimap newPool; A2_LOG_DEBUG("Scaning SocketPool and erasing timed out entry."); - lastSocketPoolScan_ = global::wallclock(); - for(auto & elem : socketPool_) { - if(!elem.second.isTimeout()) { + for (auto& elem : socketPool_) { + if (!elem.second.isTimeout()) { newPool.insert(elem); } } A2_LOG_DEBUG(fmt("%lu entries removed.", - static_cast - (socketPool_.size()-newPool.size()))); - socketPool_ = newPool; + static_cast + (socketPool_.size() - newPool.size()))); + socketPool_ = std::move(newPool); } namespace { diff --git a/src/DownloadEngine.h b/src/DownloadEngine.h index 749092a2..a6709f5a 100644 --- a/src/DownloadEngine.h +++ b/src/DownloadEngine.h @@ -311,6 +311,8 @@ public: uint16_t port, const std::string& username); + void evictSocketPool(); + const std::unique_ptr& getCookieStorage() const; #ifdef ENABLE_BITTORRENT diff --git a/src/DownloadEngineFactory.cc b/src/DownloadEngineFactory.cc index 6f07c2f6..8bc96e19 100644 --- a/src/DownloadEngineFactory.cc +++ b/src/DownloadEngineFactory.cc @@ -57,6 +57,7 @@ #include "a2io.h" #include "DownloadContext.h" #include "array_fun.h" +#include "EvictSocketPoolCommand.h" #ifdef HAVE_LIBUV # include "LibuvEventPoll.h" #endif // HAVE_LIBUV @@ -164,6 +165,8 @@ DownloadEngineFactory::newDownloadEngine e->addRoutineCommand(make_unique (e->newCUID(), e->getCheckIntegrityMan().get(), e.get())); + e->addRoutineCommand(make_unique + (e->newCUID(), e.get(), 30_s)); if(op->getAsInt(PREF_AUTO_SAVE_INTERVAL) > 0) { e->addRoutineCommand(make_unique( diff --git a/src/EvictSocketPoolCommand.cc b/src/EvictSocketPoolCommand.cc new file mode 100644 index 00000000..59af6843 --- /dev/null +++ b/src/EvictSocketPoolCommand.cc @@ -0,0 +1,64 @@ +/* */ +#include "EvictSocketPoolCommand.h" +#include "RequestGroupMan.h" +#include "DownloadEngine.h" + +namespace aria2 { + +EvictSocketPoolCommand::EvictSocketPoolCommand(cuid_t cuid, DownloadEngine* e, + std::chrono::seconds interval) + : TimeBasedCommand(cuid, e, std::move(interval), true) +{ +} + +EvictSocketPoolCommand::~EvictSocketPoolCommand() +{ +} + +void EvictSocketPoolCommand::preProcess() +{ + if(getDownloadEngine()->getRequestGroupMan()->downloadFinished() || + getDownloadEngine()->isHaltRequested()) { + enableExit(); + } +} + +void EvictSocketPoolCommand::process() +{ + getDownloadEngine()->evictSocketPool(); +} + +} // namespace aria2 diff --git a/src/EvictSocketPoolCommand.h b/src/EvictSocketPoolCommand.h new file mode 100644 index 00000000..c118a393 --- /dev/null +++ b/src/EvictSocketPoolCommand.h @@ -0,0 +1,53 @@ +/* */ +#ifndef D_EVICT_SOCKET_POOL_COMMAND_H +#define D_EVICT_SOCKET_POOL_COMMAND_H + +#include "TimeBasedCommand.h" + +namespace aria2 { + +class EvictSocketPoolCommand : public TimeBasedCommand { +public: + EvictSocketPoolCommand(cuid_t cuid, DownloadEngine* e, + std::chrono::seconds interval); + virtual ~EvictSocketPoolCommand(); + virtual void preProcess() CXX11_OVERRIDE; + virtual void process() CXX11_OVERRIDE; +}; + +} // namespace aria2 + +#endif // D_EVICT_SOCKET_POOL_COMMAND_H diff --git a/src/Makefile.am b/src/Makefile.am index 6850a778..7f253c56 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -268,7 +268,8 @@ SRCS = \ WrDiskCacheEntry.cc WrDiskCacheEntry.h\ XmlRpcRequestParserController.cc XmlRpcRequestParserController.h\ OpenedFileCounter.cc OpenedFileCounter.h \ - SHA1IOFile.cc SHA1IOFile.h + SHA1IOFile.cc SHA1IOFile.h \ + EvictSocketPoolCommand.cc EvictSocketPoolCommand.h if ANDROID SRCS += android/android.c