llarp::service::Endpoint::ReadyToDoLookup()

previously we had a checking style function that passes in an optional
defaulting to nullopt as a micro optimzation, this makes the code
unnessarily obtuse.

simplify this by splitting up into 2 functions,
one for getting the unique endpoints and one for checking if the
number of them is above the minimum.

add overload for ReadyToDoLookup() that checks against constant but
can do more in the future if desired to reduce the burden on future contributors.
pull/2048/head
Jeff Becker 2 years ago
parent 9dfb4a389c
commit e5efe793ca
No known key found for this signature in database
GPG Key ID: 025C02EE3A092F2D

@ -40,16 +40,10 @@
#include <uvw.hpp> #include <uvw.hpp>
#include <variant> #include <variant>
namespace
{
constexpr size_t MIN_ENDPOINTS_FOR_LNS_LOOKUP = 2;
} // namespace
namespace llarp namespace llarp
{ {
namespace service namespace service
{ {
static auto logcat = log::Cat("endpoint"); static auto logcat = log::Cat("endpoint");
Endpoint::Endpoint(AbstractRouter* r, Context* parent) Endpoint::Endpoint(AbstractRouter* r, Context* parent)
@ -317,7 +311,7 @@ namespace llarp
auto obj = path::Builder::ExtractStatus(); auto obj = path::Builder::ExtractStatus();
obj["exitMap"] = m_ExitMap.ExtractStatus(); obj["exitMap"] = m_ExitMap.ExtractStatus();
obj["identity"] = m_Identity.pub.Addr().ToString(); obj["identity"] = m_Identity.pub.Addr().ToString();
obj["networkReady"] = ReadyToDoLookup(); obj["networkReady"] = ReadyForNetwork();
util::StatusObject authCodes; util::StatusObject authCodes;
for (const auto& [service, info] : m_RemoteAuthInfos) for (const auto& [service, info] : m_RemoteAuthInfos)
@ -958,20 +952,28 @@ namespace llarp
return not m_ExitMap.Empty(); return not m_ExitMap.Empty();
} }
path::Path::UniqueEndpointSet_t
Endpoint::GetUniqueEndpointsForLookup() const
{
path::Path::UniqueEndpointSet_t paths;
ForEachPath([&paths](auto path) {
if (path and path->IsReady())
paths.insert(path);
});
return paths;
}
bool bool
Endpoint::ReadyToDoLookup(std::optional<uint64_t> numPaths) const Endpoint::ReadyForNetwork() const
{ {
if (not numPaths) return IsReady() and ReadyToDoLookup(GetUniqueEndpointsForLookup().size());
{ }
path::Path::UniqueEndpointSet_t paths;
ForEachPath([&paths](auto path) {
if (path and path->IsReady())
paths.insert(path);
});
numPaths = paths.size();
}
return numPaths >= MIN_ENDPOINTS_FOR_LNS_LOOKUP; bool
Endpoint::ReadyToDoLookup(size_t num_paths) const
{
// Currently just checks the number of paths, but could do more checks in the future. (jason)
return num_paths >= MIN_ENDPOINTS_FOR_LNS_LOOKUP;
} }
void void
@ -992,12 +994,7 @@ namespace llarp
return; return;
} }
LogInfo(Name(), " looking up LNS name: ", name); LogInfo(Name(), " looking up LNS name: ", name);
path::Path::UniqueEndpointSet_t paths; auto paths = GetUniqueEndpointsForLookup();
ForEachPath([&paths](auto path) {
if (path and path->IsReady())
paths.insert(path);
});
// not enough paths // not enough paths
if (not ReadyToDoLookup(paths.size())) if (not ReadyToDoLookup(paths.size()))
{ {

@ -48,13 +48,16 @@ namespace llarp
struct OutboundContext; struct OutboundContext;
/// minimum interval for publishing introsets /// minimum interval for publishing introsets
static constexpr auto IntrosetPublishInterval = path::intro_path_spread / 2; inline constexpr auto IntrosetPublishInterval = path::intro_path_spread / 2;
/// how agressively should we retry publishing introset on failure /// how agressively should we retry publishing introset on failure
static constexpr auto IntrosetPublishRetryCooldown = 1s; inline constexpr auto IntrosetPublishRetryCooldown = 1s;
/// how aggressively should we retry looking up introsets /// how aggressively should we retry looking up introsets
static constexpr auto IntrosetLookupCooldown = 250ms; inline constexpr auto IntrosetLookupCooldown = 250ms;
/// number of unique snodes we want to talk to do to ons lookups
inline constexpr size_t MIN_ENDPOINTS_FOR_LNS_LOOKUP = 2;
struct Endpoint : public path::Builder, struct Endpoint : public path::Builder,
public ILookupHolder, public ILookupHolder,
@ -64,7 +67,9 @@ namespace llarp
Endpoint(AbstractRouter* r, Context* parent); Endpoint(AbstractRouter* r, Context* parent);
~Endpoint() override; ~Endpoint() override;
/// return true if we are ready to recv packets from the void /// return true if we are ready to recv packets from the void.
/// really should be ReadyForInboundTraffic() but the diff is HUGE and we need to rewrite this
/// component anyways.
bool bool
IsReady() const; IsReady() const;
@ -521,10 +526,16 @@ namespace llarp
return false; return false;
} }
/// return true if we are ready to do outbound and inbound traffic
bool bool
ReadyToDoLookup(std::optional<uint64_t> numPaths = std::nullopt) const; ReadyForNetwork() const;
protected: protected:
bool
ReadyToDoLookup(size_t num_paths) const;
path::Path::UniqueEndpointSet_t
GetUniqueEndpointsForLookup() const;
IDataHandler* m_DataHandler = nullptr; IDataHandler* m_DataHandler = nullptr;
Identity m_Identity; Identity m_Identity;
net::IPRangeMap<service::Address> m_ExitMap; net::IPRangeMap<service::Address> m_ExitMap;

Loading…
Cancel
Save