Skip to content

Commit

Permalink
【CURL】Optimize requests to use caches and TCP keep alive.
Browse files Browse the repository at this point in the history
  • Loading branch information
skylersaleh committed Aug 17, 2024
1 parent 5987e95 commit e2018b0
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions src/https.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ struct thread_pool {

private:
void main_loop() {
CURL* curl = curl_easy_init();
curl_easy_setopt(curl, CURLOPT_TCP_KEEPALIVE, 1L);
curl_easy_setopt(curl, CURLOPT_TCP_NODELAY, 1L);
while (!terminate_all) {
job j;
{
Expand All @@ -60,11 +63,12 @@ struct thread_pool {
j = jobs.front();
jobs.pop();
}
handle_job(j);
handle_job(j,curl);
}
curl_easy_cleanup(curl);
}

void handle_job(job& j);
void handle_job(job& j, CURL* curl);

std::atomic_bool terminate_all = { false };

Expand Down Expand Up @@ -188,9 +192,8 @@ CURLcode sslctx_function(CURL *curl, void *sslctx, void *parm)
return rv;
}

void thread_pool::handle_job(job& j)
void thread_pool::handle_job(job& j, CURL * curl)
{
CURL* curl = curl_easy_init();
if (!curl)
{
printf("[cloud] failed to initialize curl\n");
Expand Down Expand Up @@ -248,6 +251,12 @@ void thread_pool::handle_job(job& j)
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, chunk);

res = curl_easy_perform(curl);
res = curl_easy_setopt(curl, CURLOPT_HTTPGET, 0L); se_validate();
res = curl_easy_setopt(curl, CURLOPT_POST, 0L); se_validate();
res = curl_easy_setopt(curl, CURLOPT_POSTFIELDS, ""); se_validate();
res = curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, 0); se_validate();


if (res != CURLE_OK)
{
printf("[cloud] curl failed: %s\n", curl_easy_strerror(res));
Expand All @@ -259,7 +268,6 @@ void thread_pool::handle_job(job& j)
}

curl_slist_free_all(chunk);
curl_easy_cleanup(curl);
}
#endif

Expand Down

0 comments on commit e2018b0

Please sign in to comment.