microsoft / cpprestsdk

The C++ REST SDK is a Microsoft project for cloud-based client-server communication in native code using a modern asynchronous C++ API design. This project aims to help C++ developers connect to and interact with services.
Other
8.01k stars 1.66k forks source link

use_certificate_file: Too many open files #1365

Open uf8dx2 opened 4 years ago

uf8dx2 commented 4 years ago

I'm getting this error each time when the number of TCP connections reach 1024. I can see with lsof command that each of those connection has status ESTABLISHED. How can I use the library to close the connections that are unused for a long time?

    web::http::experimental::listener::http_listener_config conf;
    conf.set_ssl_context_callback([](boost::asio::ssl::context &ctx) {
        ctx.set_options(boost::asio::ssl::context::default_workarounds);
        ctx.set_password_callback([](std::size_t max_length, boost::asio::ssl::context::password_purpose purpose) {
            return "somepassword";
        });
        ctx.use_certificate_file("cert/cert.pem", boost::asio::ssl::context::pem);
        ctx.use_private_key_file("cert/key.pem", boost::asio::ssl::context::pem);
    });

   http_listener listener(url, conf);

   listener.support(methods::GET,  handle_get);

   try {
      listener
         .open()
         .then([&listener]() {TRACE(L"\nstarting to listen\n"); })
         .wait();

      while (true) {
        usleep(1000000);
      }
   }
   catch (exception const & e) {
      wcout << e.what() << endl;
   }
uf8dx2 commented 3 years ago

Any one can help here after one year? I can increase the open file limits in my system, but it's just a workaround. No matter how much I increase the limit, the open connections will still reach it after some time and then I need to restart the server. I'm getting the following exception:

terminate called after throwing an instance of 'boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector >' what(): use_certificate_file: Too many open files

I feel like I'm doing something wrong with the code, because no one has ever reported something like I have. Any help is greatly appreciated.