fix up exit logic

pull/87/head^2
Jeff Becker 6 years ago
parent 0147672416
commit b004b9e2a1
No known key found for this signature in database
GPG Key ID: F357B3B42F6F9B05

@ -19,7 +19,7 @@ extern "C"
} }
namespace llarp namespace llarp
{ {
/// aligned buffer, aligns to the nears Long_t /// aligned buffer that is sz bytes long and aligns to the nears Long_t
template < size_t sz, bool randomize = false, typename Long_t = uint64_t > template < size_t sz, bool randomize = false, typename Long_t = uint64_t >
struct AlignedBuffer struct AlignedBuffer
{ {

@ -35,6 +35,10 @@ namespace llarp
bool bool
ExpiresSoon(llarp_time_t now, llarp_time_t dlt = 5000) const; ExpiresSoon(llarp_time_t now, llarp_time_t dlt = 5000) const;
/// return true if this endpoint looks dead right now
bool
LooksDead(llarp_time_t now, llarp_time_t timeout = 10000) const;
/// tick ourself, reset tx/rx rates /// tick ourself, reset tx/rx rates
void void
Tick(llarp_time_t now); Tick(llarp_time_t now);
@ -92,6 +96,7 @@ namespace llarp
llarp::PathID_t m_CurrentPath; llarp::PathID_t m_CurrentPath;
llarp::huint32_t m_IP; llarp::huint32_t m_IP;
uint64_t m_TxRate, m_RxRate; uint64_t m_TxRate, m_RxRate;
llarp_time_t m_LastActive;
bool m_RewriteSource; bool m_RewriteSource;
}; };
} // namespace exit } // namespace exit

@ -44,6 +44,9 @@ namespace llarp
llarp_router* llarp_router*
Router(); Router();
llarp_time_t
Now() const;
llarp_crypto* llarp_crypto*
Crypto(); Crypto();

@ -14,6 +14,7 @@ namespace llarp
, m_IP(ip) , m_IP(ip)
, m_RewriteSource(rewriteIP) , m_RewriteSource(rewriteIP)
{ {
m_LastActive = parent->Now();
} }
Endpoint::~Endpoint() Endpoint::~Endpoint()
@ -65,6 +66,15 @@ namespace llarp
return true; return true;
} }
bool Endpoint::LooksDead(llarp_time_t now, llarp_time_t timeout) const
{
if(ExpiresSoon(now, timeout))
return true;
if (now > m_LastActive)
return now - m_LastActive > timeout;
return true;
}
bool bool
Endpoint::SendOutboundTraffic(llarp_buffer_t buf) Endpoint::SendOutboundTraffic(llarp_buffer_t buf)
{ {
@ -83,6 +93,7 @@ namespace llarp
return false; return false;
} }
m_TxRate += buf.sz; m_TxRate += buf.sz;
m_LastActive = m_Parent->Now();
return true; return true;
} }

@ -37,10 +37,16 @@ namespace llarp
{ {
} }
llarp_time_t
ExitEndpoint::Now() const
{
return m_Router->Now();
}
void void
ExitEndpoint::FlushInbound() ExitEndpoint::FlushInbound()
{ {
auto now = Router()->Now(); auto now = Now();
m_InetToNetwork.Process([&](Pkt_t &pkt) { m_InetToNetwork.Process([&](Pkt_t &pkt) {
llarp::PubKey pk; llarp::PubKey pk;
{ {
@ -58,23 +64,27 @@ namespace llarp
auto range = m_ActiveExits.equal_range(pk); auto range = m_ActiveExits.equal_range(pk);
auto itr = range.first; auto itr = range.first;
uint64_t min = std::numeric_limits< uint64_t >::max(); uint64_t min = std::numeric_limits< uint64_t >::max();
/// pick path with lowest rx rate /// pick non dead looking path with lowest tx rate
while(itr != range.second) while(itr != range.second)
{ {
if(ep == nullptr) if(itr->second->TxRate() < min && !itr->second->LooksDead(now))
ep = itr->second.get();
else if(itr->second->RxRate() < min && !itr->second->ExpiresSoon(now))
{ {
min = ep->RxRate();
ep = itr->second.get(); ep = itr->second.get();
min = ep->TxRate();
} }
++itr; ++itr;
} }
if(ep)
if(ep == nullptr)
{
// we may have all dead sessions, wtf now?
llarp::LogWarn(Name(), " dropped inbound traffic for session ", pk, " as we have no working endpoints");
}
else
{ {
if(!ep->SendInboundTraffic(pkt.Buffer())) if(!ep->SendInboundTraffic(pkt.Buffer()))
{ {
llarp::LogWarn(Name(), " dropped inbound traffic for session ", pk); llarp::LogWarn(Name(), " dropped inbound traffic for session ", pk, " as we are overloaded (probably)");
} }
} }
}); });

Loading…
Cancel
Save