From 486e1b8351429e5e4af67dfa2a95a8bc8e5cdc91 Mon Sep 17 00:00:00 2001 From: tiptorrent development team Date: Sat, 2 Oct 2021 11:46:22 +0200 Subject: add retry logic - if the connection to server fails, retry 5 times, sleeping 5 seconds before each retry. - if the redirection fails, go back and request the chunk from the server again. --- src/main.c | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/main.c b/src/main.c index fef2447..ed3b032 100644 --- a/src/main.c +++ b/src/main.c @@ -54,6 +54,7 @@ struct tip_client { uint64_t data_len; uint64_t content_len; int state; + int num_retries; int fd; bool error; bool redirected; @@ -444,6 +445,9 @@ static uint32_t select_file_chunk(bool *file_chunk) static char _filename[PATH_MAX + 1]; +#define MAX_RETRIES 5 +#define WAIT_RETRY 5 /* wait 5 seconds before retrying. */ + int main(int argc, char *argv[]) { struct timeval tv_start, tv_stop, tv; @@ -471,7 +475,7 @@ int main(int argc, char *argv[]) k = select_file_chunk(file_chunk); snprintf(_filename, sizeof(_filename), "%s.%u", argv[2], k); filename = _filename; - +retry: syslog(LOG_INFO, "Requesting file %s to server\n", filename); tip_client_connect(argv[1]); @@ -482,7 +486,14 @@ int main(int argc, char *argv[]) if (_cli.error) { syslog(LOG_ERR, "Failed to fetch file %s\n", filename); - break; + sleep(WAIT_RETRY); + if (_cli.num_retries++ >= MAX_RETRIES) { + syslog(LOG_ERR, "Maximum number of retries (%d), bailing out!\n", + MAX_RETRIES); + break; + } + _cli.error = false; + goto retry; } tip_client_progress(&_cli, true); -- cgit v1.2.3-18-g5258