Skip to content

Commit

Permalink
Refactor kill_nonused_tcp()
Browse files Browse the repository at this point in the history
This makes the code a little more flat and readable. We also change the
logic so that we can kill the necessary number of unused relays immediately
rather than having to collect them and remove them in a separate loop
  • Loading branch information
JFreegman committed Dec 7, 2021
1 parent 1ce6aab commit 01ea270
Showing 1 changed file with 24 additions and 18 deletions.
42 changes: 24 additions & 18 deletions toxcore/TCP_connection.c
Original file line number Diff line number Diff line change
Expand Up @@ -1438,42 +1438,48 @@ static void do_tcp_conns(const Logger *logger, TCP_Connections *tcp_c, void *use

static void kill_nonused_tcp(TCP_Connections *tcp_c)
{
if (tcp_c->tcp_connections_length == 0) {
if (tcp_c->tcp_connections_length <= RECOMMENDED_FRIEND_TCP_CONNECTIONS) {
return;
}

uint32_t num_online = 0;
uint32_t num_kill = 0;
VLA(unsigned int, to_kill, tcp_c->tcp_connections_length);

for (uint32_t i = 0; i < tcp_c->tcp_connections_length; ++i) {
TCP_con *tcp_con = get_tcp_connection(tcp_c, i);

if (tcp_con) {
if (tcp_con->status == TCP_CONN_CONNECTED) {
if (!tcp_con->onion && !tcp_con->lock_count
&& mono_time_is_timeout(tcp_c->mono_time, tcp_con->connected_time, TCP_CONNECTION_ANNOUNCE_TIMEOUT)) {
to_kill[num_kill] = i;
++num_kill;
}
if (tcp_con == nullptr) {
continue;
}

++num_online;
}
if (tcp_con->status == TCP_CONN_CONNECTED) {
++num_online;
}
}

if (num_online <= RECOMMENDED_FRIEND_TCP_CONNECTIONS) {
return;
}

uint32_t n = num_online - RECOMMENDED_FRIEND_TCP_CONNECTIONS;
const uint32_t max_kill_count = num_online - RECOMMENDED_FRIEND_TCP_CONNECTIONS;
uint32_t kill_count = 0;

if (n < num_kill) {
num_kill = n;
}
for (uint32_t i = 0; i < tcp_c->tcp_connections_length && kill_count < max_kill_count; ++i) {
TCP_con *tcp_con = get_tcp_connection(tcp_c, i);

for (uint32_t i = 0; i < num_kill; ++i) {
kill_tcp_relay_connection(tcp_c, to_kill[i]);
if (tcp_con == nullptr) {
continue;
}

if (tcp_con->status == TCP_CONN_CONNECTED) {
if (tcp_con->onion || tcp_con->lock_count) { // connection is in use so we skip it
continue;
}

if (mono_time_is_timeout(tcp_c->mono_time, tcp_con->connected_time, TCP_CONNECTION_ANNOUNCE_TIMEOUT)) {
kill_tcp_relay_connection(tcp_c, i);
++kill_count;
}
}
}
}

Expand Down

0 comments on commit 01ea270

Please sign in to comment.