diff --git a/LeaseSet.cpp b/LeaseSet.cpp index 0d6d3fb8..b9c2b132 100644 --- a/LeaseSet.cpp +++ b/LeaseSet.cpp @@ -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); @@ -107,13 +107,18 @@ namespace data LogPrint ("LeaseSet verification failed"); } - const std::vector LeaseSet::GetNonExpiredLeases () const + const std::vector LeaseSet::GetNonExpiredLeases (bool withThreshold) const { auto ts = i2p::util::GetMillisecondsSinceEpoch (); std::vector 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; } diff --git a/LeaseSet.h b/LeaseSet.h index 10fa71e9..eac5fc28 100644 --- a/LeaseSet.h +++ b/LeaseSet.h @@ -49,7 +49,7 @@ namespace data // implements RoutingDestination const IdentHash& GetIdentHash () const { return m_Identity.GetIdentHash (); }; const std::vector& GetLeases () const { return m_Leases; }; - const std::vector GetNonExpiredLeases () const; + const std::vector GetNonExpiredLeases (bool withThreshold = true) const; bool HasExpiredLeases () const; bool HasNonExpiredLeases () const; const uint8_t * GetEncryptionPublicKey () const { return m_EncryptionKey; }; diff --git a/Streaming.cpp b/Streaming.cpp index 8b73e849..5f110fd5 100644 --- a/Streaming.cpp +++ b/Streaming.cpp @@ -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) { @@ -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"); } @@ -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); @@ -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