Skip to content

Instantly share code, notes, and snippets.

@cliffcrosland
Last active Jun 11, 2019
Embed
What would you like to do?
Demonstration: Old connections are re-used when they shouldn't be
#include <curl/curl.h>
#include <unistd.h>
int main(int argc, char **argv) {
const long maxage_conn = 1L;
curl_global_init(CURL_GLOBAL_ALL);
CURL *easy = curl_easy_init();
curl_easy_setopt(easy, CURLOPT_URL, "https://httpbin.org/get");
curl_easy_setopt(easy, CURLOPT_HTTPGET, 1);
curl_easy_setopt(easy, CURLOPT_VERBOSE, 1);
// Connections should become too old for re-use after 1 second.
curl_easy_setopt(easy, CURLOPT_MAXAGE_CONN, maxage_conn);
CURLcode code = curl_easy_perform(easy);
if (code != CURLE_OK) return 1;
printf(">>> Sleep for 5 seconds...\n");
usleep(5000000);
printf(">>> Should not re-use previous host connection from cache.\n");
printf(">>> In curl verbose logs, we should see: \"Too old connection (5 seconds), disconnect it\"\n");
printf(">>> Instead, we will see that it reuses the existing connection.\n");
curl_easy_reset(easy);
curl_easy_setopt(easy, CURLOPT_URL, "https://httpbin.org/get");
curl_easy_setopt(easy, CURLOPT_HTTPGET, 1);
curl_easy_setopt(easy, CURLOPT_VERBOSE, 1);
curl_easy_setopt(easy, CURLOPT_MAXAGE_CONN, maxage_conn);
code = curl_easy_perform(easy);
if (code != CURLE_OK) return 1;
curl_easy_cleanup(easy);
curl_global_cleanup();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment