-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbulkLoader.go
75 lines (64 loc) · 1.6 KB
/
bulkLoader.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 (
"bufio"
"fmt"
"os"
"regexp"
"runtime"
"strconv"
"sync"
"time"
)
var splitter = regexp.MustCompile("[\r\n, ]+")
func bulkWorker(domainChan chan string, wg *sync.WaitGroup) {
defer wg.Done()
for domain := range domainChan {
lookupBimi(domain)
}
}
func bulkLoader(filenames []string) {
domainChan := make(chan string, 50*1024*1024)
wg := &sync.WaitGroup{}
maxWorkers, maxErr := strconv.Atoi(os.Getenv("MAXWORKERS"))
if maxErr != nil {
maxWorkers = 10 * runtime.NumCPU()
}
total := 0
for _, filename := range filenames {
fmt.Fprintf(os.Stderr, "INFO: loading %s\n", filename)
count := 0
file, openErr := os.Open(filename)
if openErr != nil {
fmt.Fprintf(os.Stderr, "ERROR: unable to open file %s: %v", filename, openErr)
continue
}
defer file.Close()
scanner := bufio.NewScanner(bufio.NewReaderSize(file, 100*1024))
for scanner.Scan() {
domainChan <- scanner.Text()
count++
if count%1000 == 0 {
fmt.Fprintf(os.Stderr, ".")
}
}
fmt.Fprintf(os.Stderr, "\nINFO: %d loaded from %s\n", count, filename)
total += count
}
fmt.Fprintf(os.Stderr, "INFO: %d total domains\n", total)
for i := 0; i < maxWorkers; i++ {
wg.Add(1)
go bulkWorker(domainChan, wg)
}
close(domainChan)
previousLen := len(domainChan)
for len(domainChan) > 500 {
if previousLen-len(domainChan) > 1000 {
fmt.Fprintf(os.Stderr, "\nINFO: Domains remaining: %d..", len(domainChan))
previousLen = len(domainChan)
}
fmt.Fprintf(os.Stderr, ".")
time.Sleep(100 * time.Millisecond)
}
fmt.Fprintf(os.Stderr, "\nINFO: Total domains: %d\n", total)
wg.Wait()
}