-
Notifications
You must be signed in to change notification settings - Fork 1
/
postgres.go
60 lines (50 loc) · 1.17 KB
/
postgres.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
package main
import (
"database/sql"
"fmt"
_ "github.com/lib/pq"
cdns "github.com/niclabs/dnszeppelin"
"log"
"sync"
"time"
)
const (
host = "localhost"
dbport = 5432
)
func PGCollect(resultChannel chan cdns.DNSResult, exiting chan bool, wg *sync.WaitGroup, wsize, batchSize uint, dbname, user, pass string){
wg.Add(1)
defer wg.Done()
// Connect to DB
psqlconn := fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=disable", host, dbport, user , pass, dbname)
db, err := sql.Open("postgres", psqlconn)
if err != nil {
log.Fatal("Error DB:",err)
}
defer db.Close()
// check db
err = db.Ping()
if err != nil {
log.Fatal("Error DB:",err)
}
batch := make([]cdns.DNSResult, 0, batchSize)
ticker := time.NewTicker(time.Duration(wsize) * time.Second)
defer ticker.Stop()
for {
select {
case data := <-resultChannel:
batch = append(batch, data)
case <-ticker.C:
if err := PGWrite(db,batch,300); err != nil { // this is Sparta!
log.Fatal("Error writing to DB:", err)
exiting <- true
return
} else {
batch = make([]cdns.DNSResult, 0, batchSize)
}
case <-exiting:
exiting <- true
return
}
}
}