This repository has been archived by the owner on Jan 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathworker.go
90 lines (85 loc) · 1.62 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
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
package main
import (
"bufio"
"go-proxy-checker/core"
"io"
"log"
"sync"
)
var workerWait = &sync.WaitGroup{}
var inProxyChan = make(chan string, 1000)
var outProxyChan = make(chan string, 1000)
var resultChan = make(chan CheckResult, 1000)
type CheckResult struct {
proxy string
result bool
}
func startCheck() {
go inputProxiesFromFile()
// start workers
for i := 0; i < currency; i++ {
workerWait.Add(1)
go worker()
}
go writeResultToFile()
go printProcess()
// wait works done
workerWait.Wait()
close(outProxyChan)
close(resultChan)
}
func inputProxiesFromFile() {
inputReader := bufio.NewReader(InputFile)
defer func() {
close(inProxyChan)
_ = InputFile.Close()
}()
for {
line, _, err := inputReader.ReadLine()
if err != nil {
if err == io.EOF {
break
}
}
inProxyChan <- string(line)
}
}
func writeResultToFile() {
defer func() {
_ = OutputFile.Close()
wg.Done()
}()
for proxy := range outProxyChan {
_, err := OutputFile.WriteString(proxy + "\n")
if err != nil {
panic(err)
}
}
}
func printProcess() {
defer wg.Done()
var success int
var count int
for r := range resultChan {
count += 1
if r.result {
success += 1
log.Printf("[now: %d / total: %d / success:%d ] proxy %s is vaild\n", count, total, success, r.proxy)
} else {
log.Printf("[now: %d / total: %d / success:%d ] proxy %s is invalid\n", count, total, success, r.proxy)
}
}
}
func worker() {
defer workerWait.Done()
for proxy := range inProxyChan {
result := core.CheckProxy(proxy)
if result {
outProxyChan <- proxy
}
resultChan <- CheckResult{
proxy: proxy,
result: result,
}
}
}