Merge pull request #145 from majestrate/dev

expire old snode sessions
This commit is contained in:
Jeff 2018-12-13 07:59:07 -05:00 committed by GitHub
commit 984d281d0b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 35 additions and 4 deletions

View File

@ -13,6 +13,7 @@ namespace llarp
, m_ExitRouter(router)
, m_WritePacket(writepkt)
, m_Counter(0)
, m_LastUse(0)
{
r->crypto.identity_keygen(m_ExitIdentity);
}
@ -76,10 +77,10 @@ namespace llarp
bool
BaseSession::HandleGotExit(llarp::path::Path* p, llarp_time_t b)
{
m_LastUse = router->Now();
if(b == 0)
{
llarp::LogInfo("obtained an exit via ", p->Endpoint());
}
return true;
}
@ -88,7 +89,13 @@ namespace llarp
{
(void)p;
if(m_WritePacket)
return m_WritePacket(pkt);
{
if(!m_WritePacket(pkt))
return false;
m_LastUse = router->Now();
return true;
}
return false;
}
@ -133,9 +140,16 @@ namespace llarp
return AvailablePaths(llarp::path::ePathRoleExit) > 0;
}
bool
BaseSession::IsExpired(llarp_time_t now) const
{
return m_LastUse && now > m_LastUse && now - m_LastUse > LifeSpan;
}
bool
BaseSession::FlushUpstreamTraffic()
{
auto now = router->Now();
auto path = PickRandomEstablishedPath(llarp::path::ePathRoleExit);
if(!path)
{
@ -151,7 +165,8 @@ namespace llarp
{
auto& msg = queue.front();
msg.S = path->NextSeqNo();
path->SendRoutingMessage(&msg, router);
if(path->SendRoutingMessage(&msg, router))
m_LastUse = now;
queue.pop_front();
}
}

View File

@ -16,6 +16,7 @@ namespace llarp
struct BaseSession : public llarp::path::Builder
{
static constexpr size_t MaxUpstreamQueueLength = 256;
static constexpr llarp_time_t LifeSpan = 60 * 10 * 1000;
BaseSession(const llarp::RouterID& exitRouter,
std::function< bool(llarp_buffer_t) > writepkt,
@ -48,6 +49,9 @@ namespace llarp
return m_ExitRouter;
}
bool
IsExpired(llarp_time_t now) const;
protected:
llarp::RouterID m_ExitRouter;
std::function< bool(llarp_buffer_t) > m_WritePacket;
@ -72,6 +76,7 @@ namespace llarp
TieredQueue_t m_Upstream;
uint64_t m_Counter;
llarp::SecretKey m_ExitIdentity;
llarp_time_t m_LastUse;
};
struct ExitSession final : public BaseSession

View File

@ -142,6 +142,17 @@ namespace llarp
{
RegenAndPublishIntroSet(now);
}
// expire snode sessions
{
auto itr = m_SNodeSessions.begin();
while(itr != m_SNodeSessions.end())
{
if(itr->second->IsExpired(now))
itr = m_SNodeSessions.erase(itr);
else
++itr;
}
}
// expire pending tx
{
std::set< service::IntroSet > empty;