-
Notifications
You must be signed in to change notification settings - Fork 0
/
temp_update_empty.go
77 lines (64 loc) · 1.38 KB
/
temp_update_empty.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
package main
import (
"fmt"
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
"net"
"time"
)
func worker(id int, jobs <-chan string, results chan<- IPAddr) {
for j := range jobs {
fmt.Println("worker", id, "on ip", j)
name := "null"
names, err := net.LookupAddr(j)
if err != nil {
name = "null"
} else {
name = names[0]
}
results <- IPAddr{Address: j, Reverse: name}
time.Sleep(100 * time.Millisecond)
}
}
func UpdateDocReverse(c *mgo.Collection, addr string, reverse string) {
res := IPAddr{}
err := c.Find(bson.M{"address": addr}).One(&res)
if err != nil {
fmt.Println(err.Error())
}
res.Reverse = reverse
err = c.Update(IPAddr{Address: res.Address}, res)
if err != nil {
fmt.Println(err)
}
}
func main() {
s, c, _ := Collection("192.168.1.25")
defer s.Close()
var ips []IPAddr
_ = c.Find(bson.M{"reverse": ""}).All(&ips)
fmt.Println(len(ips))
jobs := make(chan string)
results := make(chan IPAddr)
for w := 1; w <= 50; w++ {
go worker(w, jobs, results)
}
go func() {
for result := range results {
UpdateDocReverse(c, result.Address, result.Reverse)
fmt.Println("updated", result.Address, "put", result.Reverse)
}
}()
go func() {
for _, ip := range ips {
jobs <- ip.Address
fmt.Println("Added", ip.Address, "to jobs")
}
}()
fmt.Println("Loop ended")
fmt.Println(len(jobs))
go func() {
time.Sleep(time.Second)
}()
select {}
}