linear backoff for path building to lessen strain on the network

pull/18/head
Jeff Becker 6 years ago
parent f85cfcdab0
commit b40dd30c7a
No known key found for this signature in database
GPG Key ID: F357B3B42F6F9B05

@ -233,12 +233,14 @@ namespace llarp
HopList hops; HopList hops;
PathSet* m_PathSet;
llarp::service::Introduction intro; llarp::service::Introduction intro;
llarp_time_t buildStarted; llarp_time_t buildStarted;
PathStatus _status; PathStatus _status;
Path(const std::vector< RouterContact >& routers); Path(const std::vector< RouterContact >& routers, PathSet* parent);
void void
SetBuildResultHook(BuildResultHookFunc func); SetBuildResultHook(BuildResultHookFunc func);

@ -7,12 +7,17 @@ namespace llarp
{ {
namespace path namespace path
{ {
// milliseconds waiting between builds on a path
constexpr llarp_time_t MIN_PATH_BUILD_INTERVAL = 5 * 1000;
struct Builder : public PathSet struct Builder : public PathSet
{ {
struct llarp_router* router; struct llarp_router* router;
struct llarp_dht_context* dht; struct llarp_dht_context* dht;
llarp::SecretKey enckey; llarp::SecretKey enckey;
size_t numHops; size_t numHops;
llarp_time_t lastBuild = 0;
llarp_time_t buildIntervalLimit = MIN_PATH_BUILD_INTERVAL;
/// construct /// construct
Builder(llarp_router* p_router, struct llarp_dht_context* p_dht, Builder(llarp_router* p_router, struct llarp_dht_context* p_dht,
size_t numPaths, size_t numHops); size_t numPaths, size_t numHops);
@ -34,6 +39,12 @@ namespace llarp
virtual const byte_t* virtual const byte_t*
GetTunnelEncryptionSecretKey() const; GetTunnelEncryptionSecretKey() const;
virtual void
HandlePathBuilt(Path* p);
virtual void
HandlePathBuildTimeout(Path* p);
}; };
} // namespace path } // namespace path

@ -48,6 +48,9 @@ namespace llarp
virtual void virtual void
HandlePathBuilt(Path* path); HandlePathBuilt(Path* path);
virtual void
HandlePathBuildTimeout(Path* path);
void void
AddPath(Path* path); AddPath(Path* path);

@ -323,7 +323,8 @@ namespace llarp
{ {
} }
Path::Path(const std::vector< RouterContact >& h) Path::Path(const std::vector< RouterContact >& h, PathSet* parent)
: m_PathSet(parent)
{ {
hops.resize(h.size()); hops.resize(h.size());
size_t hsz = h.size(); size_t hsz = h.size();
@ -386,7 +387,7 @@ namespace llarp
{ {
if(st == ePathTimeout) if(st == ePathTimeout)
{ {
llarp::LogInfo("path ", Name(), " has timed out"); m_PathSet->HandlePathBuildTimeout(this);
} }
else if(st == ePathBuilding) else if(st == ePathBuilding)
{ {

@ -190,8 +190,9 @@ namespace llarp
bool bool
Builder::ShouldBuildMore() const Builder::ShouldBuildMore() const
{ {
return llarp::path::PathSet::ShouldBuildMore() auto now = llarp_time_now_ms();
|| router->NumberOfConnectedRouters() == 0; return llarp::path::PathSet::ShouldBuildMore() && now > lastBuild
&& now - lastBuild > buildIntervalLimit;
} }
void void
@ -222,17 +223,33 @@ namespace llarp
} }
++idx; ++idx;
} }
lastBuild = llarp_time_now_ms();
// async generate keys // async generate keys
AsyncPathKeyExchangeContext< Builder >* ctx = AsyncPathKeyExchangeContext< Builder >* ctx =
new AsyncPathKeyExchangeContext< Builder >(&router->crypto); new AsyncPathKeyExchangeContext< Builder >(&router->crypto);
ctx->pathset = this; ctx->pathset = this;
auto path = new llarp::path::Path(hops); auto path = new llarp::path::Path(hops, this);
path->SetBuildResultHook(std::bind(&llarp::path::PathSet::HandlePathBuilt, path->SetBuildResultHook(std::bind(&llarp::path::Builder::HandlePathBuilt,
ctx->pathset, std::placeholders::_1)); this, std::placeholders::_1));
ctx->AsyncGenerateKeys(path, router->logic, router->tp, this, ctx->AsyncGenerateKeys(path, router->logic, router->tp, this,
&pathbuilder_generated_keys); &pathbuilder_generated_keys);
} }
void
Builder::HandlePathBuilt(Path* p)
{
buildIntervalLimit = MIN_PATH_BUILD_INTERVAL;
PathSet::HandlePathBuilt(p);
}
void
Builder::HandlePathBuildTimeout(Path* p)
{
// linear backoff
buildIntervalLimit += 1000;
PathSet::HandlePathBuildTimeout(p);
}
void void
Builder::ManualRebuild(size_t num) Builder::ManualRebuild(size_t num)
{ {

@ -184,6 +184,12 @@ namespace llarp
return count > 0; return count > 0;
} }
void
PathSet::HandlePathBuildTimeout(Path* p)
{
llarp::LogInfo("path ", p->Name(), " has timed out");
}
Path* Path*
PathSet::PickRandomEstablishedPath() const PathSet::PickRandomEstablishedPath() const
{ {

@ -727,6 +727,7 @@ namespace llarp
std::placeholders::_1, std::placeholders::_1,
std::placeholders::_2)); std::placeholders::_2));
RegenAndPublishIntroSet(llarp_time_now_ms()); RegenAndPublishIntroSet(llarp_time_now_ms());
path::Builder::HandlePathBuilt(p);
} }
bool bool
@ -799,6 +800,7 @@ namespace llarp
p->SetDeadChecker(std::bind(&Endpoint::CheckPathIsDead, m_Endpoint, p->SetDeadChecker(std::bind(&Endpoint::CheckPathIsDead, m_Endpoint,
std::placeholders::_1, std::placeholders::_1,
std::placeholders::_2)); std::placeholders::_2));
path::Builder::HandlePathBuilt(p);
} }
void void
@ -1211,6 +1213,8 @@ namespace llarp
Endpoint::SendContext::Send(ProtocolFrame& msg) Endpoint::SendContext::Send(ProtocolFrame& msg)
{ {
auto path = m_PathSet->GetPathByRouter(remoteIntro.router); auto path = m_PathSet->GetPathByRouter(remoteIntro.router);
if(path == nullptr)
path = m_Endpoint->GetPathByRouter(remoteIntro.router);
if(path) if(path)
{ {
auto now = llarp_time_now_ms(); auto now = llarp_time_now_ms();

Loading…
Cancel
Save