Browse Source

clean-up datagram session toghters with leasesets and tags

pull/633/head
orignal 8 years ago
parent
commit
bee407ea34
  1. 26
      Datagram.cpp
  2. 12
      Datagram.h
  3. 7
      Destination.cpp
  4. 2
      Destination.h

26
Datagram.cpp

@ -12,16 +12,12 @@ namespace i2p @@ -12,16 +12,12 @@ namespace i2p
namespace datagram
{
DatagramDestination::DatagramDestination (std::shared_ptr<i2p::client::ClientDestination> owner):
m_Owner (owner.get()),
m_CleanupTimer(owner->GetService()),
m_Receiver (nullptr)
m_Owner (owner.get()), m_Receiver (nullptr)
{
ScheduleCleanup();
}
DatagramDestination::~DatagramDestination ()
{
m_CleanupTimer.cancel();
m_Sessions.clear();
}
@ -120,34 +116,28 @@ namespace datagram @@ -120,34 +116,28 @@ namespace datagram
return msg;
}
void DatagramDestination::ScheduleCleanup()
void DatagramDestination::CleanUp ()
{
m_CleanupTimer.expires_from_now(boost::posix_time::seconds(DATAGRAM_SESSION_CLEANUP_INTERVAL));
m_CleanupTimer.async_wait(std::bind(&DatagramDestination::HandleCleanUp, this, std::placeholders::_1));
}
void DatagramDestination::HandleCleanUp(const boost::system::error_code & ecode)
std::vector<i2p::data::IdentHash> expiredSessions;
{
if(ecode)
return;
std::lock_guard<std::mutex> lock(m_SessionsMutex);
auto now = i2p::util::GetMillisecondsSinceEpoch();
LogPrint(eLogDebug, "DatagramDestination: clean up sessions");
std::vector<i2p::data::IdentHash> expiredSessions;
// for each session ...
for (auto & e : m_Sessions) {
for (auto & e : m_Sessions)
{
// check if expired
if(now - e.second->LastActivity() >= DATAGRAM_SESSION_MAX_IDLE)
expiredSessions.push_back(e.first); // we are expired
}
}
// for each expired session ...
for (auto & ident : expiredSessions) {
for (auto & ident : expiredSessions)
{
// remove the expired session
LogPrint(eLogInfo, "DatagramDestination: expiring idle session with ", ident.ToBase32());
m_Sessions.erase(ident);
}
m_Owner->CleanupExpiredTags();
ScheduleCleanup();
}
std::shared_ptr<DatagramSession> DatagramDestination::ObtainSession(const i2p::data::IdentHash & ident)

12
Datagram.h

@ -19,9 +19,6 @@ namespace client @@ -19,9 +19,6 @@ namespace client
}
namespace datagram
{
// seconds interval for cleanup timer
const int DATAGRAM_SESSION_CLEANUP_INTERVAL = 3;
// milliseconds for max session idle time
const uint64_t DATAGRAM_SESSION_MAX_IDLE = 10 * 60 * 1000;
// milliseconds for how long we try sticking to a dead routing path before trying to switch
@ -127,12 +124,10 @@ namespace datagram @@ -127,12 +124,10 @@ namespace datagram
std::shared_ptr<DatagramSession::Info> GetInfoForRemote(const i2p::data::IdentHash & remote);
private:
// clean up after next tick
void ScheduleCleanup();
// clean up stale sessions
void CleanUp ();
// clean up stale sessions and expire tags
void HandleCleanUp(const boost::system::error_code & ecode);
private:
std::shared_ptr<DatagramSession> ObtainSession(const i2p::data::IdentHash & ident);
@ -145,7 +140,6 @@ namespace datagram @@ -145,7 +140,6 @@ namespace datagram
private:
i2p::client::ClientDestination * m_Owner;
boost::asio::deadline_timer m_CleanupTimer;
Receiver m_Receiver; // default
std::mutex m_SessionsMutex;
std::map<i2p::data::IdentHash, std::shared_ptr<DatagramSession> > m_Sessions;

7
Destination.cpp

@ -641,6 +641,7 @@ namespace client @@ -641,6 +641,7 @@ namespace client
{
CleanupExpiredTags ();
CleanupRemoteLeaseSets ();
CleanupDestination ();
m_CleanupTimer.expires_from_now (boost::posix_time::minutes (DESTINATION_CLEANUP_TIMEOUT));
m_CleanupTimer.async_wait (std::bind (&LeaseSetDestination::HandleCleanupTimer,
shared_from_this (), std::placeholders::_1));
@ -892,5 +893,11 @@ namespace client @@ -892,5 +893,11 @@ namespace client
Sign (leaseSet->GetBuffer (), leaseSet->GetBufferLen () - leaseSet->GetSignatureLen (), leaseSet->GetSignature ()); // TODO
SetLeaseSet (leaseSet);
}
void ClientDestination::CleanupDestination ()
{
if (m_DatagramDestination) m_DatagramDestination->CleanUp ();
}
}
}

2
Destination.h

@ -96,6 +96,7 @@ namespace client @@ -96,6 +96,7 @@ namespace client
protected:
void SetLeaseSet (i2p::data::LocalLeaseSet * newLeaseSet);
virtual void CleanupDestination () {}; // additional clean up in derived classes
// I2CP
virtual void HandleDataMessage (const uint8_t * buf, size_t len) = 0;
virtual void CreateNewLeaseSet (std::vector<std::shared_ptr<i2p::tunnel::InboundTunnel> > tunnels) = 0;
@ -180,6 +181,7 @@ namespace client @@ -180,6 +181,7 @@ namespace client
protected:
void CleanupDestination ();
// I2CP
void HandleDataMessage (const uint8_t * buf, size_t len);
void CreateNewLeaseSet (std::vector<std::shared_ptr<i2p::tunnel::InboundTunnel> > tunnels);

Loading…
Cancel
Save