trim search suggestions if there is too many
This commit is contained in:
parent
9e81b484fb
commit
7e1b946bcc
1 changed files with 11 additions and 7 deletions
|
@ -99,6 +99,9 @@ func handleSuggestions(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
}
|
||||
|
||||
// Trim the suggestions to a maximum of 8 items
|
||||
suggestions = trimSuggestions(suggestions)
|
||||
|
||||
if len(suggestions) == 0 {
|
||||
printErr("All suggestion services failed. Returning empty response.")
|
||||
}
|
||||
|
@ -108,6 +111,14 @@ func handleSuggestions(w http.ResponseWriter, r *http.Request) {
|
|||
fmt.Fprintf(w, `["",%s]`, toJSONStringArray(suggestions))
|
||||
}
|
||||
|
||||
// trimSuggestions trims the suggestion list to a maximum of 8 suggestions.
|
||||
func trimSuggestions(suggestions []string) []string {
|
||||
if len(suggestions) > 8 {
|
||||
return suggestions[:8]
|
||||
}
|
||||
return suggestions
|
||||
}
|
||||
|
||||
// updateLatency updates the latency of a suggestion source using an exponential moving average.
|
||||
func updateLatency(source *SuggestionSource, newLatency time.Duration) {
|
||||
source.mu.Lock()
|
||||
|
@ -180,13 +191,6 @@ func fetchYahooSuggestions(query string) []string {
|
|||
// return fetchSuggestionsFromURL(url)
|
||||
// }
|
||||
|
||||
// func fetchSogouSuggestions(query string) []string {
|
||||
// encodedQuery := url.QueryEscape(query)
|
||||
// url := fmt.Sprintf("https://w.sugg.sogou.com/sugg/ajaj_json.jsp?key=%s", encodedQuery)
|
||||
// printDebug("Fetching suggestions from Sogou: %s", url)
|
||||
// return fetchSuggestionsFromURL(url)
|
||||
// }
|
||||
|
||||
func fetchSuggestionsFromURL(url string) []string {
|
||||
resp, err := http.Get(url)
|
||||
if err != nil {
|
||||
|
|
Loading…
Add table
Reference in a new issue