From f9b502b5fdfeb7e11e3eacc158b41748623efd56 Mon Sep 17 00:00:00 2001 From: Mole Shang <135e2@135e2.dev> Date: Mon, 7 Aug 2023 21:50:10 +0800 Subject: process_url: add retries while downloading --- src/constants.h | 1 + src/process_url.c | 13 ++++++++++--- 2 files changed, 11 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/constants.h b/src/constants.h index 2d49ffd..0158e7e 100644 --- a/src/constants.h +++ b/src/constants.h @@ -6,6 +6,7 @@ #define MAX_THREAD 6 #define MAX_THREAD_SIZE 10485760 +#define MAX_RETRY 3 #define CEIL_DIV(a, b) (((a) + (b)-1) / (b)) #define MAX(x, y) ((x) > (y)) ? (x) : (y) diff --git a/src/process_url.c b/src/process_url.c index b6fa730..71fdacd 100644 --- a/src/process_url.c +++ b/src/process_url.c @@ -1,3 +1,4 @@ +#include "constants.h" #include #include #include @@ -46,7 +47,6 @@ static CURLU *h; static bool logerr_b(CURLcode r) { if (r && !corrupted) { LOG("libcurl", "Error %d: %s\n", r, ERRTOSTRING(r)); - corrupted = true; } return r; } @@ -84,7 +84,6 @@ static bool logerr_h(CURLHcode r) { break; } LOG("libcurl", "Header Error %d: %s\n", r, err_str); - corrupted = true; } return r; } @@ -292,7 +291,7 @@ static int pull_part(void *a) { sprintf(curl_c->partfn.str[n], "%s.%d", curl_c->outfn, n); DEBUG_PRINT("[THRD %hhu] partfn: %s, range: %s\n", n, get_str_element(&curl_c->partfn, n), ti->range); - { + for (unsigned char retry = 0;; retry++) { curl_c->fplist[n] = fopen(get_str_element(&curl_c->partfn, n), "wb+"); CURL *curl; @@ -313,6 +312,14 @@ static int pull_part(void *a) { append_log("[THRD %hhu] File downloaded.\n", n); curl_easy_cleanup(curl); logerr(res); + if (!res) { + break; + } + if (retry + 1 == MAX_RETRY) { + append_log("Error after %d retries, exiting...\n", MAX_RETRY); + corrupted = true; + break; + } } mtx_lock(&mtx); curl_c->success_thrd += 1; -- cgit v1.2.3