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
pull/481/head
Tatsuhiro Tsujikawa 2015-11-11 22:31:00 +09:00
parent 269520ee69
commit af98861aff
6 changed files with 134 additions and 8 deletions

View File

@ -311,22 +311,25 @@ void DownloadEngine::poolSocket(const std::string& key,
A2_LOG_INFO(fmt("Pool socket for %s", key.c_str())); A2_LOG_INFO(fmt("Pool socket for %s", key.c_str()));
std::multimap<std::string, SocketPoolEntry>::value_type p(key, entry); std::multimap<std::string, SocketPoolEntry>::value_type p(key, entry);
socketPool_.insert(p); socketPool_.insert(p);
}
if(lastSocketPoolScan_.difference(global::wallclock()) < 1_min) { void DownloadEngine::evictSocketPool()
{
if (socketPool_.empty()) {
return; return;
} }
std::multimap<std::string, SocketPoolEntry> newPool; std::multimap<std::string, SocketPoolEntry> newPool;
A2_LOG_DEBUG("Scaning SocketPool and erasing timed out entry."); A2_LOG_DEBUG("Scaning SocketPool and erasing timed out entry.");
lastSocketPoolScan_ = global::wallclock(); for (auto& elem : socketPool_) {
for(auto & elem : socketPool_) { if (!elem.second.isTimeout()) {
if(!elem.second.isTimeout()) {
newPool.insert(elem); newPool.insert(elem);
} }
} }
A2_LOG_DEBUG(fmt("%lu entries removed.", A2_LOG_DEBUG(fmt("%lu entries removed.",
static_cast<unsigned long> static_cast<unsigned long>
(socketPool_.size()-newPool.size()))); (socketPool_.size() - newPool.size())));
socketPool_ = newPool; socketPool_ = std::move(newPool);
} }
namespace { namespace {

View File

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

View File

@ -57,6 +57,7 @@
#include "a2io.h" #include "a2io.h"
#include "DownloadContext.h" #include "DownloadContext.h"
#include "array_fun.h" #include "array_fun.h"
#include "EvictSocketPoolCommand.h"
#ifdef HAVE_LIBUV #ifdef HAVE_LIBUV
# include "LibuvEventPoll.h" # include "LibuvEventPoll.h"
#endif // HAVE_LIBUV #endif // HAVE_LIBUV
@ -164,6 +165,8 @@ DownloadEngineFactory::newDownloadEngine
e->addRoutineCommand(make_unique<CheckIntegrityDispatcherCommand> e->addRoutineCommand(make_unique<CheckIntegrityDispatcherCommand>
(e->newCUID(), e->getCheckIntegrityMan().get(), (e->newCUID(), e->getCheckIntegrityMan().get(),
e.get())); e.get()));
e->addRoutineCommand(make_unique<EvictSocketPoolCommand>
(e->newCUID(), e.get(), 30_s));
if(op->getAsInt(PREF_AUTO_SAVE_INTERVAL) > 0) { if(op->getAsInt(PREF_AUTO_SAVE_INTERVAL) > 0) {
e->addRoutineCommand(make_unique<AutoSaveCommand>( e->addRoutineCommand(make_unique<AutoSaveCommand>(

View File

@ -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

View File

@ -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

View File

@ -268,7 +268,8 @@ SRCS = \
WrDiskCacheEntry.cc WrDiskCacheEntry.h\ WrDiskCacheEntry.cc WrDiskCacheEntry.h\
XmlRpcRequestParserController.cc XmlRpcRequestParserController.h\ XmlRpcRequestParserController.cc XmlRpcRequestParserController.h\
OpenedFileCounter.cc OpenedFileCounter.h \ OpenedFileCounter.cc OpenedFileCounter.h \
SHA1IOFile.cc SHA1IOFile.h SHA1IOFile.cc SHA1IOFile.h \
EvictSocketPoolCommand.cc EvictSocketPoolCommand.h
if ANDROID if ANDROID
SRCS += android/android.c SRCS += android/android.c