Skip to content

Commit 7ee7720

Browse files
committed
Make some improvements to how often/when we announce a group
Instead of announcing a group whenever our connection status changes, including when we gain or lose a TCP relay connection, we now only when our UDP status changes, or if our previously announced relay has gone offline. We also refresh our announcement 20 minutes regardless of any connection changes. change should vastly reduce the amount of unnecessary DHT traffic related to group announcements.
1 parent b8aa21c commit 7ee7720

File tree

7 files changed

+35
-18
lines changed

7 files changed

+35
-18
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
cbd8bea9d23a961f27aacd35c4509fc1c22d72356f61d1ec74cc469b6b14490d /usr/local/bin/tox-bootstrapd
1+
5cf49ad258527f6a2734a9d1f863084f66189338f47ca9d0a49482b4217577fb /usr/local/bin/tox-bootstrapd

toxcore/Messenger.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2573,6 +2573,13 @@ static bool self_announce_group(const Messenger *m, GC_Chat *chat, Onion_Friend
25732573

25742574
onion_friend_set_gc_data(onion_friend, gc_data, (uint16_t)length);
25752575
chat->update_self_announces = false;
2576+
chat->last_time_self_announce = mono_time_get(chat->mono_time);
2577+
2578+
if (tcp_num > 0) {
2579+
pk_copy(chat->announced_tcp_relay_pk, announce.base_announce.tcp_relays[0].public_key);
2580+
} else {
2581+
memset(chat->announced_tcp_relay_pk, 0, sizeof(chat->announced_tcp_relay_pk));
2582+
}
25762583

25772584
LOGGER_DEBUG(chat->log, "Published group announce. TCP relays: %d, UDP status: %d", tcp_num,
25782585
chat->self_udp_status);

toxcore/TCP_connection.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,6 @@ static TCP_con *get_tcp_connection(const TCP_Connections *tcp_c, int tcp_connect
277277
return &tcp_c->tcp_connections[tcp_connections_number];
278278
}
279279

280-
/** Returns the number of connected TCP relays */
281280
uint32_t tcp_connected_relays_count(const TCP_Connections *tcp_c)
282281
{
283282
uint32_t count = 0;
@@ -635,6 +634,11 @@ static int find_tcp_connection_relay(const TCP_Connections *tcp_c, const uint8_t
635634
return -1;
636635
}
637636

637+
bool tcp_relay_is_valid(const TCP_Connections *tcp_c, const uint8_t *relay_pk)
638+
{
639+
return find_tcp_connection_relay(tcp_c, relay_pk) != -1;
640+
}
641+
638642
/** @brief Create a new TCP connection to public_key.
639643
*
640644
* public_key must be the counterpart to the secret key that the other peer used with `new_tcp_connections()`.

toxcore/TCP_connection.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,14 @@ const uint8_t *tcp_connections_public_key(const TCP_Connections *tcp_c);
7878
non_null()
7979
uint32_t tcp_connections_count(const TCP_Connections *tcp_c);
8080

81-
/** Returns the number of connected TCP relays */
81+
/** @brief Returns the number of connected TCP relays. */
8282
non_null()
8383
uint32_t tcp_connected_relays_count(const TCP_Connections *tcp_c);
8484

85+
/** @brief Returns true if we know of a valid TCP relay with the passed public key. */
86+
non_null()
87+
bool tcp_relay_is_valid(const TCP_Connections *tcp_c, const uint8_t *relay_pk);
88+
8589
/** @brief Send a packet to the TCP connection.
8690
*
8791
* return -1 on failure.

toxcore/group_chats.c

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2016,7 +2016,7 @@ static bool send_gc_tcp_relays(const GC_Chat *chat, GC_Connection *gconn)
20162016

20172017
for (uint32_t i = 0; i < n; ++i) {
20182018
add_tcp_relay_connection(chat->tcp_conn, gconn->tcp_connection_num, &tcp_relays[i].ip_port,
2019-
tcp_relays[i].public_key);
2019+
tcp_relays[i].public_key);
20202020
}
20212021

20222022
const int nodes_len = pack_nodes(chat->log, data, sizeof(data), tcp_relays, n);
@@ -7050,9 +7050,10 @@ static void do_gc_tcp(const GC_Session *c, GC_Chat *chat, void *userdata)
70507050
set_tcp_connection_to_status(chat->tcp_conn, gconn->tcp_connection_num, tcp_set);
70517051
}
70527052

7053-
if (mono_time_is_timeout(chat->mono_time, chat->last_checked_tcp_relays, TCP_RELAYS_CHECK_INTERVAL) &&
7054-
tcp_connected_relays_count(chat->tcp_conn) != chat->tcp_connections) {
7053+
if (mono_time_is_timeout(chat->mono_time, chat->last_checked_tcp_relays, TCP_RELAYS_CHECK_INTERVAL)
7054+
&& tcp_connected_relays_count(chat->tcp_conn) != chat->connected_tcp_relays) {
70557055
add_tcp_relays_to_chat(c, chat);
7056+
chat->connected_tcp_relays = tcp_connected_relays_count(chat->tcp_conn);
70567057
chat->last_checked_tcp_relays = mono_time_get(chat->mono_time);
70577058
}
70587059
}
@@ -7061,27 +7062,27 @@ static void do_gc_tcp(const GC_Session *c, GC_Chat *chat, void *userdata)
70617062
* Updates our TCP and UDP connection status and flags a new announcement if our connection has
70627063
* changed and we have either a UDP or TCP connection.
70637064
*/
7064-
#define GC_SELF_CONNECTION_CHECK_INTERVAL 2
7065+
#define GC_SELF_CONNECTION_CHECK_INTERVAL 5 // how often in seconds we should run this function
7066+
#define GC_SELF_REFRESH_ANNOUNCE_INTERVAL (60 * 20) // how often in seconds we force refresh our group announcement
70657067
non_null()
70667068
static void do_self_connection(const GC_Session *c, GC_Chat *chat)
70677069
{
70687070
if (!mono_time_is_timeout(chat->mono_time, chat->last_self_announce_check, GC_SELF_CONNECTION_CHECK_INTERVAL)) {
70697071
return;
70707072
}
70717073

7072-
const uint32_t tcp_connections = tcp_connected_relays_count(chat->tcp_conn);
7073-
7074-
const Messenger *m = c->messenger;
7075-
const unsigned int self_udp_status = ipport_self_copy(m->dht, &chat->self_ip_port);
7076-
7074+
const unsigned int self_udp_status = ipport_self_copy(c->messenger->dht, &chat->self_ip_port);
70777075
const bool udp_change = (chat->self_udp_status != self_udp_status) && (self_udp_status != SELF_UDP_STATUS_NONE);
7078-
const bool tcp_change = tcp_connections != chat->tcp_connections;
70797076

7080-
if (is_public_chat(chat) && (udp_change || tcp_change)) {
7077+
// We flag a group announce if our UDP status has changed since last run, or if our last announced TCP
7078+
// relay is no longer valid. Additionally, we will always flag an announce in the specified interval
7079+
// regardless of the prior conditions. Private groups are never announced.
7080+
if (is_public_chat(chat) &&
7081+
((udp_change || !tcp_relay_is_valid(chat->tcp_conn, chat->announced_tcp_relay_pk))
7082+
|| mono_time_is_timeout(chat->mono_time, chat->last_time_self_announce, GC_SELF_REFRESH_ANNOUNCE_INTERVAL))) {
70817083
chat->update_self_announces = true;
70827084
}
70837085

7084-
chat->tcp_connections = tcp_connections;
70857086
chat->self_udp_status = (Self_UDP_Status) self_udp_status;
70867087
chat->last_self_announce_check = mono_time_get(chat->mono_time);
70877088
}

toxcore/group_chats.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -242,8 +242,7 @@ void gc_get_topic(const GC_Chat *chat, uint8_t *topic);
242242

243243
/** @brief Returns the topic length.
244244
*
245-
* The return value is equal to the `length` agument received by the last topic
246-
* callback.
245+
* The return value is equal to the `length` agument received by the last topic callback.
247246
*/
248247
non_null()
249248
uint16_t gc_get_topic_size(const GC_Chat *chat);

toxcore/group_common.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,13 +249,13 @@ typedef struct GC_Chat {
249249
const Logger *log;
250250
const Random *rng;
251251

252+
uint32_t connected_tcp_relays;
252253
Self_UDP_Status self_udp_status;
253254
IP_Port self_ip_port;
254255

255256
Networking_Core *net;
256257
TCP_Connections *tcp_conn;
257258

258-
uint32_t tcp_connections; // the number of global TCP relays we're connected to
259259
uint64_t last_checked_tcp_relays;
260260
Group_Handshake_Join_Type join_type;
261261

@@ -312,6 +312,8 @@ typedef struct GC_Chat {
312312

313313
bool update_self_announces; // true if we should try to update our announcements
314314
uint64_t last_self_announce_check; // the last time we checked if we should update our announcements
315+
uint64_t last_time_self_announce; // the last time we announced the group
316+
uint8_t announced_tcp_relay_pk[CRYPTO_PUBLIC_KEY_SIZE]; // The pk of the last TCP relay we announced
315317

316318
uint8_t m_group_public_key[CRYPTO_PUBLIC_KEY_SIZE]; // public key for group's messenger friend connection
317319
int friend_connection_id; // identifier for group's messenger friend connection

0 commit comments

Comments
 (0)