From 5b520a4dff548667e59fa181a9f2c82b8643633f Mon Sep 17 00:00:00 2001 From: Stephen Shelton Date: Thu, 26 Mar 2020 18:49:05 -0600 Subject: [PATCH] Simplify connection limit constants --- llarp/CMakeLists.txt | 2 +- llarp/config/config.cpp | 36 ++++++++++++++++++++++-------------- llarp/constants/limits.cpp | 13 ------------- llarp/constants/limits.hpp | 26 -------------------------- llarp/router/router.cpp | 8 -------- 5 files changed, 23 insertions(+), 62 deletions(-) delete mode 100644 llarp/constants/limits.cpp delete mode 100644 llarp/constants/limits.hpp diff --git a/llarp/CMakeLists.txt b/llarp/CMakeLists.txt index 003464c14..635ba3908 100644 --- a/llarp/CMakeLists.txt +++ b/llarp/CMakeLists.txt @@ -5,7 +5,7 @@ set(LIB_UTIL_SRC config/definition.cpp config/ini.cpp config/key_manager.cpp - constants/limits.cpp + ${CMAKE_CURRENT_BINARY_DIR}/constants/version.cpp util/bencode.cpp util/buffer.cpp util/encode.cpp diff --git a/llarp/config/config.cpp b/llarp/config/config.cpp index e5c843fe8..6e3406dcc 100644 --- a/llarp/config/config.cpp +++ b/llarp/config/config.cpp @@ -2,7 +2,6 @@ #include #include -#include #include #include #include @@ -21,6 +20,14 @@ namespace llarp { + + // constants for config file default values + constexpr int DefaultMinConnectionsForRouter = 6; + constexpr int DefaultMaxConnectionsForRouter = 60; + + constexpr int DefaultMinConnectionsForClient = 4; + constexpr int DefaultMaxConnectionsForClient = 6; + const char* lokinetEnv(string_view suffix) { @@ -110,18 +117,24 @@ namespace llarp m_netId = std::move(arg); }); - conf.defineOption("router", "min-connections", false, m_minConnectedRouters, - [this](int arg) { - if (arg < 1) - throw std::invalid_argument("min-connections must be >= 1"); + // TODO: refactor to pass isRelay in + bool isRelay = false; + int minConnections = (isRelay ? DefaultMinConnectionsForRouter + : DefaultMinConnectionsForClient); + conf.defineOption("router", "min-connections", false, minConnections, + [=](int arg) { + if (arg < minConnections) + throw std::invalid_argument(stringify("min-connections must be >= ", minConnections)); m_minConnectedRouters = arg; }); - conf.defineOption("router", "max-connections", false, m_maxConnectedRouters, - [this](int arg) { - if (arg < 1) - throw std::invalid_argument("max-connections must be >= 1"); + int maxConnections = (isRelay ? DefaultMaxConnectionsForRouter + : DefaultMaxConnectionsForClient); + conf.defineOption("router", "max-connections", false, maxConnections, + [=](int arg) { + if (arg < maxConnections) + throw std::invalid_argument(stringify("max-connections must be >= ", maxConnections)); m_maxConnectedRouters = arg; }); @@ -582,7 +595,6 @@ namespace llarp // TODO: pass these in const std::string basepath = ""; - bool isRouter = false; // router def.addSectionComment("router", "Configuration for routing activity."); @@ -609,15 +621,11 @@ namespace llarp // TODO: why did Kee want this, and/or what does it really do? Something about logs? def.addOptionComment("router", "nickname", "Router nickname. Kee wanted it."); - const auto limits = isRouter ? llarp::limits::snode : llarp::limits::client; - def.addOptionComment("router", "min-connections", "Minimum number of routers lokinet will attempt to maintain connections to."); - def.addConfigValue("router", "min-connections", stringify(limits.DefaultMinRouters)); def.addOptionComment("router", "max-connections", "Maximum number (hard limit) of routers lokinet will be connected to at any time."); - def.addConfigValue("router", "max-connections", stringify(limits.DefaultMaxRouters)); // logging def.addSectionComment("logging", "logging settings"); diff --git a/llarp/constants/limits.cpp b/llarp/constants/limits.cpp deleted file mode 100644 index e7358b876..000000000 --- a/llarp/constants/limits.cpp +++ /dev/null @@ -1,13 +0,0 @@ -#include - -namespace llarp -{ - namespace limits - { - /// snode limit parameters - const LimitParameters snode = {6, 60}; - - /// client limit parameters - const LimitParameters client = {4, 6}; - } // namespace limits -} // namespace llarp \ No newline at end of file diff --git a/llarp/constants/limits.hpp b/llarp/constants/limits.hpp deleted file mode 100644 index 626170139..000000000 --- a/llarp/constants/limits.hpp +++ /dev/null @@ -1,26 +0,0 @@ -#ifndef LLARP_CONSTANTS_LIMITS_HPP -#define LLARP_CONSTANTS_LIMITS_HPP -#include -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 \ No newline at end of file diff --git a/llarp/router/router.cpp b/llarp/router/router.cpp index d6671a529..a26375b31 100644 --- a/llarp/router/router.cpp +++ b/llarp/router/router.cpp @@ -2,7 +2,6 @@ #include #include -#include #include #include #include @@ -1046,13 +1045,6 @@ 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