Submit uses goroutines and channels to perform a search against the three leading search engines concurrently.
(query string, options ...func(*searchSession))
| 55 | // Submit uses goroutines and channels to perform a search against the three |
| 56 | // leading search engines concurrently. |
| 57 | func Submit(query string, options ...func(*searchSession)) []Result { |
| 58 | var session searchSession |
| 59 | session.searchers = make(map[string]Searcher) |
| 60 | session.resultChan = make(chan []Result) |
| 61 | |
| 62 | for _, opt := range options { |
| 63 | opt(&session) |
| 64 | } |
| 65 | |
| 66 | // Perform the searches concurrently. Using a map because |
| 67 | // it returns the searchers in a random order every time. |
| 68 | for _, s := range session.searchers { |
| 69 | go s.Search(query, session.resultChan) |
| 70 | } |
| 71 | |
| 72 | var results []Result |
| 73 | |
| 74 | // Wait for the results to come back. |
| 75 | for search := 0; search < len(session.searchers); search++ { |
| 76 | // If we just want the first result, don't wait any longer by |
| 77 | // concurrently discarding the remaining searchResults. |
| 78 | // Failing to do so will leave the Searchers blocked forever. |
| 79 | if session.first && search > 0 { |
| 80 | go func() { |
| 81 | r := <-session.resultChan |
| 82 | log.Printf("search : Submit : Info : Results Discarded : Results[%d]\n", len(r)) |
| 83 | }() |
| 84 | continue |
| 85 | } |
| 86 | |
| 87 | // Wait to recieve results. |
| 88 | log.Println("search : Submit : Info : Waiting For Results...") |
| 89 | result := <-session.resultChan |
| 90 | |
| 91 | // Save the results to the final slice. |
| 92 | log.Printf("search : Submit : Info : Results Used : Results[%d]\n", len(result)) |
| 93 | results = append(results, result...) |
| 94 | } |
| 95 | |
| 96 | log.Printf("search : Submit : Completed : Found [%d] Results\n", len(results)) |
| 97 | return results |
| 98 | } |