Skip to content

Commit c8e3daf

Browse files
authored
#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
1 parent 6dee5e9 commit c8e3daf

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

http/search.go

+17-1
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,12 @@
1515
package http
1616

1717
import (
18+
"context"
1819
"encoding/json"
1920
"fmt"
2021
"io"
2122
"net/http"
23+
"time"
2224

2325
"github.com/blevesearch/bleve/v2"
2426
"github.com/blevesearch/bleve/v2/search/query"
@@ -80,8 +82,22 @@ func (h *SearchHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
8082
}
8183
}
8284

85+
// check for timeout and create context
86+
var ctx context.Context
87+
timeoutStr := req.FormValue("timeout")
88+
if timeoutStr == "" {
89+
ctx = context.Background()
90+
} else {
91+
timeout, err := time.ParseDuration(timeoutStr)
92+
if err != nil {
93+
showError(w, req, fmt.Sprintf("error parsing timeout value: %v", err), 400)
94+
return
95+
}
96+
ctx, _ = context.WithTimeout(context.Background(), timeout)
97+
}
98+
8399
// execute the query
84-
searchResponse, err := index.Search(&searchRequest)
100+
searchResponse, err := index.SearchInContext(ctx, &searchRequest)
85101
if err != nil {
86102
showError(w, req, fmt.Sprintf("error executing query: %v", err), 500)
87103
return

0 commit comments

Comments
 (0)