-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconfig.go
154 lines (116 loc) · 3.03 KB
/
config.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package main
import (
"fmt"
"time"
"github.com/BurntSushi/toml"
)
type duration struct {
time.Duration
}
func (d *duration) UnmarshalText(text []byte) error {
var err error
d.Duration, err = time.ParseDuration(string(text))
return err
}
type inputCfg struct {
Name string
Listen string
ListenWS string
TimeoutRead duration `toml:"timeout_read"`
TimeoutWrite duration `toml:"timeout_write"`
Outputs []string
}
type outputCfg struct {
Name string
Type string
Algo string
AlgoFailover bool `toml:"algo_failover"`
HashFields []string `toml:"hash_fields"`
CHTable string `toml:"ch_table"`
RiemannFields []string `toml:"riemann_fields"`
RiemannValue string `toml:"riemann_value"`
Targets []string
Connections int `toml:"connections"`
ReconnectInterval duration `toml:"reconnect_interval"`
TimeoutConnect duration `toml:"timeout_connect"`
TimeoutRead duration `toml:"timeout_read"`
TimeoutWrite duration `toml:"timeout_write"`
BufferSize int `toml:"buffer_size"`
BatchSize int `toml:"batch_size"`
BatchTimeout duration `toml:"batch_timeout"`
}
type config struct {
ListenHTTP string
StatsInterval duration `toml:"stats_interval"`
LogLevel string `toml:"log_level"`
Inputs map[string]*inputCfg `toml:"input"`
Outputs map[string]*outputCfg `toml:"output"`
}
func defaultConfig() config {
return config{
ListenHTTP: "127.0.0.1:12347",
StatsInterval: duration{60 * time.Second},
Outputs: map[string]*outputCfg{},
}
}
var cfg = defaultConfig()
func configLoad(file string) error {
if _, err := toml.DecodeFile(file, &cfg); err != nil {
return fmt.Errorf("Unable to load config: %s", err)
}
if len(cfg.Inputs) == 0 {
return fmt.Errorf("No inputs defined")
}
if len(cfg.Outputs) == 0 {
return fmt.Errorf("No outputs defined")
}
outputs := map[string]bool{}
for n, o := range cfg.Outputs {
outputs[n] = true
o.Name = n
if o.TimeoutConnect.Duration == 0 {
o.TimeoutConnect.Duration = 5 * time.Second
}
if o.TimeoutRead.Duration == 0 {
o.TimeoutRead.Duration = 5 * time.Second
}
if o.TimeoutWrite.Duration == 0 {
o.TimeoutWrite.Duration = 5 * time.Second
}
if o.ReconnectInterval.Duration == 0 {
o.ReconnectInterval.Duration = 1 * time.Second
}
if o.BufferSize == 0 {
o.BufferSize = 50000
}
if o.BatchSize == 0 {
o.BatchSize = 50
}
if o.BatchTimeout.Duration == 0 {
o.BatchTimeout.Duration = 1 * time.Second
}
if o.Connections == 0 {
o.Connections = 1
}
tgtMap := map[string]bool{}
for _, t := range o.Targets {
if tgtMap[t] {
return fmt.Errorf("Output '%s': Target '%s' is specified more than once", n, t)
}
tgtMap[t] = true
}
}
for n, i := range cfg.Inputs {
i.Name = n
if i.TimeoutRead.Duration == 0 {
i.TimeoutRead.Duration = 5 * time.Second
}
if i.TimeoutWrite.Duration == 0 {
i.TimeoutWrite.Duration = 5 * time.Second
}
if len(i.Outputs) == 0 {
return fmt.Errorf("Input %s: No outputs defined", n)
}
}
return nil
}