Skip to content

Commit

Permalink
check lease expiration with threshold
Browse files Browse the repository at this point in the history
  • Loading branch information
orignal committed Mar 26, 2015
1 parent ca3b9f2 commit 2a23537
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 12 deletions.
11 changes: 8 additions & 3 deletions LeaseSet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ namespace data
m_BufferLen += 32; // gateway id
htobe32buf (m_Buffer + m_BufferLen, it->GetNextTunnelID ());
m_BufferLen += 4; // tunnel id
uint64_t ts = it->GetCreationTime () + i2p::tunnel::TUNNEL_EXPIRATION_TIMEOUT - 60; // 1 minute before expiration
uint64_t ts = it->GetCreationTime () + i2p::tunnel::TUNNEL_EXPIRATION_TIMEOUT - i2p::tunnel::TUNNEL_EXPIRATION_THRESHOLD; // 1 minute before expiration
ts *= 1000; // in milliseconds
ts += rnd.GenerateWord32 (0, 5); // + random milliseconds
htobe64buf (m_Buffer + m_BufferLen, ts);
Expand Down Expand Up @@ -107,13 +107,18 @@ namespace data
LogPrint ("LeaseSet verification failed");
}

const std::vector<Lease> LeaseSet::GetNonExpiredLeases () const
const std::vector<Lease> LeaseSet::GetNonExpiredLeases (bool withThreshold) const
{
auto ts = i2p::util::GetMillisecondsSinceEpoch ();
std::vector<Lease> leases;
for (auto& it: m_Leases)
if (ts < it.endDate)
{
auto endDate = it.endDate;
if (!withThreshold)
endDate -= i2p::tunnel::TUNNEL_EXPIRATION_THRESHOLD*1000;
if (ts < endDate)
leases.push_back (it);
}
return leases;
}

Expand Down
2 changes: 1 addition & 1 deletion LeaseSet.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ namespace data
// implements RoutingDestination
const IdentHash& GetIdentHash () const { return m_Identity.GetIdentHash (); };
const std::vector<Lease>& GetLeases () const { return m_Leases; };
const std::vector<Lease> GetNonExpiredLeases () const;
const std::vector<Lease> GetNonExpiredLeases (bool withThreshold = true) const;
bool HasExpiredLeases () const;
bool HasNonExpiredLeases () const;
const uint8_t * GetEncryptionPublicKey () const { return m_EncryptionKey; };
Expand Down
18 changes: 10 additions & 8 deletions Streaming.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -585,7 +585,7 @@ namespace stream
}

auto ts = i2p::util::GetMillisecondsSinceEpoch ();
if (ts >= m_CurrentRemoteLease.endDate)
if (ts >= m_CurrentRemoteLease.endDate - i2p::tunnel::TUNNEL_EXPIRATION_THRESHOLD*1000)
UpdateCurrentRemoteLease ();
if (ts < m_CurrentRemoteLease.endDate)
{
Expand All @@ -604,11 +604,7 @@ namespace stream
m_CurrentOutboundTunnel->SendTunnelDataMsg (msgs);
}
else
{
LogPrint (eLogInfo, "All leases are expired. Trying to request");
m_RemoteLeaseSet = nullptr;
m_LocalDestination.GetOwner ().RequestDestination (m_RemoteIdentity.GetIdentHash ());
}
LogPrint (eLogWarning, "All leases are expired");
}


Expand Down Expand Up @@ -703,7 +699,12 @@ namespace stream
{
if (!m_RoutingSession)
m_RoutingSession = m_LocalDestination.GetOwner ().GetRoutingSession (m_RemoteLeaseSet, 32);
auto leases = m_RemoteLeaseSet->GetNonExpiredLeases ();
auto leases = m_RemoteLeaseSet->GetNonExpiredLeases (false); // try without threshold first
if (leases.empty ())
{
m_LocalDestination.GetOwner ().RequestDestination (m_RemoteIdentity.GetIdentHash ()); // time to re-request
leases = m_RemoteLeaseSet->GetNonExpiredLeases (true); // then with threshold
}
if (!leases.empty ())
{
uint32_t i = i2p::context.GetRandomNumberGenerator ().GenerateWord32 (0, leases.size () - 1);
Expand All @@ -714,8 +715,9 @@ namespace stream
}
else
{
m_RemoteLeaseSet = m_LocalDestination.GetOwner ().FindLeaseSet (m_RemoteIdentity.GetIdentHash ()); // re-request expired
m_RemoteLeaseSet = nullptr;
m_CurrentRemoteLease.endDate = 0;
// re-request expired
}
}
else
Expand Down

0 comments on commit 2a23537

Please sign in to comment.