-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Description
I can get my server to handle more SSL connections before using all the RAM of my machine if I instead of using ssl::stream I use ip::tcp::socket and let a nginx reverse proxy handle the SSL.
As far as I can see the problem is the use of a BIO pair (each with its own buffer), and the lack of access to the internal buffers of these BIOs, which creates the need for yet another 17 KiB ASIO buffer pair. #440 suggests using BIO_nread to be able to access the internal buffers. But that's an undocumented function and https://github.com/openssl/openssl/blob/master/crypto/bio/bss_bio.c#L199 says WARNING: The non-copying interface is largely untested as of yet and may contain bugs.
The use of SSL_MODE_RELEASE_BUFFERS is not helping (enough). In part because of the ssl::detail::stream_core::max_tls_record_size value.
Notice that an user worried about memory usage may be using https://www.openssl.org/docs/manmaster/man3/SSL_CTX_set_tlsext_max_fragment_length.html to reduce the maximum fragment size to 512 bytes, but ASIO would still be allocating those two 17 KiB buffers.
From my understanding the best solution would be creating a custom BIO_s_asio (e.g. https://github.com/liuqun/openssl-dtls-custom-bio/blob/majipeng-20190816/cbio.c). Since ASIO would be the BIO it would have direct access to everything, with no need for extra buffers and being able to respect SSL_CTX_set_tlsext_max_fragment_length()/SSL_CTX_set_max_send_fragment. Instead of restricting itself to the BIO external interface, ASIO could be a BIO itself.