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.
master
tiptorrent development team 2021-10-02 11:46:22 +02:00
parent 48da60d883
commit 486e1b8351
1 changed files with 13 additions and 2 deletions

View File

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