From c8e3daf115aee56897e841aee9ddc9dfa78ba5d6 Mon Sep 17 00:00:00 2001 From: Likith B <62029862+Likith101@users.noreply.github.com> Date: Thu, 16 Nov 2023 20:04:32 +0530 Subject: [PATCH] #1873: Added timeout option in the Search Handler (#1898) - Added timeout value, which the user can pass through request parameters - Create context with the timeout value if present - Change the call to SearchInContext --- http/search.go | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/http/search.go b/http/search.go index 186d3d2c6..37a33f031 100644 --- a/http/search.go +++ b/http/search.go @@ -15,10 +15,12 @@ package http import ( + "context" "encoding/json" "fmt" "io" "net/http" + "time" "github.com/blevesearch/bleve/v2" "github.com/blevesearch/bleve/v2/search/query" @@ -80,8 +82,22 @@ func (h *SearchHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) { } } + // check for timeout and create context + var ctx context.Context + timeoutStr := req.FormValue("timeout") + if timeoutStr == "" { + ctx = context.Background() + } else { + timeout, err := time.ParseDuration(timeoutStr) + if err != nil { + showError(w, req, fmt.Sprintf("error parsing timeout value: %v", err), 400) + return + } + ctx, _ = context.WithTimeout(context.Background(), timeout) + } + // execute the query - searchResponse, err := index.Search(&searchRequest) + searchResponse, err := index.SearchInContext(ctx, &searchRequest) if err != nil { showError(w, req, fmt.Sprintf("error executing query: %v", err), 500) return