Skip to content

Commit

Permalink
#1873: Added timeout option in the Search Handler (#1898)
Browse files Browse the repository at this point in the history
- Added timeout value, which the user can pass through request
parameters
 - Create context with the timeout value if present
 - Change the call to SearchInContext
  • Loading branch information
Likith101 committed Nov 16, 2023
1 parent 6dee5e9 commit c8e3daf
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion http/search.go
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit c8e3daf

Please sign in to comment.