diff --git a/src/comm/comm_server.hpp b/src/comm/comm_server.hpp index 8036b796..76316689 100644 --- a/src/comm/comm_server.hpp +++ b/src/comm/comm_server.hpp @@ -16,7 +16,7 @@ namespace comm class comm_server { protected: - const uint64_t (&metric_thresholds)[4]; + const uint64_t (&metric_thresholds)[5]; const uint64_t max_msg_size; bool is_shutting_down = false; std::list sessions; @@ -210,7 +210,7 @@ namespace comm } public: - comm_server(std::string_view name, const uint16_t port, const uint64_t (&metric_thresholds)[4], const uint64_t max_msg_size) + comm_server(std::string_view name, const uint16_t port, const uint64_t (&metric_thresholds)[5], const uint64_t max_msg_size) : name(name), listen_port(port), metric_thresholds(metric_thresholds), diff --git a/src/comm/comm_session.cpp b/src/comm/comm_session.cpp index d5fbc8b7..711482a9 100644 --- a/src/comm/comm_session.cpp +++ b/src/comm/comm_session.cpp @@ -9,11 +9,10 @@ namespace comm { constexpr uint32_t INTERVALMS = 60000; - constexpr uint16_t INACTIVE_TIMEOUT = 120; // Time threshold for verified inactive connections in seconds. constexpr uint16_t UNVERIFIED_INACTIVE_TIMEOUT = 5; // Time threshold for unverified inactive connections in seconds. comm_session::comm_session( - std::string_view host_address, hpws::client &&hpws_client, const bool is_inbound, const uint64_t (&metric_thresholds)[4]) + std::string_view host_address, hpws::client &&hpws_client, const bool is_inbound, const uint64_t (&metric_thresholds)[5]) : uniqueid(host_address), host_address(host_address), hpws_client(std::move(hpws_client)), @@ -23,8 +22,8 @@ namespace comm // Create new session_thresholds and insert it to thresholds vector. // Have to maintain the SESSION_THRESHOLDS enum order in inserting new thresholds to thresholds vector // since enum's value is used as index in the vector to update vector values. - thresholds.reserve(4); - for (size_t i = 0; i < 4; i++) + thresholds.reserve(5); + for (size_t i = 0; i < 5; i++) thresholds.push_back(session_threshold(metric_thresholds[i], INTERVALMS)); } @@ -296,7 +295,11 @@ namespace comm */ void comm_session::check_last_activity_rules() { - const uint16_t timeout_seconds = (challenge_status == CHALLENGE_STATUS::CHALLENGE_VERIFIED ? INACTIVE_TIMEOUT : UNVERIFIED_INACTIVE_TIMEOUT); + const uint16_t timeout_seconds = (challenge_status == CHALLENGE_STATUS::CHALLENGE_VERIFIED ? thresholds[SESSION_THRESHOLDS::IDLE_CONNECTION_TIMEOUT].threshold_limit : UNVERIFIED_INACTIVE_TIMEOUT); + + // Timeout zero means unlimited. + if (timeout_seconds == 0) + return; if (util::get_epoch_milliseconds() - last_activity_timestamp >= (timeout_seconds * 1000)) { diff --git a/src/comm/comm_session.hpp b/src/comm/comm_session.hpp index 820ba69c..4bdb5664 100644 --- a/src/comm/comm_session.hpp +++ b/src/comm/comm_session.hpp @@ -56,7 +56,7 @@ namespace comm uint64_t last_activity_timestamp; // Keep track of the last activity timestamp in milliseconds. comm_session( - std::string_view host_address, hpws::client &&hpws_client, const bool is_inbound, const uint64_t (&metric_thresholds)[4]); + std::string_view host_address, hpws::client &&hpws_client, const bool is_inbound, const uint64_t (&metric_thresholds)[5]); int init(); int process_next_inbound_message(); int send(const std::vector &message); diff --git a/src/comm/comm_session_threshold.hpp b/src/comm/comm_session_threshold.hpp index 2a973e58..3c21049b 100644 --- a/src/comm/comm_session_threshold.hpp +++ b/src/comm/comm_session_threshold.hpp @@ -21,7 +21,10 @@ enum SESSION_THRESHOLDS MAX_BADSIGMSGS_PER_MINUTE = 2, // Max messages with bad structure per minute. - MAX_BADMSGS_PER_MINUTE = 3 + MAX_BADMSGS_PER_MINUTE = 3, + + // Idle connection timeout. + IDLE_CONNECTION_TIMEOUT = 4 }; /* diff --git a/src/conf.cpp b/src/conf.cpp index 5f8fa273..082f20f7 100644 --- a/src/conf.cpp +++ b/src/conf.cpp @@ -87,6 +87,8 @@ namespace conf cfg.roundtime = 1000; cfg.pubport = 8080; cfg.peerdiscoverytime = 30000; + cfg.pubidletimeout = 0; + cfg.peeridletimeout = 120; cfg.msgforwarding = false; cfg.dynamicpeerdiscovery = false; @@ -284,6 +286,9 @@ namespace conf cfg.roundtime = d["roundtime"].as(); cfg.peerdiscoverytime = d["peerdiscoverytime"].as(); + cfg.peeridletimeout = d["peeridletimeout"].as(); + cfg.pubidletimeout = d["pubidletimeout"].as(); + cfg.pubmaxsize = d["pubmaxsize"].as(); cfg.pubmaxcpm = d["pubmaxcpm"].as(); cfg.pubmaxbadmpm = d["pubmaxbadmpm"].as(); @@ -364,6 +369,9 @@ namespace conf d.insert_or_assign("roundtime", cfg.roundtime); d.insert_or_assign("peerdiscoverytime", cfg.peerdiscoverytime); + d.insert_or_assign("peeridletimeout", cfg.peeridletimeout); + d.insert_or_assign("pubidletimeout", cfg.pubidletimeout); + d.insert_or_assign("pubmaxsize", cfg.pubmaxsize); d.insert_or_assign("pubmaxcpm", cfg.pubmaxcpm); d.insert_or_assign("pubmaxbadmpm", cfg.pubmaxbadmpm); diff --git a/src/conf.hpp b/src/conf.hpp index 7216913f..6b80dbe0 100644 --- a/src/conf.hpp +++ b/src/conf.hpp @@ -99,6 +99,9 @@ namespace conf uint16_t pubport = 0; // Listening port for public user connections uint16_t peerdiscoverytime = 0; // Time interval in ms to find for peers dynamicpeerdiscovery should be on for this + uint16_t peeridletimeout = 0; // Idle connection timeout for peer connections in seconds. + uint16_t pubidletimeout = 0; // Idle connection timeout for user connections in seconds. + uint64_t pubmaxsize = 0; // User message max size in bytes uint64_t pubmaxcpm = 0; // User message rate (characters(bytes) per minute) uint64_t pubmaxbadmpm = 0; // User bad messages per minute diff --git a/src/p2p/p2p.cpp b/src/p2p/p2p.cpp index d0d83dfd..7a9310e7 100644 --- a/src/p2p/p2p.cpp +++ b/src/p2p/p2p.cpp @@ -14,7 +14,7 @@ namespace p2p // Holds global connected-peers and related objects. connected_context ctx; - uint64_t metric_thresholds[4]; + uint64_t metric_thresholds[5]; bool init_success = false; /** @@ -27,6 +27,7 @@ namespace p2p metric_thresholds[1] = conf::cfg.peermaxdupmpm; metric_thresholds[2] = conf::cfg.peermaxbadsigpm; metric_thresholds[3] = conf::cfg.peermaxbadmpm; + metric_thresholds[4] = conf::cfg.peeridletimeout; //Entry point for p2p which will start peer connections to other nodes if (start_peer_connections() == -1) diff --git a/src/p2p/peer_comm_server.cpp b/src/p2p/peer_comm_server.cpp index 96be43fe..3753bcdb 100644 --- a/src/p2p/peer_comm_server.cpp +++ b/src/p2p/peer_comm_server.cpp @@ -12,7 +12,7 @@ namespace p2p // Globally exposed weakly connected status variable. bool is_weakly_connected = false; - peer_comm_server::peer_comm_server(const uint16_t port, const uint64_t (&metric_thresholds)[4], + peer_comm_server::peer_comm_server(const uint16_t port, const uint64_t (&metric_thresholds)[5], const uint64_t max_msg_size, std::vector &req_known_remotes) : comm::comm_server("Peer", port, metric_thresholds, max_msg_size), req_known_remotes(req_known_remotes) diff --git a/src/p2p/peer_comm_server.hpp b/src/p2p/peer_comm_server.hpp index d9ed2142..f98145aa 100644 --- a/src/p2p/peer_comm_server.hpp +++ b/src/p2p/peer_comm_server.hpp @@ -31,7 +31,7 @@ namespace p2p std::atomic known_remote_count = 0; std::mutex req_known_remotes_mutex; std::vector &req_known_remotes; - peer_comm_server(const uint16_t port, const uint64_t (&metric_thresholds)[4], + peer_comm_server(const uint16_t port, const uint64_t (&metric_thresholds)[5], const uint64_t max_msg_size, std::vector &req_known_remotes); }; } // namespace p2p diff --git a/src/usr/usr.cpp b/src/usr/usr.cpp index 2c0856ec..02eab829 100644 --- a/src/usr/usr.cpp +++ b/src/usr/usr.cpp @@ -20,7 +20,7 @@ namespace usr // Holds global connected-users and related objects. connected_context ctx; - uint64_t metric_thresholds[4]; + uint64_t metric_thresholds[5]; bool init_success = false; /** @@ -33,6 +33,7 @@ namespace usr metric_thresholds[1] = 0; // This metric doesn't apply to user context. metric_thresholds[2] = 0; // This metric doesn't apply to user context. metric_thresholds[3] = conf::cfg.pubmaxbadmpm; + metric_thresholds[4] = conf::cfg.pubidletimeout; // Start listening for incoming user connections. if (start_listening() == -1)