Skip to content

Commit

Permalink
added wait timeout to server start
Browse files Browse the repository at this point in the history
  • Loading branch information
gittiver committed Nov 25, 2024
1 parent 52a213d commit d566cb9
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 11 deletions.
27 changes: 20 additions & 7 deletions include/crow/app.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::mutex> 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:
Expand Down
11 changes: 7 additions & 4 deletions include/crow/http_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::mutex> 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()
Expand Down

0 comments on commit d566cb9

Please sign in to comment.