From d566cb9b86fa8df54283e82e5f3756ac7db1aa8d Mon Sep 17 00:00:00 2001 From: gulliver Date: Mon, 14 Oct 2024 15:35:02 +0200 Subject: [PATCH] added wait timeout to server start --- include/crow/app.h | 27 ++++++++++++++++++++------- include/crow/http_server.h | 11 +++++++---- 2 files changed, 27 insertions(+), 11 deletions(-) diff --git a/include/crow/app.h b/include/crow/app.h index 25592da69..82d099205 100644 --- a/include/crow/app.h +++ b/include/crow/app.h @@ -703,19 +703,32 @@ namespace crow } /// \brief Wait until the server has properly started - void wait_for_server_start() + std::cv_status wait_for_server_start(std::chrono::milliseconds wait_timeout = std::chrono::milliseconds(3000)) { + std::cv_status status = std::cv_status::no_timeout; + auto wait_until = std::chrono::steady_clock::now() + wait_timeout; { std::unique_lock lock(start_mutex_); - while (!server_started_) - cv_started_.wait(lock); + while (!server_started_ && (status == std::cv_status::no_timeout)) + { + status = cv_started_.wait_until(lock, wait_until); + } } - if (server_) - server_->wait_for_start(); + + if (status == std::cv_status::no_timeout) + { + if (server_) + { + status = server_->wait_for_start(wait_until); + } #ifdef CROW_ENABLE_SSL - else if (ssl_server_) - ssl_server_->wait_for_start(); + else if (ssl_server_) + { + status = ssl_server_->wait_for_start(wait_until); + } #endif + } + return status; } private: diff --git a/include/crow/http_server.h b/include/crow/http_server.h index 6834a2637..90c95364d 100644 --- a/include/crow/http_server.h +++ b/include/crow/http_server.h @@ -196,12 +196,15 @@ namespace crow // NOTE: Already documented in "crow/app.h" return acceptor_.local_endpoint().port(); } - /// Wait until the server has properly started - void wait_for_start() + /// Wait until the server has properly started or until timeout + std::cv_status wait_for_start(std::chrono::steady_clock::time_point wait_until) { std::unique_lock lock(start_mutex_); - while (!server_started_) - cv_started_.wait(lock); + + std::cv_status status = std::cv_status::no_timeout; + while (!server_started_ && ( status==std::cv_status::no_timeout )) + status = cv_started_.wait_until(lock,wait_until); + return status; } void signal_clear()