-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathworker.go
53 lines (44 loc) · 933 Bytes
/
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
package main
type pool struct {
checks chan string
workers int
ending chan bool
MOTD chan string
}
func newPool(workers int) *pool {
p := &pool{
// Buffer the channel with 10x as many IPs as workers.
checks: make(chan string, workers*10),
ending: make(chan bool),
MOTD: make(chan string, workers*10),
workers: workers,
}
p.spawn()
return p
}
// Spawns out the number of workers in the pool.
func (p *pool) spawn() {
for i := 0; i < p.workers; i++ {
go p.work()
}
}
// Adds a new IP to be checked.
func (p *pool) add(ip string) {
p.checks <- ip
}
// Listens from and works on the checks.
func (p *pool) work() {
for ip := range p.checks {
if isMinecraft(ip) {
}
}
p.ending <- true
}
// Closes the pool. Should be sent when you're done adding IPs to
// be checked. Blocks until the pool is finished.
func (p *pool) end() {
close(p.checks)
for i := 0; i < p.workers; i++ {
<-p.ending
}
}