feat: Graceful shutdown with old web server (#2786)

- Stop accepting connections during graceful shutdown in the old web server
- Stop all the services before Clio exits
- Move cache saving into stop callback
This commit is contained in:
Sergey Kuznetsov
2025-11-19 15:40:33 +00:00
committed by GitHub
parent 56f074e6ee
commit b62cfe949f
17 changed files with 414 additions and 47 deletions

View File

@@ -34,6 +34,7 @@
#include <boost/asio/ip/address.hpp>
#include <boost/asio/ip/tcp.hpp>
#include <boost/asio/socket_base.hpp>
#include <boost/asio/spawn.hpp>
#include <boost/asio/ssl/context.hpp>
#include <boost/asio/ssl/error.hpp>
#include <boost/asio/strand.hpp>
@@ -43,6 +44,7 @@
#include <boost/beast/core/tcp_stream.hpp>
#include <fmt/format.h>
#include <atomic>
#include <chrono>
#include <cstdint>
#include <exception>
@@ -221,7 +223,8 @@ template <
template <typename> class PlainSessionType,
template <typename> class SslSessionType,
SomeServerHandler HandlerType>
class Server : public std::enable_shared_from_this<Server<PlainSessionType, SslSessionType, HandlerType>> {
class Server : public ServerTag,
public std::enable_shared_from_this<Server<PlainSessionType, SslSessionType, HandlerType>> {
using std::enable_shared_from_this<Server<PlainSessionType, SslSessionType, HandlerType>>::shared_from_this;
util::Logger log_{"WebServer"};
@@ -235,6 +238,7 @@ class Server : public std::enable_shared_from_this<Server<PlainSessionType, SslS
std::shared_ptr<AdminVerificationStrategy> adminVerification_;
std::uint32_t maxWsSendingQueueSize_;
std::shared_ptr<ProxyIpResolver> proxyIpResolver_;
std::atomic_bool isStopped_{false};
public:
/**
@@ -308,6 +312,13 @@ public:
doAccept();
}
/** @brief Stop accepting new connections */
void
stop(boost::asio::yield_context)
{
isStopped_ = true;
}
private:
void
doAccept()
@@ -321,6 +332,10 @@ private:
void
onAccept(boost::beast::error_code ec, tcp::socket socket)
{
if (isStopped_) {
return;
}
if (!ec) {
auto ctxRef =
ctx_ ? std::optional<std::reference_wrapper<boost::asio::ssl::context>>{ctx_.value()} : std::nullopt;

View File

@@ -22,6 +22,7 @@
#include <boost/beast.hpp>
#include <boost/beast/core/error.hpp>
#include <concepts>
#include <memory>
#include <string>
@@ -39,4 +40,14 @@ concept SomeServerHandler =
{ handler(req, ws) };
};
/**
* @brief A tag class for server to help identify Server in templated code.
*/
struct ServerTag {
virtual ~ServerTag() = default;
};
template <typename T>
concept SomeServer = std::derived_from<T, ServerTag>;
} // namespace web

View File

@@ -23,6 +23,7 @@
#include "util/config/ConfigDefinition.hpp"
#include "util/log/Logger.hpp"
#include "web/ProxyIpResolver.hpp"
#include "web/interface/Concepts.hpp"
#include "web/ng/Connection.hpp"
#include "web/ng/MessageHandler.hpp"
#include "web/ng/ProcessingPolicy.hpp"
@@ -34,7 +35,6 @@
#include <boost/asio/spawn.hpp>
#include <boost/asio/ssl/context.hpp>
#include <concepts>
#include <cstddef>
#include <functional>
#include <optional>
@@ -42,16 +42,6 @@
namespace web::ng {
/**
* @brief A tag class for server to help identify Server in templated code.
*/
struct ServerTag {
virtual ~ServerTag() = default;
};
template <typename T>
concept SomeServer = std::derived_from<T, ServerTag>;
/**
* @brief Web server class.
*/