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;
PathSet* m_PathSet;
llarp::service::Introduction intro;
llarp_time_t buildStarted;
PathStatus _status;
Path(const std::vector< RouterContact >& routers);
Path(const std::vector< RouterContact >& routers, PathSet* parent);
void
SetBuildResultHook(BuildResultHookFunc func);

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

@ -48,6 +48,9 @@ namespace llarp
virtual void
HandlePathBuilt(Path* path);
virtual void
HandlePathBuildTimeout(Path* path);
void
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());
size_t hsz = h.size();
@ -386,7 +387,7 @@ namespace llarp
{
if(st == ePathTimeout)
{
llarp::LogInfo("path ", Name(), " has timed out");
m_PathSet->HandlePathBuildTimeout(this);
}
else if(st == ePathBuilding)
{

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

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

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

Loading…
Cancel
Save