How do we avoid discarding result from the slow server. We duplicates to many instance, and perform parallel request.
(query string, replicas ...Search)
| 28 | // How do we avoid discarding result from the slow server. |
| 29 | // We duplicates to many instance, and perform parallel request. |
| 30 | func First(query string, replicas ...Search) Result { |
| 31 | c := make(chan Result) |
| 32 | for i := range replicas { |
| 33 | go func(idx int) { |
| 34 | c <- replicas[idx](query) |
| 35 | }(i) |
| 36 | } |
| 37 | // the magic is here. First function always waits for 1 time after receiving the result |
| 38 | return <-c |
| 39 | } |
| 40 | |
| 41 | // I don't want to wait for slow server |
| 42 | func Google(query string) []Result { |