-
Notifications
You must be signed in to change notification settings - Fork 0
/
writer.go
61 lines (51 loc) · 1.51 KB
/
writer.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
package uconn
import (
"fmt"
"sync"
"github.com/activecm/rita/config"
"github.com/activecm/rita/database"
)
type (
//writer blah blah
writer struct { //structure for writing blacklist results to mongo
targetCollection string
db *database.DB // provides access to MongoDB
conf *config.Config // contains details needed to access MongoDB
writeChannel chan update // holds analyzed data
writeWg sync.WaitGroup // wait for writing to finish
}
)
//newWriter creates a new writer object to write output data to blacklisted collections
func newWriter(targetCollection string, db *database.DB, conf *config.Config) *writer {
return &writer{
targetCollection: targetCollection,
db: db,
conf: conf,
writeChannel: make(chan update),
}
}
//collect sends a group of results to the writer for writing out to the database
func (w *writer) collect(data update) {
w.writeChannel <- data
}
//close waits for the write threads to finish
func (w *writer) close() {
close(w.writeChannel)
w.writeWg.Wait()
}
//start kicks off a new write thread
func (w *writer) start() {
w.writeWg.Add(1)
go func() {
ssn := w.db.Session.Copy()
defer ssn.Close()
for data := range w.writeChannel {
info, err := ssn.DB(w.db.GetSelectedDB()).C(w.targetCollection).Upsert(data.selector, data.query)
if err != nil ||
((info.Updated == 0) && (info.UpsertedId == nil)) {
fmt.Println("uconns module: ", err, info, data)
}
}
w.writeWg.Done()
}()
}