From af98861aff50737f87d9c251ee9a68280bba3e64 Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Wed, 11 Nov 2015 22:31:00 +0900 Subject: Evict timed out pooled socket periodically Previously we only scanned pool socket to check they are timed out when we pooled another socket. This means that pooled socket is not closed long time (stays in CLOSE-WAIT state) if we don't pool any more socket. In this commit, we now check pooled socket periodically (30 seconds) to avoid the sockets hanging in CLOSE-WAIT state long time. See GH-477 --- src/DownloadEngine.cc | 17 +++++++----- src/DownloadEngine.h | 2 ++ src/DownloadEngineFactory.cc | 3 ++ src/EvictSocketPoolCommand.cc | 64 +++++++++++++++++++++++++++++++++++++++++++ src/EvictSocketPoolCommand.h | 53 +++++++++++++++++++++++++++++++++++ src/Makefile.am | 3 +- 6 files changed, 134 insertions(+), 8 deletions(-) create mode 100644 src/EvictSocketPoolCommand.cc create mode 100644 src/EvictSocketPoolCommand.h 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 -- cgit