-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathfound_block_async.go
More file actions
50 lines (43 loc) · 1.02 KB
/
found_block_async.go
File metadata and controls
50 lines (43 loc) · 1.02 KB
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
package main
import (
"strings"
"sync"
"time"
)
// foundBlockLogEntry represents a single JSONL line to append to the
// found_blocks.jsonl log. Writes are serialized by a background goroutine
// so the submit hot path never blocks on filesystem I/O.
type foundBlockLogEntry struct {
Dir string
Line []byte
Done chan struct{}
}
var (
foundBlockLogCh = make(chan foundBlockLogEntry, 64)
foundBlockLogOnce sync.Once
)
func init() {
foundBlockLogOnce.Do(startFoundBlockLogger)
}
func startFoundBlockLogger() {
go func() {
for entry := range foundBlockLogCh {
if entry.Done != nil {
close(entry.Done)
continue
}
// Use the shared state database connection
db := getSharedStateDB()
if db == nil {
continue
}
line := strings.TrimSpace(string(entry.Line))
if line == "" {
continue
}
if _, err := db.Exec("INSERT INTO found_blocks_log (created_at_unix, json) VALUES (?, ?)", time.Now().Unix(), line); err != nil {
logger.Warn("found block sqlite insert", "error", err)
}
}
}()
}