perf: improve reconnect operation

Random delay of 5 to 14 seconds to prevent a large number of
devices connecting to the server simultaneously.

Signed-off-by: Jianhui Zhao <zhaojh329@gmail.com>
This commit is contained in:
Jianhui Zhao
2025-06-17 10:48:35 +08:00
parent 7b2fddbef0
commit ea7bcf930a
2 changed files with 11 additions and 2 deletions

View File

@@ -26,6 +26,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <time.h>
#include <glob.h>
#include "log/log.h"
@@ -53,7 +54,10 @@ static void load_default_ca_cert(struct ssl_context *ctx)
static void signal_cb(struct ev_loop *loop, ev_signal *w, int revents)
{
struct rtty *rtty = ev_userdata(loop);
if (w->signum == SIGINT) {
rtty->reconnect = false;
ev_break(loop, EVBREAK_ALL);
log_info("Normal quit\n");
}
@@ -138,6 +142,8 @@ int main(int argc, char **argv)
return -1;
#endif
ev_set_userdata(loop, &rtty);
while (true) {
c = getopt_long(argc, argv, shortopts, long_options, &option_index);
if (c == -1)
@@ -272,6 +278,8 @@ int main(int argc, char **argv)
load_default_ca_cert(rtty.ssl_ctx);
#endif
srand(time(NULL));
if (rtty_start(&rtty) < 0)
return -1;

View File

@@ -324,8 +324,10 @@ done:
if (!rtty->reconnect) {
ev_break(rtty->loop, EVBREAK_ALL);
} else {
ev_timer_set(&rtty->tmr, 5.0, 0);
int delay = rand() % 10 + 5;
ev_timer_set(&rtty->tmr, delay, 0);
ev_timer_start(rtty->loop, &rtty->tmr);
log_err("reconnect in %d seconds\n", delay);
}
}
@@ -737,7 +739,6 @@ static void rtty_timer_cb(struct ev_loop *loop, struct ev_timer *w, int revents)
struct rtty *rtty = container_of(w, struct rtty, tmr);
if (rtty->sock < 0) {
log_err("rtty reconnecting...\n");
tcp_connect(rtty->loop, rtty->host, rtty->port, on_net_connected, rtty);
return;
}