/
main.go
84 lines (70 loc) · 1.7 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
// Copyright Fuzamei Corp. 2018 All Rights Reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"flag"
_ "net/http/pprof"
"os"
"os/signal"
"path/filepath"
"runtime"
"time"
"github.com/33cn/chain33/common/limits"
clog "github.com/33cn/chain33/common/log"
log "github.com/33cn/chain33/common/log/log15"
"github.com/33cn/chain33/types"
"github.com/33cn/plugin/plugin/dapp/relay/cmd/relayd/relayd"
)
var (
cpuNum = runtime.NumCPU()
configPath = flag.String("f", "relayd.toml", "configfile")
chain33ConfigPath = flag.String("chain33flie", "", "chain33configfile")
)
func main() {
// TODO this is daemon
runtime.GOMAXPROCS(cpuNum)
os.Chdir(pwd())
d, _ := os.Getwd()
log.Info("current dir:", "dir", d)
err := limits.SetLimits()
if err != nil {
panic(err)
}
flag.Parse()
cfg := relayd.NewConfig(*configPath)
clog.SetFileLog(&cfg.Log)
if *chain33ConfigPath != "" {
cfg.Chain33Cfg = types.NewChain33Config(types.ReadFile(*chain33ConfigPath))
}
if cfg.Watch {
// set watching
log.Info("watching 10s")
t := time.Tick(10 * time.Second)
go func() {
for range t {
watching()
}
}()
}
r := relayd.NewRelayd(cfg)
go r.Start()
interrupt := make(chan os.Signal, 2)
signal.Notify(interrupt, os.Interrupt, os.Kill)
s := <-interrupt
log.Warn("Got signal:", "signal", s)
r.Close()
}
func watching() {
var m runtime.MemStats
runtime.ReadMemStats(&m)
log.Info("watching:", "NumGoroutine:", runtime.NumGoroutine())
log.Info("watching:", "Mem:", m.Sys/(1024*1024))
}
func pwd() string {
dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
if err != nil {
panic(err)
}
return dir
}