diff --git a/suggestions.go b/suggestions.go index 88095fd..089ca8b 100644 --- a/suggestions.go +++ b/suggestions.go @@ -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 {