-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearch.go
More file actions
70 lines (59 loc) · 1.41 KB
/
search.go
File metadata and controls
70 lines (59 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package main
//based on Concurrency patterns (Google IO 2012)
import (
"fmt"
"math/rand"
"time"
)
var (
Web1 = fakeSearch("web1")
Web2 = fakeSearch("web2")
Web3 = fakeSearch("web3")
SQL = fakeSearch("sql")
SQL2 = fakeSearch("sql2")
XML = fakeSearch("xml")
XML2 = fakeSearch("xml2")
)
type Result string
type Finder func(query string) Result
func fakeSearch(kind string) Finder {
return func(query string) Result {
start := time.Now()
time.Sleep(time.Duration(rand.Intn(100)) * time.Millisecond)
timer := time.Since(start)
return Result(fmt.Sprintf("%s result for %q : %s\n", kind, query, timer))
}
}
func Search(query string) (results []Result) {
c := make(chan Result)
go func() { c <- First(query, Web1, Web2, Web3) }()
go func() { c <- First(query, SQL, SQL2) }()
go func() { c <- First(query, XML, XML2) }()
timeout := time.After(80 * time.Millisecond)
for i := 0; i < 3; i++ {
select {
case result := <-c:
results = append(results, result)
case <-timeout:
fmt.Println("timed out")
return
}
}
return
}
func First(query string, replicas ...Finder) Result {
c := make(chan Result)
searchReplica := func(i int) { c <- replicas[i](query) }
for i := range replicas {
go searchReplica(i)
}
return <-c
}
func main() {
rand.Seed(time.Now().UnixNano())
start := time.Now()
results := Search("golang")
elapsed := time.Since(start)
fmt.Println(results)
fmt.Println(elapsed)
}