浏览代码

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
Tatsuhiro Tsujikawa 10 年之前
父节点
当前提交
af98861aff
共有 6 个文件被更改,包括 134 次插入8 次删除
  1. 10 7
      src/DownloadEngine.cc
  2. 2 0
      src/DownloadEngine.h
  3. 3 0
      src/DownloadEngineFactory.cc
  4. 64 0
      src/EvictSocketPoolCommand.cc
  5. 53 0
      src/EvictSocketPoolCommand.h
  6. 2 1
      src/Makefile.am

+ 10 - 7
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<std::string, SocketPoolEntry>::value_type p(key, entry);
   socketPool_.insert(p);
+}
 
-  if(lastSocketPoolScan_.difference(global::wallclock()) < 1_min) {
+void DownloadEngine::evictSocketPool()
+{
+  if (socketPool_.empty()) {
     return;
   }
+
   std::multimap<std::string, SocketPoolEntry> 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<unsigned long>
-                    (socketPool_.size()-newPool.size())));
-  socketPool_ = newPool;
+                   static_cast<unsigned long>
+                   (socketPool_.size() - newPool.size())));
+  socketPool_ = std::move(newPool);
 }
 
 namespace {

+ 2 - 0
src/DownloadEngine.h

@@ -311,6 +311,8 @@ public:
    uint16_t port,
    const std::string& username);
 
+  void evictSocketPool();
+
   const std::unique_ptr<CookieStorage>& getCookieStorage() const;
 
 #ifdef ENABLE_BITTORRENT

+ 3 - 0
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<CheckIntegrityDispatcherCommand>
                        (e->newCUID(), e->getCheckIntegrityMan().get(),
                         e.get()));
+  e->addRoutineCommand(make_unique<EvictSocketPoolCommand>
+                       (e->newCUID(), e.get(), 30_s));
 
   if(op->getAsInt(PREF_AUTO_SAVE_INTERVAL) > 0) {
     e->addRoutineCommand(make_unique<AutoSaveCommand>(

+ 64 - 0
src/EvictSocketPoolCommand.cc

@@ -0,0 +1,64 @@
+/* <!-- copyright */
+/*
+ * aria2 - The high speed download utility
+ *
+ * Copyright (C) 2015 Tatsuhiro Tsujikawa
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ *
+ * In addition, as a special exception, the copyright holders give
+ * permission to link the code of portions of this program with the
+ * OpenSSL library under certain conditions as described in each
+ * individual source file, and distribute linked combinations
+ * including the two.
+ * You must obey the GNU General Public License in all respects
+ * for all of the code used other than OpenSSL.  If you modify
+ * file(s) with this exception, you may extend this exception to your
+ * version of the file(s), but you are not obligated to do so.  If you
+ * do not wish to do so, delete this exception statement from your
+ * version.  If you delete this exception statement from all source
+ * files in the program, then also delete it here.
+ */
+/* copyright --> */
+#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

+ 53 - 0
src/EvictSocketPoolCommand.h

@@ -0,0 +1,53 @@
+/* <!-- copyright */
+/*
+ * aria2 - The high speed download utility
+ *
+ * Copyright (C) 2015 Tatsuhiro Tsujikawa
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ *
+ * In addition, as a special exception, the copyright holders give
+ * permission to link the code of portions of this program with the
+ * OpenSSL library under certain conditions as described in each
+ * individual source file, and distribute linked combinations
+ * including the two.
+ * You must obey the GNU General Public License in all respects
+ * for all of the code used other than OpenSSL.  If you modify
+ * file(s) with this exception, you may extend this exception to your
+ * version of the file(s), but you are not obligated to do so.  If you
+ * do not wish to do so, delete this exception statement from your
+ * version.  If you delete this exception statement from all source
+ * files in the program, then also delete it here.
+ */
+/* copyright --> */
+#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

+ 2 - 1
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