Merge pull request #768 from majestrate/sane-limits

Sane limits
pull/776/head
Jeff 5 years ago committed by GitHub
commit ecf3c37d2e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -2,6 +2,7 @@ set(LIB_UTIL_SRC
config/config.cpp
config/ini.cpp
constants/defaults.cpp
constants/limits.cpp
constants/link_layer.cpp
constants/path.cpp
constants/proto.cpp

@ -2,6 +2,7 @@
#include <config/ini.hpp>
#include <constants/defaults.hpp>
#include <constants/limits.hpp>
#include <net/net.hpp>
#include <util/fs.hpp>
#include <util/logger.hpp>
@ -501,7 +502,7 @@ llarp_ensure_config(const char *fname, const char *basedir, bool overwrite,
return false;
}
auto &f = optional_f.value();
llarp_generic_ensure_config(f, basepath);
llarp_generic_ensure_config(f, basepath, asRouter);
if(asRouter)
{
llarp_ensure_router_config(f, basepath);
@ -515,7 +516,8 @@ llarp_ensure_config(const char *fname, const char *basedir, bool overwrite,
}
void
llarp_generic_ensure_config(std::ofstream &f, std::string basepath)
llarp_generic_ensure_config(std::ofstream &f, std::string basepath,
bool isRouter)
{
f << "# this configuration was auto generated with 'sane' defaults"
<< std::endl;
@ -536,6 +538,13 @@ llarp_generic_ensure_config(std::ofstream &f, std::string basepath)
f << "# uncomment following line to set router nickname to 'lokinet'"
<< std::endl;
f << "#nickname=lokinet" << std::endl;
const auto limits = isRouter ? llarp::limits::snode : llarp::limits::client;
f << "# maintain min connections to other routers" << std::endl;
f << "min-routers=" << std::to_string(limits.DefaultMinRouters) << std::endl;
f << "# hard limit of routers globally we are connected to at any given time"
<< std::endl;
f << "max-routers=" << std::to_string(limits.DefaultMaxRouters) << std::endl;
f << std::endl << std::endl;
// logging
@ -693,7 +702,7 @@ llarp_ensure_client_config(std::ofstream &f, std::string basepath)
// now do up fname
f << std::endl << std::endl;
f << "# snapps configuration section" << std::endl;
f << "[services]";
f << "[services]" << std::endl;
f << "# uncomment next line to enable a snapp" << std::endl;
f << "#example-snapp=" << snappExample_fpath << std::endl;
f << std::endl << std::endl;

@ -95,7 +95,7 @@ namespace llarp
size_t m_minConnectedRouters = 2;
/// hard upperbound limit on the number of router to router connections
size_t m_maxConnectedRouters = 2000;
size_t m_maxConnectedRouters = 5;
std::string m_netId;
std::string m_nickname;
@ -315,7 +315,8 @@ namespace llarp
} // namespace llarp
void
llarp_generic_ensure_config(std::ofstream& f, std::string basepath);
llarp_generic_ensure_config(std::ofstream& f, std::string basepath,
bool isRouter);
void
llarp_ensure_router_config(std::ofstream& f, std::string basepath);

@ -0,0 +1,13 @@
#include <constants/limits.hpp>
namespace llarp
{
namespace limits
{
/// snode limit parameters
const LimitParameters snode = {6, 60};
/// client limit parameters
const LimitParameters client = {4, 6};
} // namespace limits
} // namespace llarp

@ -0,0 +1,26 @@
#ifndef LLARP_CONSTANTS_LIMITS_HPP
#define LLARP_CONSTANTS_LIMITS_HPP
#include <cstddef>
namespace llarp
{
namespace limits
{
/// Limits are a struct that contains all hard and soft limit
/// parameters for a given mode of operation
struct LimitParameters
{
/// minimum routers needed to run
std::size_t DefaultMinRouters;
/// hard limit on router sessions (by pubkey)
std::size_t DefaultMaxRouters;
};
/// snode limit parameters
const extern LimitParameters snode;
/// client limit parameters
const extern LimitParameters client;
} // namespace limits
} // namespace llarp
#endif

@ -199,7 +199,7 @@ namespace libuv
auto* self = static_cast< conn_glue* >(h->data);
h->data = nullptr;
delete self;
llarp::LogInfo("deleted");
llarp::LogDebug("deleted");
}
void
@ -217,7 +217,7 @@ namespace libuv
m_Conn.closed(&m_Conn);
}
m_Conn.impl = nullptr;
llarp::LogInfo("closed");
llarp::LogDebug("closed");
uv_close((uv_handle_t*)&m_Ticker, &FullClose);
}

@ -52,6 +52,9 @@ namespace llarp
virtual util::StatusObject
ExtractStatus() const = 0;
virtual bool
ShouldConnectTo(const RouterID &router) const = 0;
};
} // namespace llarp

@ -216,9 +216,26 @@ namespace llarp
itr->second = session;
}
if(ShouldConnectTo(router))
{
auto fn = std::bind(&OutboundSessionMaker::DoEstablish, this, router);
_logic->queue_func(fn);
}
}
auto fn = std::bind(&OutboundSessionMaker::DoEstablish, this, router);
_logic->queue_func(fn);
bool
OutboundSessionMaker::ShouldConnectTo(const RouterID &router) const
{
size_t numPending = 0;
{
util::Lock lock(&_mutex);
if(pendingSessions.find(router) == pendingSessions.end())
numPending += pendingSessions.size();
}
if(_linkManager->HasSessionTo(router))
return false;
return _linkManager->NumberOfConnectedRouters() + numPending
< maxConnectedRouters;
}
void

