/
main.go
102 lines (87 loc) · 1.94 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
package main
import (
"context"
"flag"
"net/http"
_ "net/http/pprof"
"os"
"sync"
"time"
"github.com/reddit/baseplate.go/log"
"github.com/reddit/baseplate.go/runtimebp"
yaml "gopkg.in/yaml.v2"
)
var (
configPath = flag.String(
"config",
"/data/config.yaml",
"path to the config file",
)
logLevel = flag.String(
"log-level",
"info",
"minimal log level to keep",
)
)
const (
defaultThreshold = 0.95
defaultLimit = 1024 * 10
)
type config struct {
Threshold *float64 `yaml:"similarity_threshold"`
Limit int64 `yaml:"read_limit"`
HN struct {
Username string `yaml:"username"`
Password string `yaml:"password"`
Timeout time.Duration `yaml:"timeout"`
ReplyTimeout time.Duration `yaml:"reply_timeout"`
Interval time.Duration `yaml:"interval"`
Workers int `yaml:"workers"`
TickLogSampleRate float64 `yaml:"tick_log_sample_rate"`
} `yaml:"hn"`
}
func main() {
flag.Parse()
log.InitLogger(log.Level(*logLevel))
cfg := parseConfig(*configPath)
if cfg.Threshold == nil {
t := defaultThreshold
cfg.Threshold = &t
}
if cfg.Limit <= 0 {
cfg.Limit = defaultLimit
}
go func() {
// for pprof
http.ListenAndServe("localhost:6060", nil)
}()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
var wg sync.WaitGroup
wg.Add(1)
go runtimebp.HandleShutdown(
context.Background(),
func(signal os.Signal) {
defer wg.Done()
log.Infow("shutting down...", "signal", signal)
cancel()
},
)
wg.Add(1)
go hnMain(ctx, &wg, cfg)
wg.Wait()
}
func parseConfig(path string) config {
var cfg config
f, err := os.Open(path)
if err != nil {
log.Fatalw("Cannot open config file", "err", err, "path", path)
}
defer f.Close()
decoder := yaml.NewDecoder(f)
decoder.SetStrict(true)
if err := decoder.Decode(&cfg); err != nil {
log.Fatalw("Cannot parse config file", "err", err, "path", path)
}
return cfg
}