if we get HTTP 429, wait for 5 secs

This commit is contained in:
Fufu Fang 2019-04-26 13:20:55 +01:00
parent 9065dcffb5
commit 774f14c8e7
2 changed files with 24 additions and 15 deletions

View File

@ -35,7 +35,7 @@ typedef enum {
EFREAD = -1, /**< Fread failed */ EFREAD = -1, /**< Fread failed */
EINCONSIST = -2, /**< Inconsistency in metadata */ EINCONSIST = -2, /**< Inconsistency in metadata */
EZERO = -3, /**< Unexpected zeros in metadata */ EZERO = -3, /**< Unexpected zeros in metadata */
EMEM = -4, /**< Memory allocation failure */ EMEM = -4 /**< Memory allocation failure */
} MetaError; } MetaError;

View File

@ -11,9 +11,12 @@
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
#define HTTP_OK 200 typedef enum {
#define HTTP_PARTIAL_CONTENT 206 HTTP_OK = 200,
#define HTTP_RANGE_NOT_SATISFIABLE 416 HTTP_PARTIAL_CONTENT = 206,
HTTP_RANGE_NOT_SATISFIABLE = 416,
HTTP_TOO_MANY_REQUESTS = 429
}HTTPResponseCode;
/* ---------------- External variables -----------------------*/ /* ---------------- External variables -----------------------*/
LinkTable *ROOT_LINK_TBL = NULL; LinkTable *ROOT_LINK_TBL = NULL;
@ -267,19 +270,25 @@ LinkTable *LinkTable_new(const char *url)
buf.memory = NULL; buf.memory = NULL;
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&buf); curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&buf);
/* If we get HTTP 429, wait for 5 seconds before retry */
volatile long http_resp = 0;
do {
transfer_blocking(curl); transfer_blocking(curl);
/* if downloading base URL failed */
long http_resp;
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_resp); curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_resp);
if (http_resp != HTTP_OK) { if (http_resp == HTTP_TOO_MANY_REQUESTS) {
fprintf(stderr, "link.c: LinkTable_new() cannot retrieve the base URL, \ fprintf(stderr, "link.c: LinkTable_new(): HTTP 429, Too Many \
URL: %s, HTTP %ld\n", url, http_resp); Requests, URL: %s, HTTP %ld\n", url, http_resp);
sleep(5);
} else if (http_resp != HTTP_OK) {
fprintf(stderr, "link.c: LinkTable_new(): cannot retrieve the base \
URL, URL: %s, HTTP %ld\n", url, http_resp);
LinkTable_free(linktbl); LinkTable_free(linktbl);
curl_easy_cleanup(curl); curl_easy_cleanup(curl);
return NULL; return NULL;
}; };
} while (http_resp != HTTP_OK);
curl_easy_getinfo(curl, CURLINFO_FILETIME, &(head_link->time)); curl_easy_getinfo(curl, CURLINFO_FILETIME, &(head_link->time));
curl_easy_cleanup(curl); curl_easy_cleanup(curl);