-
Notifications
You must be signed in to change notification settings - Fork 0
/
worker.go
61 lines (56 loc) · 1.07 KB
/
worker.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
package main
import (
"net/http"
"time"
)
func makeWorker(stop chan bool) (chan Data, chan Progress) {
ch := make(chan Data, 1024)
progress := make(chan Progress, 1)
for i := 0; i < 1024; i++ {
go worker(ch, progress, stop)
}
return ch, progress
}
func worker(ch chan Data, pro chan Progress, stop chan bool) {
var client = &http.Client{
Timeout: 10 * time.Second,
}
var st = false
go func() {
st = <-stop
}()
for {
if !st {
task := <-ch
valid := isValid(client, task.url)
if valid {
pro <- Progress{typ: 1}
} else {
pro <- Progress{typ: 2}
}
go write(task, valid)
} else {
break
}
}
}
func isValid(client *http.Client, link string) bool {
url := "http://" + link
_, err := client.Get(url)
if err != nil {
//if strings.Contains(err.Error(), "forcibly") {
// //println(link, "=>", "[X]", err.Error())
// return isValid(client, link)
//}
return false
}
//println(link, "=>", "[√]")
return true
}
func write(task Data, valid bool) {
var v = "0"
if valid {
v = "1"
}
writer(task.url + "," + task.typ + "," + v)
}