From b77525b72da23ea6be03586401a5a32b7bddcaca Mon Sep 17 00:00:00 2001 From: Jeff Becker Date: Wed, 22 Aug 2018 12:19:51 -0400 Subject: [PATCH] persist on commit --- include/llarp/path.hpp | 3 +++ llarp/pathbuilder.cpp | 3 +++ llarp/relay_commit.cpp | 4 ++++ llarp/router.cpp | 48 +++++++++++++++++++++++--------------- llarp/service/endpoint.cpp | 4 ++-- llarp/transit_hop.cpp | 6 +++++ 6 files changed, 47 insertions(+), 21 deletions(-) diff --git a/include/llarp/path.hpp b/include/llarp/path.hpp index a1c520eed..03946c386 100644 --- a/include/llarp/path.hpp +++ b/include/llarp/path.hpp @@ -131,6 +131,9 @@ namespace llarp llarp_time_t lifetime = DEFAULT_PATH_LIFETIME; llarp_proto_version_t version; + llarp_time_t + ExpireTime() const; + llarp::routing::InboundMessageParser m_MessageParser; friend std::ostream& diff --git a/llarp/pathbuilder.cpp b/llarp/pathbuilder.cpp index 0676ad6cc..efe05a6a2 100644 --- a/llarp/pathbuilder.cpp +++ b/llarp/pathbuilder.cpp @@ -204,7 +204,9 @@ llarp_pathbuilder_context::SelectHop(llarp_nodedb* db, llarp_rc* prev, llarp_rc* cur, size_t hop) { if(hop == 0) + { return router->GetRandomConnectedRouter(cur); + } else llarp_nodedb_select_random_hop(db, prev, cur, hop); return true; @@ -231,6 +233,7 @@ llarp_pathbuilder_context::BuildOne() void llarp_pathbuilder_context::ManualRebuild(size_t num) { + llarp::LogDebug("manual rebuild ", num); while(num--) BuildOne(); } diff --git a/llarp/relay_commit.cpp b/llarp/relay_commit.cpp index 5bfdf76e5..130520e20 100644 --- a/llarp/relay_commit.cpp +++ b/llarp/relay_commit.cpp @@ -191,6 +191,10 @@ namespace llarp SendLRCM(void* user) { LRCMFrameDecrypt* self = static_cast< LRCMFrameDecrypt* >(user); + self->context->Router()->PersistSessionUntil(self->hop->info.downstream, + self->hop->ExpireTime()); + self->context->Router()->PersistSessionUntil(self->hop->info.upstream, + self->hop->ExpireTime()); self->context->ForwardLRCM(self->hop->info.upstream, self->frames); delete self; } diff --git a/llarp/router.cpp b/llarp/router.cpp index 82d7cc53e..05e6408f7 100644 --- a/llarp/router.cpp +++ b/llarp/router.cpp @@ -59,7 +59,14 @@ void llarp_router::PersistSessionUntil(const llarp::RouterID &remote, llarp_time_t until) { - m_PersistingSessions[remote] = until; + llarp::LogDebug("persist session to ", remote, " until ", until); + if(m_PersistingSessions.find(remote) == m_PersistingSessions.end()) + m_PersistingSessions[remote] = until; + else + { + if(m_PersistingSessions[remote] < until) + m_PersistingSessions[remote] = until; + } } bool @@ -412,34 +419,37 @@ llarp_router::Tick() // llarp::LogDebug("tick router"); auto now = llarp_time_now_ms(); paths.ExpirePaths(); - if(inboundLinks.size() == 0) { + auto itr = m_PersistingSessions.begin(); + while(itr != m_PersistingSessions.end()) { - auto itr = m_PersistingSessions.begin(); - while(itr != m_PersistingSessions.end()) + auto link = GetLinkWithSessionByPubkey(itr->first); + if(now < itr->second) { - auto link = GetLinkWithSessionByPubkey(itr->first); - if(now <= itr->second) + // persisting ended + if(link) + link->CloseSessionTo(itr->first); + itr = m_PersistingSessions.erase(itr); + } + else + { + if(link) { - // persisting ended - if(link) - link->CloseSessionTo(itr->first); - itr = m_PersistingSessions.erase(itr); + llarp::LogDebug("keepalive to ", itr->first); + link->KeepAliveSessionTo(itr->first); } else { - if(link) - { - link->KeepAliveSessionTo(itr->first); - } - else - { - TryEstablishTo(itr->first); - } - ++itr; + llarp::LogDebug("establish to ", itr->first); + TryEstablishTo(itr->first); } + ++itr; } } + } + + if(inboundLinks.size() == 0) + { auto N = llarp_nodedb_num_loaded(nodedb); if(N > 3) { diff --git a/llarp/service/endpoint.cpp b/llarp/service/endpoint.cpp index f4fc0f482..68cbd6f3b 100644 --- a/llarp/service/endpoint.cpp +++ b/llarp/service/endpoint.cpp @@ -98,8 +98,8 @@ namespace llarp { llarp::LogWarn("could not publish descriptors for endpoint ", Name(), " because we couldn't get any introductions"); - if(ShouldBuildMore()) - ManualRebuild(1); + // if(ShouldBuildMore()) + ManualRebuild(1); return; } m_IntroSet.I.clear(); diff --git a/llarp/transit_hop.cpp b/llarp/transit_hop.cpp index 5556bc384..048706664 100644 --- a/llarp/transit_hop.cpp +++ b/llarp/transit_hop.cpp @@ -17,6 +17,12 @@ namespace llarp return now - started > lifetime; } + llarp_time_t + TransitHop::ExpireTime() const + { + return started + lifetime; + } + TransitHopInfo::TransitHopInfo(const TransitHopInfo& other) : txID(other.txID) , rxID(other.rxID)