From 977c43b88fce0bf5c67d3c10e9b05824f1d2f799 Mon Sep 17 00:00:00 2001 From: Roman Khavronenko Date: Wed, 3 May 2023 10:42:17 +0200 Subject: [PATCH] vmselect: exit early from queue on context cancel (#4223) * vmselect: exit early from queue on context cancel When `-search.maxConcurrentRequests` is reached, vmselect puts request in the queue. It is expected, that requests in the queue will be processed as soon as it would be enough capacity to do so. However, it could happen that while request was waiting its turn, the client could have already cancel it (close the connection, or just close the tab with UI). In this case, we should de-queue such requests to avoid spending extra resources on them. Signed-off-by: hagen1778 * app/vmselect: address review comments Signed-off-by: hagen1778 --------- Signed-off-by: hagen1778 --- app/vmselect/main.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/app/vmselect/main.go b/app/vmselect/main.go index e7656ecfd..956a29789 100644 --- a/app/vmselect/main.go +++ b/app/vmselect/main.go @@ -209,6 +209,13 @@ func requestHandler(w http.ResponseWriter, r *http.Request) bool { timerpool.Put(t) qt.Printf("wait in queue because -search.maxConcurrentRequests=%d concurrent requests are executed", *maxConcurrentRequests) defer func() { <-concurrencyLimitCh }() + case <-r.Context().Done(): + timerpool.Put(t) + remoteAddr := httpserver.GetQuotedRemoteAddr(r) + requestURI := httpserver.GetRequestURI(r) + logger.Infof("client has cancelled the request after %.3f seconds: remoteAddr=%s, requestURI: %q", + d.Seconds(), remoteAddr, requestURI) + return true case <-t.C: timerpool.Put(t) concurrencyLimitTimeout.Inc()