added "VisitedNodes" to message, to prevent re-requesting
This commit is contained in:
parent
d34ca730e4
commit
6b99213ec4
7 changed files with 120 additions and 76 deletions
26
text.go
26
text.go
|
@ -149,7 +149,7 @@ func fetchTextResults(query, safe, lang string, page int) []TextSearchResult {
|
|||
// If no results found after trying all engines
|
||||
if len(results) == 0 {
|
||||
log.Printf("No text results found for query: %s, trying other nodes", query)
|
||||
results = tryOtherNodesForTextSearch(query, safe, lang, page)
|
||||
results = tryOtherNodesForTextSearch(query, safe, lang, page, []string{hostID})
|
||||
}
|
||||
|
||||
return results
|
||||
|
@ -183,9 +183,12 @@ func wrapTextSearchFunc(f func(string, string, string, int) ([]TextSearchResult,
|
|||
}
|
||||
}
|
||||
|
||||
func tryOtherNodesForTextSearch(query, safe, lang string, page int) []TextSearchResult {
|
||||
func tryOtherNodesForTextSearch(query, safe, lang string, page int, visitedNodes []string) []TextSearchResult {
|
||||
for _, nodeAddr := range peers {
|
||||
results, err := sendTextSearchRequestToNode(nodeAddr, query, safe, lang, page)
|
||||
if contains(visitedNodes, nodeAddr) {
|
||||
continue // Skip nodes already visited
|
||||
}
|
||||
results, err := sendTextSearchRequestToNode(nodeAddr, query, safe, lang, page, visitedNodes)
|
||||
if err != nil {
|
||||
log.Printf("Error contacting node %s: %v", nodeAddr, err)
|
||||
continue
|
||||
|
@ -197,19 +200,22 @@ func tryOtherNodesForTextSearch(query, safe, lang string, page int) []TextSearch
|
|||
return nil
|
||||
}
|
||||
|
||||
func sendTextSearchRequestToNode(nodeAddr, query, safe, lang string, page int) ([]TextSearchResult, error) {
|
||||
func sendTextSearchRequestToNode(nodeAddr, query, safe, lang string, page int, visitedNodes []string) ([]TextSearchResult, error) {
|
||||
visitedNodes = append(visitedNodes, nodeAddr)
|
||||
searchParams := struct {
|
||||
Query string `json:"query"`
|
||||
Safe string `json:"safe"`
|
||||
Lang string `json:"lang"`
|
||||
Page int `json:"page"`
|
||||
ResponseAddr string `json:"responseAddr"`
|
||||
Query string `json:"query"`
|
||||
Safe string `json:"safe"`
|
||||
Lang string `json:"lang"`
|
||||
Page int `json:"page"`
|
||||
ResponseAddr string `json:"responseAddr"`
|
||||
VisitedNodes []string `json:"visitedNodes"`
|
||||
}{
|
||||
Query: query,
|
||||
Safe: safe,
|
||||
Lang: lang,
|
||||
Page: page,
|
||||
ResponseAddr: fmt.Sprintf("http://localhost:%d/node", config.Port),
|
||||
VisitedNodes: visitedNodes,
|
||||
}
|
||||
|
||||
msgBytes, err := json.Marshal(searchParams)
|
||||
|
@ -232,7 +238,7 @@ func sendTextSearchRequestToNode(nodeAddr, query, safe, lang string, page int) (
|
|||
select {
|
||||
case res := <-textResultsChan:
|
||||
return res, nil
|
||||
case <-time.After(20 * time.Second): // Increased timeout duration
|
||||
case <-time.After(20 * time.Second):
|
||||
return nil, fmt.Errorf("timeout waiting for results from node %s", nodeAddr)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue