-
Notifications
You must be signed in to change notification settings - Fork 5
/
17_googleSearch.go
184 lines (160 loc) · 3.95 KB
/
17_googleSearch.go
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package main
import (
"fmt"
"math/rand"
"reflect"
"runtime"
"time"
)
//////////////////////////////////////////////
/// aliases
//////////////////////////////////////////////
var println = fmt.Println
var sprintf = fmt.Sprintf
var printf = fmt.Printf
//////////////////////////////////////////////
/// init
//////////////////////////////////////////////
func init() {
s := time.Now().UTC().UnixNano()
rand.Seed(s)
}
//////////////////////////////////////////////
/// search
//////////////////////////////////////////////
type Result string
type Search func(query string) Result
func fakeSearch(kind string) Search {
return func(query string) Result {
time.Sleep(time.Duration(rand.Intn(100)) * time.Millisecond)
msg := sprintf("%s result for %q\n", kind, query)
return Result(msg)
}
}
func First(query string, replicas ...Search) Result {
c := make(chan Result)
searchReplica := func(i int) { c <- replicas[i](query) }
for i := range replicas {
go searchReplica(i)
}
return <-c
}
//////////////////////////////////////////////
/// Google
//////////////////////////////////////////////
type googleFunc func(query string) (results []Result)
var (
Web = fakeSearch("web")
Image = fakeSearch("image")
Video = fakeSearch("video")
)
func Google1(query string) (results []Result) {
results = append(results, Web(query))
results = append(results, Image(query))
results = append(results, Video(query))
return
}
func Google2(query string) (results []Result) {
c := make(chan Result)
go func() { c <- Web(query) }()
go func() { c <- Image(query) }()
go func() { c <- Video(query) }()
for i := 0; i < 3; i++ {
result := <-c
results = append(results, result)
}
return
}
func Google2_1(query string) (results []Result) {
c := make(chan Result)
go func() { c <- Web(query) }()
go func() { c <- Image(query) }()
go func() { c <- Video(query) }()
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
}
var (
Web1 = fakeSearch("web1")
Web2 = fakeSearch("web2")
Image1 = fakeSearch("image1")
Image2 = fakeSearch("image2")
Video1 = fakeSearch("video1")
Video2 = fakeSearch("video2")
)
func Google3(query string) (results []Result) {
c := make(chan Result)
go func() { c <- First(query, Web1, Web2) }()
go func() { c <- First(query, Image1, Image2) }()
go func() { c <- First(query, Video1, Video2) }()
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
}
// more replicas!
func Google3_5(query string) (results []Result) {
c := make(chan Result)
webs, images, videos := []Search{}, []Search{}, []Search{}
for i := 0; i < 10; i++ {
webs = append(webs, fakeSearch(sprintf("web%v", i)))
images = append(images, fakeSearch(sprintf("image%v", i)))
videos = append(videos, fakeSearch(sprintf("videos%v", i)))
}
go func() { c <- First(query, webs...) }()
go func() { c <- First(query, images...) }()
go func() { c <- First(query, videos...) }()
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
}
//////////////////////////////////////////////
/// runGoogle
//////////////////////////////////////////////
func GetFunctionName(i interface{}) string {
return runtime.FuncForPC(reflect.ValueOf(i).Pointer()).Name()
}
func runGoogle(Google googleFunc) {
fname := GetFunctionName(Google)
printf("\n== %v ==\n", fname)
start := time.Now()
results := Google("golang")
elapsed := time.Since(start)
println(results)
println(elapsed)
}
func main() {
googles := []googleFunc{
Google1,
Google2,
Google2_1,
Google3,
Google3_5,
}
for _, g := range googles {
runGoogle(g)
}
}