/
config.go
86 lines (76 loc) · 2.25 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
package raftmeta
import (
"github.com/BurntSushi/toml"
"golang.org/x/text/encoding/unicode"
"golang.org/x/text/transform"
"io/ioutil"
"net"
)
const (
DefaultNumPendingProposals = 1000
DefaultAddr = "127.0.0.1:2347"
DefaultElectionTick = 100
DefaultHeartbeatTick = 1
DefaultMaxSizePerMsg = 4096
DefaultMaxInflightMsgs = 256
)
type IPRange struct {
Lower, Upper net.IP
}
type Config struct {
NumPendingProposals int `toml:"num-pending-proposals"`
Tracing bool `toml:"tracing"`
MyAddr string `toml:"my-addr"`
Peers []Peer `toml:"peers"`
RaftId uint64 `toml:"raft-id"`
TickTimeMs int `toml:"tick-time-ms"`
ElectionTick int `toml:"election-tick"`
HeartbeatTick int `toml:"heartbeat-tick"`
MaxSizePerMsg uint64 `toml:"max-size-per-msg"`
MaxInflightMsgs int `toml:"max-inflight-msgs"`
WalDir string `toml:"wal-dir"`
SnapshotIntervalSec int `toml:"snapshot-interval"`
ChecksumIntervalSec int `toml:"checksum-interval"`
RetentionAutoCreate bool `toml:"retention-auto-create"`
}
type Peer struct {
Addr string `toml:"addr"`
RaftId uint64 `toml:"raft-id"`
}
// NewConfig returns an instance of Config with defaults.
func NewConfig() Config {
return Config{
NumPendingProposals: DefaultNumPendingProposals,
Tracing: false,
MyAddr: DefaultAddr,
RaftId: 1,
Peers: []Peer{},
TickTimeMs: 20,
ElectionTick: DefaultElectionTick,
HeartbeatTick: DefaultHeartbeatTick,
MaxSizePerMsg: DefaultMaxSizePerMsg,
MaxInflightMsgs: DefaultMaxInflightMsgs,
WalDir: "./wal",
SnapshotIntervalSec: 60,
ChecksumIntervalSec: 10,
RetentionAutoCreate: true,
}
}
// FromTomlFile loads the config from a TOML file.
func (c *Config) FromTomlFile(fpath string) error {
bs, err := ioutil.ReadFile(fpath)
if err != nil {
return err
}
bom := unicode.BOMOverride(transform.Nop)
bs, _, err = transform.Bytes(bom, bs)
if err != nil {
return err
}
return c.FromToml(string(bs))
}
// FromToml loads the config from TOML.
func (c *Config) FromToml(input string) error {
_, err := toml.Decode(input, c)
return err
}