mirror of https://github.com/aria2/aria2
2008-05-08 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
Added timeout to socket pool. The default timeout is 15 seconds, which is the same value Apache uses. * src/DownloadEngine.cc * src/DownloadEngine.hpull/1/head
parent
8fab8859b1
commit
2c54667beb
13
ChangeLog
13
ChangeLog
|
@ -1,3 +1,16 @@
|
|||
2008-05-08 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
|
||||
|
||||
Added timeout to socket pool. The default timeout is 15 seconds,
|
||||
which is the same value Apache uses.
|
||||
* src/DownloadEngine.cc
|
||||
* src/DownloadEngine.h
|
||||
|
||||
2008-05-08 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
|
||||
|
||||
Fixed misuse of multimap::find()
|
||||
* src/HttpHeader.cc
|
||||
* test/HttpHeaderTest.cc
|
||||
|
||||
2008-05-08 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
|
||||
|
||||
Rewritten name resolver. Now async DNS can be disabled by --async-dns
|
||||
|
|
|
@ -368,29 +368,36 @@ void DownloadEngine::addRoutineCommand(Command* command)
|
|||
}
|
||||
|
||||
void DownloadEngine::poolSocket(const std::string& ipaddr, uint16_t port,
|
||||
const SharedHandle<SocketCore>& sock)
|
||||
const SharedHandle<SocketCore>& sock,
|
||||
time_t timeout)
|
||||
{
|
||||
std::string addr = ipaddr+":"+Util::uitos(port);
|
||||
logger->info("Pool socket for %s", addr.c_str());
|
||||
std::multimap<std::string, SharedHandle<SocketCore> >::value_type newPair
|
||||
(addr, sock);
|
||||
_socketPool.insert(newPair);
|
||||
|
||||
SocketPoolEntry e(sock, timeout);
|
||||
std::multimap<std::string, SocketPoolEntry>::value_type p(addr, e);
|
||||
_socketPool.insert(p);
|
||||
}
|
||||
|
||||
SharedHandle<SocketCore>
|
||||
DownloadEngine::popPooledSocket(const std::string& ipaddr, uint16_t port)
|
||||
{
|
||||
SharedHandle<SocketCore> s;
|
||||
std::string addr = ipaddr+":"+Util::uitos(port);
|
||||
std::multimap<std::string, SharedHandle<SocketCore> >::iterator i =
|
||||
_socketPool.find(addr);
|
||||
if(i == _socketPool.end()) {
|
||||
return SharedHandle<SocketCore>();
|
||||
} else {
|
||||
logger->info("Reuse socket for %s", addr.c_str());
|
||||
SharedHandle<SocketCore> s = (*i).second;
|
||||
_socketPool.erase(i);
|
||||
return s;
|
||||
|
||||
std::multimap<std::string, SocketPoolEntry>::iterator first = _socketPool.find(addr);
|
||||
|
||||
for(std::multimap<std::string, SocketPoolEntry>::iterator i = first;
|
||||
i != _socketPool.end() && (*i).first == addr; ++i) {
|
||||
const SocketPoolEntry& e = (*i).second;
|
||||
if(!e.isTimeout()) {
|
||||
logger->info("Reuse socket for %s", addr.c_str());
|
||||
s = e.getSocket();
|
||||
_socketPool.erase(first, ++i);
|
||||
break;
|
||||
}
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
SharedHandle<SocketCore>
|
||||
|
@ -407,4 +414,22 @@ DownloadEngine::popPooledSocket
|
|||
return SharedHandle<SocketCore>();
|
||||
}
|
||||
|
||||
DownloadEngine::SocketPoolEntry::SocketPoolEntry
|
||||
(const SharedHandle<SocketCore>& socket,
|
||||
time_t timeout):
|
||||
_socket(socket),
|
||||
_timeout(timeout) {}
|
||||
|
||||
DownloadEngine::SocketPoolEntry::~SocketPoolEntry() {}
|
||||
|
||||
bool DownloadEngine::SocketPoolEntry::isTimeout() const
|
||||
{
|
||||
return _registeredTime.elapsed(_timeout);
|
||||
}
|
||||
|
||||
SharedHandle<SocketCore> DownloadEngine::SocketPoolEntry::getSocket() const
|
||||
{
|
||||
return _socket;
|
||||
}
|
||||
|
||||
} // namespace aria2
|
||||
|
|
|
@ -39,6 +39,7 @@
|
|||
#include "SharedHandle.h"
|
||||
#include "Command.h"
|
||||
#include "a2netcompat.h"
|
||||
#include "TimeA2.h"
|
||||
#include <deque>
|
||||
#include <map>
|
||||
|
||||
|
@ -107,8 +108,26 @@ private:
|
|||
|
||||
bool _haltRequested;
|
||||
|
||||
// key = IP address:port, value = Socket
|
||||
std::multimap<std::string, SharedHandle<SocketCore> > _socketPool;
|
||||
class SocketPoolEntry {
|
||||
private:
|
||||
SharedHandle<SocketCore> _socket;
|
||||
|
||||
time_t _timeout;
|
||||
|
||||
Time _registeredTime;
|
||||
public:
|
||||
SocketPoolEntry(const SharedHandle<SocketCore>& socket,
|
||||
time_t timeout);
|
||||
|
||||
~SocketPoolEntry();
|
||||
|
||||
bool isTimeout() const;
|
||||
|
||||
SharedHandle<SocketCore> getSocket() const;
|
||||
};
|
||||
|
||||
// key = IP address:port, value = SocketPoolEntry
|
||||
std::multimap<std::string, SocketPoolEntry> _socketPool;
|
||||
|
||||
void shortSleep() const;
|
||||
bool addSocket(const SocketEntry& socketEntry);
|
||||
|
@ -178,7 +197,7 @@ public:
|
|||
void addRoutineCommand(Command* command);
|
||||
|
||||
void poolSocket(const std::string& ipaddr, uint16_t port,
|
||||
const SharedHandle<SocketCore>& sock);
|
||||
const SharedHandle<SocketCore>& sock, time_t timeout = 15);
|
||||
|
||||
SharedHandle<SocketCore> popPooledSocket(const std::string& ipaddr,
|
||||
uint16_t port);
|
||||
|
|
Loading…
Reference in New Issue