From 776d76d299748a79b9cb25593cd8266cb26a6553 Mon Sep 17 00:00:00 2001 From: Dan Engelbrecht Date: Fri, 27 Mar 2026 11:13:02 +0100 Subject: idle deprovision in hub (#895) - Feature: Hub watchdog automatically deprovisions inactive provisioned and hibernated instances - Feature: Added `stats/activity_counters` endpoint to measure server activity - Feature: Added configuration options for hub watchdog - `--hub-watchdog-provisioned-inactivity-timeout-seconds` Inactivity timeout before a provisioned instance is deprovisioned - `--hub-watchdog-hibernated-inactivity-timeout-seconds` Inactivity timeout before a hibernated instance is deprovisioned - `--hub-watchdog-inactivity-check-margin-seconds` Margin before timeout at which an activity check is issued - `--hub-watchdog-cycle-interval-ms` Watchdog poll interval in milliseconds - `--hub-watchdog-cycle-processing-budget-ms` Maximum time budget per watchdog cycle in milliseconds - `--hub-watchdog-instance-check-throttle-ms` Minimum delay between checks on a single instance - `--hub-watchdog-activity-check-connect-timeout-ms` Connect timeout for activity check requests - `--hub-watchdog-activity-check-request-timeout-ms` Request timeout for activity check requests --- src/zenhttp/httpserver.cpp | 97 ++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 90 insertions(+), 7 deletions(-) (limited to 'src/zenhttp/httpserver.cpp') diff --git a/src/zenhttp/httpserver.cpp b/src/zenhttp/httpserver.cpp index a46c5b851..9fc42f18c 100644 --- a/src/zenhttp/httpserver.cpp +++ b/src/zenhttp/httpserver.cpp @@ -798,7 +798,18 @@ HttpRequestRouter::HandleRequest(zen::HttpServerRequest& Request) const HttpVerb Verb = Request.RequestVerb(); - std::string_view Uri = Request.RelativeUri(); + std::string_view Uri = Request.RelativeUri(); + + // Strip the separator slash left over after the service prefix is removed. + // When a service has BaseUri "/foo", the prefix length is set to len("/foo") = 4. + // Stripping 4 chars from "/foo/bar" yields "/bar" — the path separator becomes + // the first character of the relative URI. Remove it so patterns like "bar" or + // "{id}" match without needing to account for the leading slash. + if (!Uri.empty() && Uri.front() == '/') + { + Uri.remove_prefix(1); + } + HttpRouterRequest RouterRequest(Request); for (const MatcherEndpoint& Handler : m_MatcherEndpoints) @@ -974,6 +985,12 @@ HttpServer::SetHttpRequestFilter(IHttpRequestFilter* RequestFilter) OnSetHttpRequestFilter(RequestFilter); } +void +HttpServer::HandleStatsRequest(HttpServerRequest& Request) +{ + Request.WriteResponse(HttpResponseCode::OK, CollectStats()); +} + CbObject HttpServer::CollectStats() { @@ -1004,12 +1021,6 @@ HttpServer::CollectStats() return Cbo.Save(); } -void -HttpServer::HandleStatsRequest(HttpServerRequest& Request) -{ - Request.WriteResponse(HttpResponseCode::OK, CollectStats()); -} - ////////////////////////////////////////////////////////////////////////// HttpRpcHandler::HttpRpcHandler() @@ -1446,6 +1457,78 @@ TEST_CASE("http.common") } } + SUBCASE("router-leading-slash") + { + // Verify that HandleRequest strips the leading slash that server implementations + // leave in RelativeUri() when the service base URI has no trailing slash. + // e.g. BaseUri "/stats" + prefix-strip of "/stats/foo" yields "/foo", not "foo". + + bool HandledLiteral = false; + bool HandledPattern = false; + bool HandledTwoSeg = false; + std::vector Captures; + auto Reset = [&] { + HandledLiteral = HandledPattern = HandledTwoSeg = false; + Captures.clear(); + }; + + TestHttpService Service; + HttpRequestRouter r; + + r.AddMatcher("seg", [](std::string_view In) -> bool { return !In.empty() && In.find('/') == std::string_view::npos; }); + + r.RegisterRoute( + "activity_counters", + [&](auto& /*Req*/) { HandledLiteral = true; }, + HttpVerb::kGet); + + r.RegisterRoute( + "{seg}", + [&](auto& Req) { + HandledPattern = true; + Captures = {std::string(Req.GetCapture(1))}; + }, + HttpVerb::kGet); + + r.RegisterRoute( + "prefix/{seg}", + [&](auto& Req) { + HandledTwoSeg = true; + Captures = {std::string(Req.GetCapture(1))}; + }, + HttpVerb::kGet); + + // Single-segment literal with leading slash — simulates real server RelativeUri + { + Reset(); + TestHttpServerRequest req{Service, "/activity_counters"sv}; + r.HandleRequest(req); + CHECK(HandledLiteral); + CHECK(!HandledPattern); + } + + // Single-segment pattern with leading slash + { + Reset(); + TestHttpServerRequest req{Service, "/hello"sv}; + r.HandleRequest(req); + CHECK(!HandledLiteral); + CHECK(HandledPattern); + REQUIRE_EQ(Captures.size(), 1); + CHECK_EQ(Captures[0], "hello"sv); + } + + // Two-segment route with leading slash — first literal segment + { + Reset(); + TestHttpServerRequest req{Service, "/prefix/world"sv}; + r.HandleRequest(req); + CHECK(HandledTwoSeg); + REQUIRE_EQ(Captures.size(), 1); + CHECK_EQ(Captures[0], "world"sv); + } + } + SUBCASE("content-type") { for (uint8_t i = 0; i < uint8_t(HttpContentType::kCOUNT); ++i) -- cgit v1.2.3