-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmain.go
51 lines (41 loc) · 832 Bytes
/
main.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
package main
import (
"flag"
"fmt"
"net/http"
"strings"
"sync"
)
var workers int
var endpoints string
var urlsDone int
var urlsError int
func main() {
flag.IntVar(&workers, "workers", 5, "number of workers")
flag.StringVar(&endpoints, "endpoints", "", "list of endpoints comma delimited")
wg := sync.WaitGroup{}
tasks := make(chan string)
for i := 0; i < workers; i++ {
go worker(wg, tasks)
}
for _, endpoint := range strings.Split(endpoints, ",") {
tasks <- endpoint
}
wg.Wait()
close(tasks)
fmt.Printf("work done. successfully=%d errors=%d", urlsDone, urlsError)
}
func worker(wg sync.WaitGroup, urls chan string) error {
wg.Add(1)
defer wg.Done()
for msg := range urls {
_, err := http.Get(msg)
if err != nil {
urlsError += 1
return err
} else {
urlsDone += 1
}
}
return nil
}