You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
lokinet/llarp/path/pathbuilder.hpp

122 lines
3.3 KiB
C++

#pragma once
#include "pathset.hpp"
#include <llarp/util/decaying_hashset.hpp>
#include <llarp/util/status.hpp>
#include <atomic>
#include <set>
namespace llarp::path
{
5 months ago
// maximum number of paths a path-set can maintain
inline constexpr size_t MAX_PATHS{32};
5 months ago
/// limiter for path builds
/// prevents overload and such
class BuildLimiter
{
util::DecayingHashSet<RouterID> _edge_limiter;
5 months ago
public:
/// attempt a build
/// return true if we are allowed to continue
bool Attempt(const RouterID& router);
5 months ago
/// decay limit entries
void Decay(llarp_time_t now);
5 months ago
/// return true if this router is currently limited
bool Limited(const RouterID& router) const;
};
5 months ago
struct PathBuilder : public PathSet
{
private:
llarp_time_t last_warn_time = 0s;
// size_t _num_paths_desired;
5 months ago
protected:
/// flag for PathSet::Stop()
std::atomic<bool> _run;
5 months ago
BuildStats _build_stats;
5 months ago
virtual bool UrgentBuild(llarp_time_t now) const;
5 months ago
/// return true if we hit our soft limit for building paths too fast on a first hop
bool BuildCooldownHit(RouterID edge) const;
5 months ago
private:
void DoPathBuildBackoff();
5 months ago
void setup_hop_keys(path::PathHopConfig& hop, const RouterID& nextHop);
5 months ago
std::string create_hop_info_frame(const path::PathHopConfig& hop);
5 months ago
public:
Router* const router;
size_t num_hops;
llarp_time_t _last_build = 0s;
llarp_time_t build_interval_limit = MIN_PATH_BUILD_INTERVAL;
5 months ago
/// construct
PathBuilder(Router* p_router, size_t numDesiredPaths, size_t numHops);
5 months ago
virtual ~PathBuilder() = default;
5 months ago
util::StatusObject ExtractStatus() const;
5 months ago
bool ShouldBuildMore(llarp_time_t now) const override;
5 months ago
void ResetInternalState() override;
5 months ago
/// return true if we hit our soft limit for building paths too fast
bool BuildCooldownHit(llarp_time_t now) const;
5 years ago
5 months ago
/// get roles for this path builder
virtual PathRole GetRoles() const
{
return ePathRoleAny;
}
4 years ago
5 months ago
BuildStats CurrentBuildStats() const
{
return build_stats;
}
5 months ago
bool Stop() override;
5 years ago
5 months ago
bool IsStopped() const override;
5 months ago
bool ShouldRemove() const override;
5 months ago
llarp_time_t Now() const override;
5 months ago
void Tick(llarp_time_t now) override;
5 months ago
void BuildOne(PathRole roles = ePathRoleAny) override;
5 months ago
bool BuildOneAlignedTo(const RouterID endpoint) override;
5 months ago
std::optional<std::vector<RemoteRC>> GetHopsAlignedToForBuild(
RouterID endpoint, const std::set<RouterID>& exclude = {});
5 months ago
void Build(std::vector<RemoteRC> hops, PathRole roles = ePathRoleAny) override;
5 months ago
/// pick a first hop
std::optional<RemoteRC> SelectFirstHop(const std::set<RouterID>& exclude = {}) const;
5 months ago
std::optional<std::vector<RemoteRC>> GetHopsForBuild() override;
5 months ago
void ManualRebuild(size_t N, PathRole roles = ePathRoleAny);
5 months ago
void HandlePathBuilt(std::shared_ptr<Path> p) override;
5 months ago
void HandlePathBuildTimeout(std::shared_ptr<Path> p) override;
5 months ago
void HandlePathBuildFailedAt(std::shared_ptr<Path> p, RouterID hop) override;
};
} // namespace llarp::path