Idle connection timeout config for both peer and user connections. (#158)

This commit is contained in:
Savinda Senevirathne
2020-11-20 19:48:45 +05:30
committed by GitHub
parent fe24ef2388
commit bf710c6bde
10 changed files with 32 additions and 13 deletions

View File

@@ -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<T> 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),

View File

@@ -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))
{

View File

@@ -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<uint8_t> &message);

View File

@@ -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
};
/*

View File

@@ -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<uint16_t>();
cfg.peerdiscoverytime = d["peerdiscoverytime"].as<uint16_t>();
cfg.peeridletimeout = d["peeridletimeout"].as<uint16_t>();
cfg.pubidletimeout = d["pubidletimeout"].as<uint16_t>();
cfg.pubmaxsize = d["pubmaxsize"].as<uint64_t>();
cfg.pubmaxcpm = d["pubmaxcpm"].as<uint64_t>();
cfg.pubmaxbadmpm = d["pubmaxbadmpm"].as<uint64_t>();
@@ -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);

View File

@@ -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

View File

@@ -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)

View File

@@ -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<conf::peer_properties> &req_known_remotes)
: comm::comm_server<peer_comm_session>("Peer", port, metric_thresholds, max_msg_size),
req_known_remotes(req_known_remotes)

View File

@@ -31,7 +31,7 @@ namespace p2p
std::atomic<uint16_t> known_remote_count = 0;
std::mutex req_known_remotes_mutex;
std::vector<conf::peer_properties> &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<conf::peer_properties> &req_known_remotes);
};
} // namespace p2p

View File

@@ -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)