Last active
June 11, 2019 21:23
-
-
Save cliffcrosland/bd1681f06de12e1c9489eac5632e444a to your computer and use it in GitHub Desktop.
Demonstration: Old connections are re-used when they shouldn't be
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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