-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
115 lines (90 loc) · 2.34 KB
/
main.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package main
import (
"flag"
"log"
"net"
"os"
"runtime/pprof"
"sync"
)
var flagBootstrap string // Bootstrap from the given host
var flagConnect string // Connect only to the given address
var cpuprofile string // Profile CPU
var heapprofile string // Profile Memory
var memusage string // Memory usage over time
var verbose bool // Verbose logging
var fcpu, fheap, fmem *os.File
func init() {
flag.StringVar(&flagBootstrap, "bootstrap", "", "Node to bootstrap from if none are known")
flag.StringVar(&flagConnect, "connect", "", "Connect only to the given node")
flag.StringVar(&cpuprofile, "cpuprofile", "", "Write CPU profile to file")
flag.StringVar(&heapprofile, "heapprofile", "", "Write heap profile to file")
flag.StringVar(&memusage, "memusage", "", "Write memory usage to file on every node refresh")
verboseFlag := flag.Bool("v", false, "Verbose output")
flag.Parse()
verbose = *verboseFlag
logFlags := 0 // No log flags by default
if verbose {
logFlags = logFlags | log.Ldate | log.Ltime | log.Lshortfile
}
log.SetFlags(logFlags)
}
func main() {
var err error
if cpuprofile != "" {
fcpu, err = os.Create(cpuprofile)
if err != nil {
log.Fatal(err)
}
defer fcpu.Close()
pprof.StartCPUProfile(fcpu)
defer pprof.StopCPUProfile()
}
if heapprofile != "" {
fheap, err = os.Create(heapprofile)
if err != nil {
log.Fatal(err)
}
go UpdateHeapProfile()
defer fheap.Close()
defer pprof.WriteHeapProfile(fheap)
}
if memusage != "" {
fmem, err = os.Create(memusage)
if err != nil {
log.Fatal(err)
}
defer fmem.Close()
}
err = initDB()
if err != nil {
log.Fatal(err)
}
addresses := make(chan ip_port, 2*ADDRESSES_NUM)
nodes := make(chan Node, NODE_BUFFER_SIZE)
save := make(chan Node, NODE_BUFFER_SIZE)
wg := &sync.WaitGroup{}
if flagConnect != "" {
ip, port, err := net.SplitHostPort(flagConnect)
if err != nil {
log.Fatal("Could not parse address to connect to: ", err)
}
if ip == "" {
log.Fatal("IP must be specified")
}
log.Print("Connecting to ", flagConnect)
addresses <- ip_port{ip, port}
close(addresses)
} else {
wg.Add(1)
go getNodes(addresses, wg)
}
wg.Add(3)
go connectNodes(addresses, nodes, wg)
go updateNodes(nodes, save, wg)
go saveNodes(save, wg)
go stats(60, true)
// Wait for all three main goroutines to end
wg.Wait()
cleanDB()
}