Skip to content

Instantly share code, notes, and snippets.

@jeroen
Last active June 20, 2023 12:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeroen/3089e67bfc858127ef7496b43e87b06b to your computer and use it in GitHub Desktop.
Save jeroen/3089e67bfc858127ef7496b43e87b06b to your computer and use it in GitHub Desktop.
Test number of file descriptors
#include <curl/curl.h>
#include <stdlib.h>
#include <string.h>
size_t do_nothing(void *contents, size_t sz, size_t nmemb, void *ctx){
return sz * nmemb;
}
CURL *make_handle(char *url){
CURL *handle = curl_easy_init();
curl_easy_setopt(handle, CURLOPT_URL, url);
curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, do_nothing);
curl_easy_setopt(handle, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_setopt(handle, CURLOPT_HTTP_VERSION, 2L);
return handle;
}
void print_fd_count(CURLM *multi, int still_running){
fd_set read_fd_set, write_fd_set, exc_fd_set;
int read_fd = 0, write_fd = 0, exc_fd = 0, max_fd = 0;
FD_ZERO(&read_fd_set);
FD_ZERO(&write_fd_set);
FD_ZERO(&exc_fd_set);
curl_multi_fdset(multi, &read_fd_set, &write_fd_set, &exc_fd_set, &max_fd);
for (int i = 0; i <= max_fd; i++){
if (FD_ISSET(i, &read_fd_set)) read_fd++;
if (FD_ISSET(i, &write_fd_set)) write_fd++;
if (FD_ISSET(i, &exc_fd_set)) exc_fd++;
}
printf("Pending: %d. FD count: (read) %d, (write) %d, (exc) %d\n", still_running, read_fd, write_fd, exc_fd);
}
int main(void) {
const curl_version_info_data *data = curl_version_info(CURLVERSION_NOW);
printf("Using curl %s\n", data->version);
curl_global_init(CURL_GLOBAL_DEFAULT);
CURLM *multi_handle = curl_multi_init();
curl_multi_setopt(multi_handle, CURLMOPT_MAX_HOST_CONNECTIONS, 5L);
curl_multi_add_handle(multi_handle, make_handle("https://hb.opencpu.org/delay/4"));
curl_multi_add_handle(multi_handle, make_handle("https://hb.opencpu.org/delay/3"));
curl_multi_add_handle(multi_handle, make_handle("https://hb.opencpu.org/delay/2"));
curl_multi_add_handle(multi_handle, make_handle("https://hb.opencpu.org/delay/1"));
curl_multi_add_handle(multi_handle, make_handle("https://hb.opencpu.org/delay/0"));
curl_multi_add_handle(multi_handle, make_handle("https://hb.opencpu.org/delay/1"));
curl_multi_add_handle(multi_handle, make_handle("https://hb.opencpu.org/delay/2"));
curl_multi_add_handle(multi_handle, make_handle("https://hb.opencpu.org/delay/3"));
curl_multi_add_handle(multi_handle, make_handle("https://hb.opencpu.org/delay/4"));
int msgs_left;
int pending = 0;
int complete = 0;
int still_running = 1;
while(still_running) {
int numfds;
curl_multi_perform(multi_handle, &still_running);
curl_multi_wait(multi_handle, NULL, 0, 1000, &numfds);
/* See how the transfers went */
CURLMsg *m = NULL;
while((m = curl_multi_info_read(multi_handle, &msgs_left))) {
if(m->msg == CURLMSG_DONE) {
CURL *handle = m->easy_handle;
curl_multi_remove_handle(multi_handle, handle);
curl_easy_cleanup(handle);
}
print_fd_count(multi_handle, still_running);
}
}
curl_multi_cleanup(multi_handle);
curl_global_cleanup();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment