From 4c413e245d227bae835528ddfa02b79bf47a0a36 Mon Sep 17 00:00:00 2001 From: Chunting Gu Date: Wed, 4 Sep 2019 16:27:01 +0800 Subject: [PATCH] Allow to get the size of the queue. --- examples/file_downloader.cc | 1 - webcc/client_session.cc | 2 +- webcc/queue.h | 7 ++++++- webcc/server.cc | 11 +++++++---- 4 files changed, 14 insertions(+), 7 deletions(-) diff --git a/examples/file_downloader.cc b/examples/file_downloader.cc index 5f3cb9f..d6ab77a 100644 --- a/examples/file_downloader.cc +++ b/examples/file_downloader.cc @@ -31,7 +31,6 @@ int main(int argc, char* argv[]) { if (auto file_body = r->file_body()) { file_body->Move(path); } - } catch (const std::exception& e) { std::cerr << e.what() << std::endl; } diff --git a/webcc/client_session.cc b/webcc/client_session.cc index 42cdf5d..e56b688 100644 --- a/webcc/client_session.cc +++ b/webcc/client_session.cc @@ -103,7 +103,7 @@ ResponsePtr ClientSession::DoSend(RequestPtr request, bool stream) { client->set_ssl_verify(ssl_verify_); client->set_buffer_size(buffer_size_); client->set_timeout(timeout_); - + Error error = client->Request(request, !reuse, stream); if (error) { diff --git a/webcc/queue.h b/webcc/queue.h index dfc19a5..41defb5 100644 --- a/webcc/queue.h +++ b/webcc/queue.h @@ -54,9 +54,14 @@ public: not_empty_cv_.notify_one(); } + std::size_t Size() const { + std::lock_guard lock(mutex_); + return message_list_.size(); + } + private: std::list message_list_; - std::mutex mutex_; + mutable std::mutex mutex_; std::condition_variable not_empty_cv_; }; diff --git a/webcc/server.cc b/webcc/server.cc index 557542a..2579c7b 100644 --- a/webcc/server.cc +++ b/webcc/server.cc @@ -250,10 +250,13 @@ void Server::WorkerRoutine() { void Server::StopWorkers() { LOG_INFO("Stopping workers..."); - // Clear pending connections. - // The connections will be closed later. - LOG_INFO("Clear pending connections..."); - queue_.Clear(); + // Clear/drop pending connections. + // The connections will be closed later (see DoStop). + // Alternatively, we can wait for the pending connections to be handled. + if (queue_.Size() != 0) { + LOG_INFO("Clear pending connections..."); + queue_.Clear(); + } // Enqueue a null connection to trigger the first worker to stop. queue_.Push(ConnectionPtr());