From 052ed1f58ceaca1be6447642f563e7d3bf656194 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adrien=20B=C3=A9raud?= <adrien.beraud@savoirfairelinux.com> Date: Sat, 18 Jan 2020 20:03:28 -0500 Subject: [PATCH] proxy server: handlePrintStats on startup --- include/opendht/dht_proxy_server.h | 7 ++-- src/dht_proxy_server.cpp | 51 +++++++++++------------------- 2 files changed, 22 insertions(+), 36 deletions(-) diff --git a/include/opendht/dht_proxy_server.h b/include/opendht/dht_proxy_server.h index a864eaa3..e5fbc5b6 100644 --- a/include/opendht/dht_proxy_server.h +++ b/include/opendht/dht_proxy_server.h @@ -310,6 +310,7 @@ private: using clock = std::chrono::steady_clock; using time_point = clock::time_point; + std::shared_ptr<asio::io_context> ioContext_; std::shared_ptr<DhtRunner> dht_; Json::StreamWriterBuilder jsonBuilder_; Json::CharReaderBuilder jsonReaderBuilder_; @@ -321,14 +322,14 @@ private: // http client std::pair<std::string, std::string> pushHostPort_; - + mutable std::mutex requestLock_; std::map<unsigned int /*id*/, std::shared_ptr<http::Request>> requests_; std::shared_ptr<dht::Logger> logger_; - mutable std::shared_ptr<ServerStats> stats_; - mutable std::shared_ptr<NodeInfo> nodeInfo_ {}; + std::shared_ptr<ServerStats> stats_; + std::shared_ptr<NodeInfo> nodeInfo_ {}; std::unique_ptr<asio::steady_timer> printStatsTimer_; // Thread-safe access to listeners map. diff --git a/src/dht_proxy_server.cpp b/src/dht_proxy_server.cpp index da41154f..793a4792 100644 --- a/src/dht_proxy_server.cpp +++ b/src/dht_proxy_server.cpp @@ -171,7 +171,9 @@ DhtProxyServer::DhtProxyServer( std::shared_ptr<DhtRunner> dht, in_port_t port, const std::string& pushServer, std::shared_ptr<Logger> logger ) - : dht_(dht), logger_(logger), + : ioContext_(std::make_shared<asio::io_context>()), + dht_(dht), logger_(logger), + printStatsTimer_(std::make_unique<asio::steady_timer>(*ioContext_, 3s)), connListener_(std::make_shared<ConnectionListener>(std::bind(&DhtProxyServer::onConnectionClosed, this, std::placeholders::_1))), pushServer_(pushServer) { @@ -235,7 +237,7 @@ DhtProxyServer::DhtProxyServer( settings.port(port); settings.tls_context(std::move(tls_context)); httpsServer_ = std::make_unique<restinio::http_server_t<RestRouterTraitsTls>>( - restinio::own_io_context(), + ioContext_, std::forward<restinio::run_on_this_thread_settings_t<RestRouterTraitsTls>>(std::move(settings)) ); // run http server @@ -251,7 +253,7 @@ DhtProxyServer::DhtProxyServer( addServerSettings(settings); settings.port(port); httpServer_ = std::make_unique<restinio::http_server_t<RestRouterTraits>>( - restinio::own_io_context(), + ioContext_, std::forward<restinio::run_on_this_thread_settings_t<RestRouterTraits>>(std::move(settings)) ); // run http server @@ -263,20 +265,13 @@ DhtProxyServer::DhtProxyServer( }); } dht->forwardAllMessages(true); - - printStatsTimer_ = std::make_unique<asio::steady_timer>(io_context(), 0s); printStatsTimer_->async_wait(std::bind(&DhtProxyServer::handlePrintStats, this, std::placeholders::_1)); } - asio::io_context& DhtProxyServer::io_context() const { - if (httpsServer_) - return httpsServer_->io_context(); - else if (httpServer_) - return httpServer_->io_context(); - throw std::runtime_error("No available server"); + return *ioContext_; } DhtProxyServer::~DhtProxyServer() @@ -304,10 +299,7 @@ DhtProxyServer::~DhtProxyServer() } if (logger_) logger_->d("[proxy:server] closing http server"); - if (httpServer_) - httpServer_->io_context().stop(); - if (httpsServer_) - httpsServer_->io_context().stop(); + ioContext_->stop(); if (serverThread_.joinable()) serverThread_.join(); if (logger_) @@ -480,21 +472,16 @@ RequestStatus DhtProxyServer::getNodeInfo(restinio::request_handle_t request, restinio::router::route_params_t /*params*/) const { - Json::Value result; - auto nodeInfo = nodeInfo_; - if (not nodeInfo) { - nodeInfo = std::make_shared<NodeInfo>(dht_->getNodeInfo()); - nodeInfo_ = nodeInfo; - if (auto stats = stats_) - stats->nodeInfo = nodeInfo; + if (auto nodeInfo = nodeInfo_) { + auto result = nodeInfo->toJson(); + // [ipv6:ipv4]:port or ipv4:port + result["public_ip"] = request->remote_endpoint().address().to_string(); + auto response = initHttpResponse(request->create_response()); + response.append_body(Json::writeString(jsonBuilder_, result) + "\n"); + return response.done(); } - result = nodeInfo->toJson(); - // [ipv6:ipv4]:port or ipv4:port - result["public_ip"] = request->remote_endpoint().address().to_string(); - auto output = Json::writeString(jsonBuilder_, result) + "\n"; - - auto response = initHttpResponse(request->create_response()); - response.append_body(output); + auto response = initHttpResponse(request->create_response(restinio::status_service_unavailable())); + response.set_body(RESP_MSG_SERVICE_UNAVAILABLE); return response.done(); } @@ -731,11 +718,9 @@ DhtProxyServer::subscribe(restinio::request_handle_t request, return true; } ); - // Launch timers - auto& ctx = io_context(); // expire notify if (!listener.expireNotifyTimer) - listener.expireNotifyTimer = std::make_unique<asio::steady_timer>(ctx, timeout - proxy::OP_MARGIN); + listener.expireNotifyTimer = std::make_unique<asio::steady_timer>(io_context(), timeout - proxy::OP_MARGIN); else listener.expireNotifyTimer->expires_at(timeout - proxy::OP_MARGIN); auto jsonProvider = [infoHash, clientId, sessionCtx = listener.sessionCtx](){ @@ -750,7 +735,7 @@ DhtProxyServer::subscribe(restinio::request_handle_t request, std::placeholders::_1, pushToken, std::move(jsonProvider), isAndroid)); // cancel push listen if (!listener.expireTimer) - listener.expireTimer = std::make_unique<asio::steady_timer>(ctx, timeout); + listener.expireTimer = std::make_unique<asio::steady_timer>(io_context(), timeout); else listener.expireTimer->expires_at(timeout); listener.expireTimer->async_wait(std::bind(&DhtProxyServer::handleCancelPushListen, this, -- GitLab