Skip to content

Commit 0088beb

Browse files
committed
feat server: open monitor port early (attempt 3)
commit_hash:6d35ef9cc56d58f4b9988e07f7f12fdfb4280446
1 parent 35f0945 commit 0088beb

File tree

6 files changed

+44
-18
lines changed

6 files changed

+44
-18
lines changed

core/functional_tests/early_monitor_port_open/tests/test_boot.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
import asyncio
22

33
import aiohttp
4-
import pytest
54
import pytest_userver.utils.sync as sync
65

76

8-
@pytest.mark.skip(reason='the feature is not yet enabled')
97
async def test_monitor_port_is_open_before_all_components_are_ready(
108
ensure_daemon_started,
119
service_daemon_scope,

core/include/userver/server/server.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ class Server final : public congestion_control::Limitee, public congestion_contr
5353

5454
const http::HttpRequestHandler& GetHttpRequestHandler(bool is_monitor = false) const;
5555

56+
void StartMonitorPort();
5657
void Start();
5758

5859
void Stop();

core/src/components/component_context_impl.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,11 @@ void ComponentContextImpl::OnAllComponentsAreStopping() {
168168
}
169169

170170
void ComponentContextImpl::ClearComponents() {
171+
if (components_.empty()) {
172+
// Already cleared
173+
return;
174+
}
175+
171176
StopPrintAddingComponentsTask();
172177
const tracing::Span span(kClearComponentsRootName);
173178
OnAllComponentsAreStopping();
@@ -182,6 +187,7 @@ void ComponentContextImpl::ClearComponents() {
182187
false}
183188
);
184189

190+
components_.clear();
185191
LOG_INFO() << "Stopped all components";
186192
}
187193

@@ -197,6 +203,7 @@ void ComponentContextImpl::CancelComponentsLoad() {
197203
return;
198204
}
199205
for (auto& component_info : components_) {
206+
LOG_DEBUG() << "Call OnLoadingCancelled() for component '" << component_info->GetName() << "'";
200207
component_info->OnLoadingCancelled();
201208
}
202209
}

core/src/server/component.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ Server::Server(
4444
statistics_storage.RegisterWriter("http.handler.total", [this](utils::statistics::Writer& writer) {
4545
return server_->WriteTotalHandlerStatistics(writer);
4646
});
47+
48+
server_->StartMonitorPort();
4749
}
4850

4951
Server::~Server() {

core/src/server/handlers/http_handler_base.cpp

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <userver/server/http/http_request.hpp>
1212

1313
#include <userver/components/component.hpp>
14+
#include <userver/components/scope.hpp>
1415
#include <userver/components/statistics_storage.hpp>
1516
#include <userver/dynamic_config/storage/component.hpp>
1617
#include <userver/engine/deadline.hpp>
@@ -144,17 +145,26 @@ HttpHandlerBase::HttpHandlerBase(
144145
LOG_WARNING() << "empty allowed methods list in " << config.Name();
145146
}
146147

147-
auto& server_component = context.FindComponent<components::Server>();
148+
auto& server = context.FindComponent<components::Server>().GetServer();
148149

149150
engine::TaskProcessor& task_processor =
150151
GetConfig().task_processor
151152
? context.GetTaskProcessor(*GetConfig().task_processor)
152153
: engine::current_task::GetTaskProcessor();
153-
try {
154-
server_component.AddHandler(*this, task_processor);
155-
} catch (const std::exception& ex) {
156-
throw std::runtime_error(std::string("can't add handler to server: ") + ex.what());
157-
}
154+
155+
// Postpone handler registration as a request handling requires
156+
// HandleRequest() implementation, which is available only after
157+
// the descendant constructor.
158+
context.RegisterScope(components::MakeScope([this, &server, &task_processor] {
159+
try {
160+
server.AddHandler(*this, task_processor);
161+
162+
// Note: no need to call RemoveHandler on scope exit,
163+
// PortInfo::Stop() automatically removes all handlers
164+
} catch (const std::exception& ex) {
165+
throw std::runtime_error(std::string("can't add handler to server: ") + ex.what());
166+
}
167+
}));
158168

159169
BuildMiddlewarePipeline(config, context);
160170

@@ -188,9 +198,7 @@ HttpHandlerBase::HttpHandlerBase(
188198
}
189199

190200
set_response_server_hostname_ =
191-
GetConfig()
192-
.set_response_server_hostname.value_or(server_component.GetServer().GetConfig().set_response_server_hostname
193-
);
201+
GetConfig().set_response_server_hostname.value_or(server.GetConfig().set_response_server_hostname);
194202
}
195203

196204
HttpHandlerBase::~HttpHandlerBase() { statistics_holder_.Unregister(); }

core/src/server/server.cpp

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,8 @@ class ServerImpl final {
112112
);
113113
~ServerImpl();
114114

115-
void StartPortInfos();
115+
void StartPortInfo();
116+
void StartMonitorPortInfo();
116117
void Stop();
117118

118119
void AddHandler(const handlers::HttpHandlerBase& handler, engine::TaskProcessor& task_processor);
@@ -178,7 +179,7 @@ ServerImpl::ServerImpl(
178179

179180
ServerImpl::~ServerImpl() { Stop(); }
180181

181-
void ServerImpl::StartPortInfos() {
182+
void ServerImpl::StartPortInfo() {
182183
UASSERT(main_port_info_.request_handler);
183184

184185
if (has_requests_view_watchers_.load()) {
@@ -188,13 +189,15 @@ void ServerImpl::StartPortInfos() {
188189
main_port_info_.request_handler->SetNewRequestHook(hook);
189190
}
190191

191-
main_port_info_.Start();
192192
main_port_info_.request_handler->DisableAddHandler();
193+
main_port_info_.Start();
194+
193195
if (monitor_port_info_.request_handler) {
194196
monitor_port_info_.request_handler->DisableAddHandler();
195197
}
198+
}
196199

197-
// TODO: move to ctr for early start
200+
void ServerImpl::StartMonitorPortInfo() {
198201
if (monitor_port_info_.request_handler) {
199202
monitor_port_info_.Start();
200203
} else {
@@ -383,10 +386,17 @@ const http::HttpRequestHandler& Server::GetHttpRequestHandler(bool is_monitor) c
383386
return pimpl_->GetHttpRequestHandler(is_monitor);
384387
}
385388

389+
void Server::StartMonitorPort()
390+
{
391+
LOG_INFO() << "Starting monitor port";
392+
pimpl_->StartMonitorPortInfo();
393+
LOG_INFO() << "Monitor port is started";
394+
}
395+
386396
void Server::Start() {
387-
LOG_INFO() << "Starting server";
388-
pimpl_->StartPortInfos();
389-
LOG_INFO() << "Server is started";
397+
LOG_INFO() << "Starting server port";
398+
pimpl_->StartPortInfo();
399+
LOG_INFO() << "Server port is started";
390400
}
391401

392402
void Server::Stop() { pimpl_->Stop(); }

0 commit comments

Comments
 (0)