@ -52,11 +52,20 @@ namespace llarp
util::StatusObject
ExtractStatus() const override;
bool
ShouldConnectTo(const RouterID &router) const override
LOCKS_EXCLUDED(_mutex);
void
Init(ILinkManager *linkManager, I_RCLookupHandler *rcLookup,
std::shared_ptr< Logic > logic, llarp_nodedb *nodedb,
std::shared_ptr< llarp::thread::ThreadPool > threadpool);
/// always maintain this many connections to other routers
size_t minConnectedRouters = 4;
/// hard upperbound limit on the number of router to router connections
size_t maxConnectedRouters = 6;
private:
void
DoEstablish(const RouterID &router) LOCKS_EXCLUDED(_mutex);

@ -2,6 +2,7 @@
#include <config/config.hpp>
#include <constants/proto.hpp>
#include <constants/limits.hpp>
#include <crypto/crypto.hpp>
#include <crypto/crypto_libsodium.hpp>
#include <dht/context.hpp>
@ -374,14 +375,16 @@ namespace llarp
m_OutboundPort = conf->iwp_links.outboundPort();
// Router config
_rc.SetNick(conf->router.nickname());
maxConnectedRouters = conf->router.maxConnectedRouters();
minConnectedRouters = conf->router.minConnectedRouters();
encryption_keyfile = conf->router.encryptionKeyfile();
our_rc_file = conf->router.ourRcFile();
transport_keyfile = conf->router.transportKeyfile();
addrInfo = conf->router.addrInfo();
publicOverride = conf->router.publicOverride();
ip4addr = conf->router.ip4addr();
_outboundSessionMaker.maxConnectedRouters =
conf->router.maxConnectedRouters();
_outboundSessionMaker.minConnectedRouters =
conf->router.minConnectedRouters();
encryption_keyfile = conf->router.encryptionKeyfile();
our_rc_file = conf->router.ourRcFile();
transport_keyfile = conf->router.transportKeyfile();
addrInfo = conf->router.addrInfo();
publicOverride = conf->router.publicOverride();
ip4addr = conf->router.ip4addr();
// Lokid Config
usingSNSeed = conf->lokid.usingSNSeed;
@ -656,16 +659,16 @@ namespace llarp
const size_t connected = NumberOfConnectedRouters();
const size_t N = nodedb()->num_loaded();
if(N < minRequiredRouters)
if(N < llarp::path::default_len)
{
LogInfo("We need at least ", minRequiredRouters,
LogInfo("We need at least ", llarp::path::default_len,
" service nodes to build paths but we have ", N, " in nodedb");
_rcLookupHandler.ExploreNetwork();
}
if(connected < minConnectedRouters)
if(connected < _outboundSessionMaker.minConnectedRouters)
{
size_t dlt = minConnectedRouters - connected;
size_t dlt = _outboundSessionMaker.minConnectedRouters - connected;
LogInfo("connecting to ", dlt, " random routers to keep alive");
_outboundSessionMaker.ConnectToRandomRouters(dlt, now);
}
@ -887,6 +890,14 @@ namespace llarp
EnsureNetConfigDefaultsSane(netConfig);
const auto limits =
IsServiceNode() ? llarp::limits::snode : llarp::limits::client;
_outboundSessionMaker.minConnectedRouters = std::max(
_outboundSessionMaker.minConnectedRouters, limits.DefaultMinRouters);
_outboundSessionMaker.maxConnectedRouters = std::max(
_outboundSessionMaker.maxConnectedRouters, limits.DefaultMaxRouters);
if(IsServiceNode())
{
// initialize as service node
@ -897,14 +908,12 @@ namespace llarp
}
RouterID us = pubkey();
LogInfo("initalized service node: ", us);
if(minConnectedRouters < 6)
minConnectedRouters = 6;
// relays do not use profiling
routerProfiling().Disable();
}
else
{
maxConnectedRouters = minConnectedRouters + 1;
// we are a client
// regenerate keys and resign rc before everything else
CryptoManager::instance()->identity_keygen(_identity);

@ -194,13 +194,6 @@ namespace llarp
Sign(Signature &sig, const llarp_buffer_t &buf) const override;
uint16_t m_OutboundPort = 0;
/// always maintain this many connections to other routers
size_t minConnectedRouters = 2;
/// hard upperbound limit on the number of router to router connections
size_t maxConnectedRouters = 2000;
size_t minRequiredRouters = 4;
/// how often do we resign our RC? milliseconds.
// TODO: make configurable
llarp_time_t rcRegenInterval = 60 * 60 * 1000;

@ -149,7 +149,7 @@ namespace llarp
{
PruneInboundMessages(now);
// ensure that this section is called every 1s or so
if(now - m_LastTick >= 1000)
if(now > m_LastTick && now - m_LastTick >= 1000)
{
m_TXRate = 0;
m_RXRate = 0;
@ -157,6 +157,11 @@ namespace llarp
RouterID(remoteRC.pubkey).ToString());
m_LastTick = now;
}
else
{
// try sending 1 segment
PumpWrite(1);
}
}
/// low level read

Loading…
Cancel
Save