1
1
Fork 0
mirror of https://github.com/oxen-io/lokinet synced 2023-12-14 06:53:00 +01:00

limit path builds across all builders

This commit is contained in:
Jeff Becker 2021-05-05 08:21:39 -04:00
parent 289ab9c4be
commit ec62228149
No known key found for this signature in database
GPG key ID: F357B3B42F6F9B05
5 changed files with 56 additions and 12 deletions

View file

@ -160,12 +160,26 @@ namespace llarp
namespace path
{
bool
BuildLimiter::Attempt(const RouterID& router)
{
return m_EdgeLimiter.Insert(router);
}
void
BuildLimiter::Decay(llarp_time_t now)
{
m_EdgeLimiter.Decay(now);
}
bool
BuildLimiter::Limited(const RouterID& router) const
{
return m_EdgeLimiter.Contains(router);
}
Builder::Builder(AbstractRouter* p_router, size_t pathNum, size_t hops)
: path::PathSet{pathNum}
, m_EdgeLimiter{MIN_PATH_BUILD_INTERVAL}
, _run{true}
, m_router{p_router}
, numHops{hops}
: path::PathSet{pathNum}, _run{true}, m_router{p_router}, numHops{hops}
{
CryptoManager::instance()->encryption_keygen(enckey);
}
@ -180,7 +194,6 @@ namespace llarp
void Builder::Tick(llarp_time_t)
{
const auto now = llarp::time_now_ms();
m_EdgeLimiter.Decay(now);
ExpirePaths(now, m_router);
if (ShouldBuildMore(now))
BuildOne();
@ -226,7 +239,7 @@ namespace llarp
if (exclude.count(rc.pubkey))
return;
if (m_EdgeLimiter.Contains(rc.pubkey))
if (BuildCooldownHit(rc.pubkey))
return;
found = rc;
@ -277,7 +290,7 @@ namespace llarp
bool
Builder::BuildCooldownHit(RouterID edge) const
{
return m_EdgeLimiter.Contains(edge);
return m_router->pathBuildLimiter().Limited(edge);
}
bool
@ -399,7 +412,7 @@ namespace llarp
return;
lastBuild = Now();
const RouterID edge{hops[0].pubkey};
if (not m_EdgeLimiter.Insert(edge))
if (not m_router->pathBuildLimiter().Attempt(edge))
{
LogWarn(Name(), " building too fast to edge router ", edge);
return;
@ -437,8 +450,6 @@ namespace llarp
{
PathSet::HandlePathBuildFailedAt(p, edge);
DoPathBuildBackoff();
/// add it to the edge limter even if it's not an edge for simplicity
m_EdgeLimiter.Insert(edge);
}
void

View file

@ -15,11 +15,31 @@ namespace llarp
static constexpr auto MIN_PATH_BUILD_INTERVAL = 500ms;
static constexpr auto PATH_BUILD_RATE = 100ms;
/// limiter for path builds
/// prevents overload and such
class BuildLimiter
{
util::DecayingHashSet<RouterID> m_EdgeLimiter;
public:
/// attempt a build
/// return true if we are allowed to continue
bool
Attempt(const RouterID& router);
/// decay limit entries
void
Decay(llarp_time_t now);
/// return true if this router is currently limited
bool
Limited(const RouterID& router) const;
};
struct Builder : public PathSet
{
private:
llarp_time_t m_LastWarn = 0s;
util::DecayingHashSet<RouterID> m_EdgeLimiter;
protected:
/// flag for PathSet::Stop()

View file

@ -292,6 +292,9 @@ namespace llarp
virtual bool
ConnectionToRouterAllowed(const RouterID& router) const = 0;
virtual path::BuildLimiter&
pathBuildLimiter() = 0;
/// return true if we have at least 1 session to this router in either
/// direction
virtual bool

View file

@ -753,6 +753,8 @@ namespace llarp
}
#endif
m_PathBuildLimiter.Decay(now);
routerProfiling().Tick();
if (ShouldReportStats(now))

View file

@ -74,6 +74,14 @@ namespace llarp
LMQ_ptr m_lmq;
path::BuildLimiter m_PathBuildLimiter;
path::BuildLimiter&
pathBuildLimiter() override
{
return m_PathBuildLimiter;
}
const LMQ_ptr&
lmq() const override
{