From c9e43e1ad5ea3f41e1f7df876460ede167d76351 Mon Sep 17 00:00:00 2001 From: Stefan Boberg Date: Wed, 27 Sep 2023 16:25:15 +0200 Subject: prefer to handle cache RPC requests synchronously (#428) * only handle RPC requests in a worker thread if we have an upstream. we may as well handle the request inline on the http_io thread if we're only dealing with local data since the response times should be pretty consistent in that case * http.sys: don't create async worker thread pool until it's needed (typically only if we have an upstream) --- src/zenserver/cache/httpstructuredcache.cpp | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) (limited to 'src/zenserver/cache/httpstructuredcache.cpp') diff --git a/src/zenserver/cache/httpstructuredcache.cpp b/src/zenserver/cache/httpstructuredcache.cpp index 11ac81dcb..4a2fdd96b 100644 --- a/src/zenserver/cache/httpstructuredcache.cpp +++ b/src/zenserver/cache/httpstructuredcache.cpp @@ -1729,6 +1729,9 @@ void HttpStructuredCacheService::HandleRpcRequest(HttpServerRequest& Request) { ZEN_TRACE_CPU("z$::Http::HandleRpcRequest"); + + const bool HasUpstream = m_UpstreamCache.IsActive(); + switch (Request.RequestVerb()) { case HttpVerb::kPost: @@ -1745,7 +1748,7 @@ HttpStructuredCacheService::HandleRpcRequest(HttpServerRequest& Request) return Request.WriteResponse(HttpResponseCode::BadRequest); } - Request.WriteResponseAsync( + auto HandleRpc = [this, RequestContext, Body = Request.ReadPayload(), ContentType, AcceptType](HttpServerRequest& AsyncRequest) mutable { ZEN_TRACE_CPU("z$::Http::HandleRpcRequest::WriteResponseAsync"); std::uint64_t RequestIndex = @@ -1764,9 +1767,9 @@ HttpStructuredCacheService::HandleRpcRequest(HttpServerRequest& Request) RpcResult); if (!IsHttpSuccessCode(ResultCode)) { - AsyncRequest.WriteResponse(ResultCode); - return; + return AsyncRequest.WriteResponse(ResultCode); } + if (AcceptMagic == kCbPkgMagic) { void* TargetProcessHandle = nullptr; @@ -1804,9 +1807,19 @@ HttpStructuredCacheService::HandleRpcRequest(HttpServerRequest& Request) HttpContentType::kCbPackage, IoBuffer(IoBuffer::Wrap, MemStream.GetData(), MemStream.GetSize())); } - }); + }; + + if (HasUpstream) + { + Request.WriteResponseAsync(std::move(HandleRpc)); + } + else + { + HandleRpc(Request); + } } break; + default: m_CacheStats.BadRequestCount++; Request.WriteResponse(HttpResponseCode::BadRequest); -- cgit v1.2.3