/
bridgeserver.go
66 lines (56 loc) · 1.15 KB
/
bridgeserver.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
package main
import (
"fmt"
_ "net/http/pprof"
"os"
"os/signal"
"runtime"
"runtime/debug"
"runtime/pprof"
"runtime/trace"
"syscall"
bridge "github.com/mit-dci/utreexo/bridgenode"
)
func main() {
// The allocations from loading blocks from disk can cause
// bursts of big memory allocations. This helps avoid that
// by collecting garbage early.
debug.SetGCPercent(20)
// parse the config
cfg, err := bridge.Parse(os.Args[1:])
if err != nil {
fmt.Println(err)
fmt.Println(bridge.HelpMsg)
os.Exit(1)
}
// listen for SIGINT, SIGTERM, or SIGQUIT from the os
sig := make(chan bool, 1)
handleIntSig(sig, cfg)
err = bridge.Start(cfg, sig)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}
func handleIntSig(sig chan bool, cfg *bridge.Config) {
s := make(chan os.Signal, 1)
signal.Notify(s, syscall.SIGINT, syscall.SIGQUIT, syscall.SIGTERM)
go func() {
<-s
if cfg.CpuProf != "" {
pprof.StopCPUProfile()
}
if cfg.TraceProf != "" {
trace.Stop()
}
if cfg.MemProf != "" {
f, err := os.Create(cfg.MemProf)
if err != nil {
fmt.Println(err)
}
runtime.GC()
pprof.WriteHeapProfile(f)
}
sig <- true
}()
}