-
Notifications
You must be signed in to change notification settings - Fork 5
/
barrier.go
69 lines (56 loc) · 1.02 KB
/
barrier.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
package main
import "fmt"
import "io/ioutil"
import "net/http"
import "time"
var timeout int = 5000
type Response struct {
Err error
Resp string
}
func barrier(urls ...string) {
numOfRequsts := len(urls)
in := make(chan Response, numOfRequsts)
defer close(in)
responses := make([]Response, numOfRequsts)
for _, uri := range urls {
go makeRequest(
in,
uri,
)
}
var hasError bool
for i := 0; i < numOfRequsts; i++ {
resp := <-in
if resp.Err != nil {
fmt.Println("ERROR: ", resp.Err)
hasError = true
}
responses[i] = resp
}
if !hasError {
for _, resp := range responses {
fmt.Println(resp.Resp)
}
}
}
func makeRequest(out chan<- Response, url string) {
res := Response{}
client := http.Client{
Timeout: time.Duration(time.Duration(timeout) * time.Millisecond),
}
resp, err := client.Get(url)
if err != nil {
res.Err = err
out <- res
return
}
byt, err := ioutil.ReadAll(resp.Body)
if err != nil {
res.Err = err
out <- res
return
}
res.Resp = string(byt)
out <- res
}