Skip to content

Commit

Permalink
vere: add retry logic to _king_curl_bytes
Browse files Browse the repository at this point in the history
If the curl fails or receives a non-2xx status code, it retries, backing
off a little bit, until it either succeeds, or exceeds five attempts.
  • Loading branch information
Fang- committed Oct 23, 2023
1 parent 2b024e9 commit 964c9cd
Showing 1 changed file with 23 additions and 16 deletions.
39 changes: 23 additions & 16 deletions pkg/vere/king.c
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ _king_curl_bytes(c3_c* url_c, c3_w* len_w, c3_y** hun_y, c3_t veb_t)
CURL *cul_u;
CURLcode res_i;
long cod_i;
c3_y try_y = 0;
uv_buf_t buf_u = uv_buf_init(c3_malloc(1), 0);

if ( !(cul_u = curl_easy_init()) ) {
Expand All @@ -263,29 +264,34 @@ _king_curl_bytes(c3_c* url_c, c3_w* len_w, c3_y** hun_y, c3_t veb_t)
curl_easy_setopt(cul_u, CURLOPT_WRITEFUNCTION, _king_curl_alloc);
curl_easy_setopt(cul_u, CURLOPT_WRITEDATA, (void*)&buf_u);

res_i = curl_easy_perform(cul_u);
curl_easy_getinfo(cul_u, CURLINFO_RESPONSE_CODE, &cod_i);
while ( 5 > try_y ) {
sleep(try_y++);
res_i = curl_easy_perform(cul_u);
curl_easy_getinfo(cul_u, CURLINFO_RESPONSE_CODE, &cod_i);

// XX retry?
//
if ( CURLE_OK != res_i ) {
if ( veb_t ) {
u3l_log("curl: failed %s: %s", url_c, curl_easy_strerror(res_i));
if ( CURLE_OK != res_i ) {
if ( veb_t ) {
u3l_log("curl: failed to fetch %s: %s",
url_c, curl_easy_strerror(res_i));
}
ret_i = -1;
}
ret_i = -1;
}
if ( 300 <= cod_i ) {
if ( veb_t ) {
u3l_log("curl: error %s: HTTP %ld", url_c, cod_i);
else if ( 300 <= cod_i ) {
if ( veb_t ) {
u3l_log("curl: error fetching %s: HTTP %ld", url_c, cod_i);
}
ret_i = -2;
}
else {
*len_w = buf_u.len;
*hun_y = (c3_y*)buf_u.base;
ret_i = 0;
break;
}
ret_i = -2;
}

curl_easy_cleanup(cul_u);

*len_w = buf_u.len;
*hun_y = (c3_y*)buf_u.base;

return ret_i;
}

Expand Down Expand Up @@ -376,6 +382,7 @@ u3_king_next(c3_c* pac_c, c3_c** out_c)
}

// skip printfs on failed requests (/next is usually not present)
//REVIEW new retry logic means this case will take longer. make retries optional?
//
if ( _king_curl_bytes(url_c, &len_w, &hun_y, 0) ) {
c3_free(url_c);
Expand Down

0 comments on commit 964c9cd

Please sign in to comment.