llarp/path/pathbuilder.hpp

Namespaces

Name
llarp
[crypto.hpp]
llarp::path

Classes

Name
class llarp::path::BuildLimiter
limiter for path builds prevents overload and such
struct llarp::path::Builder

Source code

#pragma once

#include "pathset.hpp"
#include <llarp/util/decaying_hashset.hpp>

#include <atomic>
#include <set>

namespace llarp
{
  namespace path
  {
    // milliseconds waiting between builds on a path per router
    static constexpr auto MIN_PATH_BUILD_INTERVAL = 500ms;
    static constexpr auto PATH_BUILD_RATE = 100ms;

    class BuildLimiter
    {
      util::DecayingHashSet<RouterID> m_EdgeLimiter;

     public:
      bool
      Attempt(const RouterID& router);

      void
      Decay(llarp_time_t now);

      bool
      Limited(const RouterID& router) const;
    };

    struct Builder : public PathSet
    {
     private:
      llarp_time_t m_LastWarn = 0s;

     protected:
      std::atomic<bool> _run;

      virtual bool
      UrgentBuild(llarp_time_t now) const;

      bool
      BuildCooldownHit(RouterID edge) const;

     private:
      void
      DoPathBuildBackoff();

     public:
      AbstractRouter* const m_router;
      SecretKey enckey;
      size_t numHops;
      llarp_time_t lastBuild = 0s;
      llarp_time_t buildIntervalLimit = MIN_PATH_BUILD_INTERVAL;

      Builder(AbstractRouter* p_router, size_t numDesiredPaths, size_t numHops);

      virtual ~Builder() = default;

      bool
      ShouldBuildMore(llarp_time_t now) const override;

      virtual bool
      ShouldBundleRC() const = 0;

      void
      ResetInternalState() override;

      bool
      BuildCooldownHit(llarp_time_t now) const;

      virtual PathRole
      GetRoles() const
      {
        return ePathRoleAny;
      }

      BuildStats
      CurrentBuildStats() const
      {
        return m_BuildStats;
      }

      bool
      Stop() override;

      bool
      IsStopped() const override;

      bool
      ShouldRemove() const override;

      llarp_time_t
      Now() const override;

      virtual void
      Tick(llarp_time_t now) override;

      void
      BuildOne(PathRole roles = ePathRoleAny) override;

      bool
      BuildOneAlignedTo(const RouterID endpoint) override;

      std::optional<std::vector<RouterContact>>
      GetHopsAlignedToForBuild(RouterID endpoint, const std::set<RouterID>& exclude = {});

      void
      Build(std::vector<RouterContact> hops, PathRole roles = ePathRoleAny) override;

      std::optional<RouterContact>
      SelectFirstHop(const std::set<RouterID>& exclude = {}) const;

      virtual std::optional<std::vector<RouterContact>>
      GetHopsForBuild() override;

      void
      ManualRebuild(size_t N, PathRole roles = ePathRoleAny);

      virtual const SecretKey&
      GetTunnelEncryptionSecretKey() const;

      virtual void
      HandlePathBuilt(Path_ptr p) override;

      virtual void
      HandlePathBuildTimeout(Path_ptr p) override;

      virtual void
      HandlePathBuildFailedAt(Path_ptr p, RouterID hop) override;
    };

    using Builder_ptr = std::shared_ptr<Builder>;

  }  // namespace path

}  // namespace llarp

Updated on 2026-01-10 at 22:49:45 +0000