forked from henson/proxypool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
75 lines (67 loc) · 1.25 KB
/
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package main
import (
"log"
"runtime"
"sync"
"time"
"github.com/henson/ProxyPool/api"
"github.com/henson/ProxyPool/getter"
"github.com/henson/ProxyPool/models"
"github.com/henson/ProxyPool/storage"
)
func main() {
runtime.GOMAXPROCS(runtime.NumCPU())
ipChan := make(chan *models.IP, 2000)
conn := storage.NewStorage()
// Start HTTP
go func() {
api.Run()
}()
// Check the IPs in DB
go func() {
storage.CheckProxyDB()
}()
// Check the IPs in channel
for i := 0; i < 50; i++ {
go func() {
for {
storage.CheckProxy(<-ipChan)
}
}()
}
// Start getters to scraper IP and put it in channel
for {
x := conn.Count()
log.Printf("Chan: %v, IP: %v\n", len(ipChan), x)
if len(ipChan) < 100 {
go run(ipChan)
}
time.Sleep(10 * time.Minute)
}
}
func run(ipChan chan<- *models.IP) {
var wg sync.WaitGroup
funs := []func() []*models.IP{
getter.Data5u,
getter.IP66,
getter.KDL,
getter.GBJ,
getter.Xici,
getter.XDL,
getter.IP181,
//getter.YDL, //失效的采集脚本,用作系统容错实验
getter.PLP,
}
for _, f := range funs {
wg.Add(1)
go func(f func() []*models.IP) {
temp := f()
for _, v := range temp {
ipChan <- v
}
wg.Done()
}(f)
}
wg.Wait()
log.Println("All getters finished.")
}