diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 75886594..430251e0 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -109,6 +109,11 @@ jobs: run: cd test && make test_split && make test_openssl_parallel env: LSAN_OPTIONS: suppressions=lsan_suppressions.txt + - name: build and run tests with keep-alive parking on (OpenSSL) + if: matrix.tls_backend == 'openssl' + run: cd test && make test_parking_parallel + env: + LSAN_OPTIONS: suppressions=lsan_suppressions.txt - name: build and run tests (Mbed TLS) if: matrix.tls_backend == 'mbedtls' run: cd test && make test_split_mbedtls && make test_mbedtls_parallel @@ -417,6 +422,11 @@ jobs: run: cd test && make test_split && make test_openssl_parallel env: LSAN_OPTIONS: suppressions=lsan_suppressions.txt + - name: build and run tests with keep-alive parking on (OpenSSL) + if: matrix.tls_backend == 'openssl' + run: cd test && make test_parking_parallel + env: + LSAN_OPTIONS: suppressions=lsan_suppressions.txt - name: build and run tests (Mbed TLS) if: matrix.tls_backend == 'mbedtls' run: cd test && make test_split_mbedtls && make test_mbedtls_parallel diff --git a/.gitignore b/.gitignore index b4400f64..5eb43ea9 100644 --- a/.gitignore +++ b/.gitignore @@ -43,6 +43,7 @@ test/test test/test_mbedtls test/test_wolfssl test/test_no_tls +test/test_parking test/server_fuzzer test/client_fuzzer test/header_parser_fuzzer diff --git a/README.md b/README.md index ccce594f..16a953e0 100644 --- a/README.md +++ b/README.md @@ -921,10 +921,15 @@ svr.set_keep_alive_timeout(10); // Default is 5 #### Keep-alive parking +> [!NOTE] +> Keep-alive parking is experimental. Its behavior may change in a future release. + ```cpp svr.set_keep_alive_parking(true); // Call it before listen() ``` +Defining `CPPHTTPLIB_KEEPALIVE_PARKING` as `true` turns it on for every `Server` from the start. If the event loop cannot be set up, the server serves as it does without it. + By default, a kept-alive connection holds a worker thread while it waits for its next request, so connections beyond the thread count wait for one to be closed. With keep-alive parking, a connection waiting for its next request is handed to an event loop instead, and goes back to a worker when the request arrives. An idle connection then costs a socket and a few kilobytes instead of a thread, and connections that outnumber the threads take turns. With it: diff --git a/docs-src/pages/en/cookbook/s20-keep-alive.md b/docs-src/pages/en/cookbook/s20-keep-alive.md index 95aff5dc..9c51cb5e 100644 --- a/docs-src/pages/en/cookbook/s20-keep-alive.md +++ b/docs-src/pages/en/cookbook/s20-keep-alive.md @@ -56,11 +56,13 @@ By default, a Keep-Alive connection holds a worker thread for its entire lifetim ## Keep idle connections off the threads +> **Note:** Keep-alive parking is experimental. Its behavior may change in a future release. + ```cpp svr.set_keep_alive_parking(true); // call it before listen() ``` -With keep-alive parking, a connection waiting for its next request is handed to an event loop instead of keeping its worker thread, and goes back to a worker when the request arrives. An idle connection then costs a socket and some memory (a few kilobytes, more with TLS) instead of a thread, and connections that outnumber the threads take turns instead of waiting for one to close. `SSLServer` supports it too. +With keep-alive parking, a connection waiting for its next request is handed to an event loop instead of keeping its worker thread, and goes back to a worker when the request arrives. An idle connection then costs a socket and some memory (a few kilobytes, more with TLS) instead of a thread, and connections that outnumber the threads take turns instead of waiting for one to close. `SSLServer` supports it too. Defining `CPPHTTPLIB_KEEPALIVE_PARKING` as `true` turns it on for every server. What changes with it: diff --git a/docs-src/pages/ja/cookbook/s20-keep-alive.md b/docs-src/pages/ja/cookbook/s20-keep-alive.md index 4cedd4f4..fd781281 100644 --- a/docs-src/pages/ja/cookbook/s20-keep-alive.md +++ b/docs-src/pages/ja/cookbook/s20-keep-alive.md @@ -56,11 +56,13 @@ svr.set_keep_alive_max_count(1000); ## アイドル接続にスレッドを持たせない +> **Note:** Keep-Aliveのパーキングは実験的な機能です。今後のリリースで挙動が変わることがあります。 + ```cpp svr.set_keep_alive_parking(true); // listen()の前に呼ぶ ``` -Keep-Aliveのパーキングを有効にすると、次のリクエストを待っている接続はワーカースレッドを手放してイベントループに預けられ、リクエストが届くとまたワーカーに戻ります。アイドル接続のコストはスレッド1つではなく、ソケットと少しのメモリ(数KB、TLSではもう少し多い)になり、スレッド数より多い接続も、どれかが閉じるのを待たずに順番に処理されます。`SSLServer`でも使えます。 +Keep-Aliveのパーキングを有効にすると、次のリクエストを待っている接続はワーカースレッドを手放してイベントループに預けられ、リクエストが届くとまたワーカーに戻ります。アイドル接続のコストはスレッド1つではなく、ソケットと少しのメモリ(数KB、TLSではもう少し多い)になり、スレッド数より多い接続も、どれかが閉じるのを待たずに順番に処理されます。`SSLServer`でも使えます。`CPPHTTPLIB_KEEPALIVE_PARKING`を`true`と定義すると、すべてのサーバーで最初から有効になります。 有効にすると変わること: diff --git a/httplib.h b/httplib.h index 43943c5a..d5e53c34 100644 --- a/httplib.h +++ b/httplib.h @@ -34,6 +34,11 @@ #define CPPHTTPLIB_KEEPALIVE_TIMEOUT_SECOND 5 #endif +// Whether a Server starts with set_keep_alive_parking(true). +#ifndef CPPHTTPLIB_KEEPALIVE_PARKING +#define CPPHTTPLIB_KEEPALIVE_PARKING false +#endif + #ifndef CPPHTTPLIB_KEEPALIVE_TIMEOUT_CHECK_INTERVAL_USECOND #define CPPHTTPLIB_KEEPALIVE_TIMEOUT_CHECK_INTERVAL_USECOND 10000 #endif @@ -14552,6 +14557,7 @@ inline Server::Server() #ifndef _WIN32 signal(SIGPIPE, SIG_IGN); #endif + set_keep_alive_parking(CPPHTTPLIB_KEEPALIVE_PARKING); } inline Server::~Server() = default; diff --git a/test/Makefile b/test/Makefile index 600e7cf2..e283b6ba 100644 --- a/test/Makefile +++ b/test/Makefile @@ -100,7 +100,7 @@ define run_parallel echo "All shards passed." endef -.PHONY: test_openssl_parallel test_mbedtls_parallel test_wolfssl_parallel test_no_tls_parallel +.PHONY: test_openssl_parallel test_mbedtls_parallel test_wolfssl_parallel test_no_tls_parallel test_parking_parallel test_openssl_parallel : test $(call run_parallel,test) @@ -114,6 +114,9 @@ test_wolfssl_parallel : test_wolfssl test_no_tls_parallel : test_no_tls $(call run_parallel,test_no_tls) +test_parking_parallel : test_parking + $(call run_parallel,test_parking) + proxy : test_proxy @echo "Starting proxy server..." cd proxy && \ @@ -201,6 +204,11 @@ test_no_tls : test.cc include_httplib.cc ../httplib.h Makefile $(CXX) -o $@ -I.. $(CXXFLAGS) test.cc include_httplib.cc $(TEST_ARGS_NO_TLS) @file $@ +# The whole suite with keep-alive parking on by default +test_parking : test.cc include_httplib.cc ../httplib.h Makefile cert.pem + $(CXX) -o $@ -I.. $(CXXFLAGS) -DCPPHTTPLIB_KEEPALIVE_PARKING=true test.cc include_httplib.cc $(TEST_ARGS) + @file $@ + test_split_no_tls : test.cc ../httplib.h httplib.cc Makefile $(CXX) -o $@ $(CXXFLAGS) test.cc httplib.cc $(TEST_ARGS_NO_TLS) @@ -321,5 +329,5 @@ cert.pem: ./gen-certs.sh clean: - rm -rf test test_split test_mbedtls test_split_mbedtls test_wolfssl test_split_wolfssl test_no_tls, test_split_no_tls test_proxy test_proxy_mbedtls test_proxy_wolfssl test_benchmark server_fuzzer client_fuzzer header_parser_fuzzer url_parser_fuzzer *.pem *.0 *.o *.1 *.srl httplib.h httplib.cc _build* *.dSYM *_shard_*.log cpp-httplib + rm -rf test test_split test_parking test_mbedtls test_split_mbedtls test_wolfssl test_split_wolfssl test_no_tls, test_split_no_tls test_proxy test_proxy_mbedtls test_proxy_wolfssl test_benchmark server_fuzzer client_fuzzer header_parser_fuzzer url_parser_fuzzer *.pem *.0 *.o *.1 *.srl httplib.h httplib.cc _build* *.dSYM *_shard_*.log cpp-httplib diff --git a/test/test.cc b/test/test.cc index 74567477..a6900b57 100644 --- a/test/test.cc +++ b/test/test.cc @@ -5864,6 +5864,8 @@ TEST(SSLClientServerTest, ClientSessionIsNotResumedPastItsCA) { TEST(SSLClientServerTest, UpdateCertsPemWaitsForHandshakeInProgress) { SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE); ASSERT_TRUE(svr.is_valid()); + // Keep-alive parking would wait for the ClientHello without a handshake. + svr.set_keep_alive_parking(false); svr.Get("/test", [](const Request &, Response &res) { res.set_content("ok", "text/plain"); }); @@ -17495,6 +17497,8 @@ TEST(SSLClientServerTest, SSLConnectTimeout) { NoListenSSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE, CLIENT_CA_CERT_FILE); ASSERT_TRUE(svr.is_valid()); + // Keep-alive parking would bypass the override. + svr.set_keep_alive_parking(false); svr.Get("/test", [&](const Request &, Response &res) { res.set_content("test", "text/plain");