/
options.go
123 lines (106 loc) · 2.2 KB
/
options.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
package raft
import (
"net"
"os"
"time"
)
type Options struct {
raftDir string
peerID string
isLeader bool
maxPool int
timeout time.Duration
retainSnapshots int
restoreSnapshotOnRestart bool
advertise net.Addr
electionTimeout time.Duration
heartbeatTimeout time.Duration
commitTimeout time.Duration
leaseTimeout time.Duration
debug bool
}
func (o *Options) setDefaults() {
if o.maxPool <= 0 {
o.maxPool = 5
}
if o.timeout <= 0 {
o.timeout = 5 * time.Second
}
if o.retainSnapshots == 0 {
o.retainSnapshots = 1
}
if o.peerID == "" {
h, _ := os.Hostname()
o.peerID = hash([]byte(h))
}
if o.raftDir == "" {
o.raftDir = "/tmp/graphik/raft"
}
os.MkdirAll(o.raftDir, 0700)
}
type Opt func(o *Options)
func WithPeerID(peerID string) Opt {
return func(o *Options) {
o.peerID = peerID
}
}
func WithIsLeader(isLeader bool) Opt {
return func(o *Options) {
o.isLeader = isLeader
}
}
func WithTimeout(timeout time.Duration) Opt {
return func(o *Options) {
o.timeout = timeout
}
}
func WithElectionTimeout(timeout time.Duration) Opt {
return func(o *Options) {
o.electionTimeout = timeout
}
}
func WithHeartbeatTimeout(timeout time.Duration) Opt {
return func(o *Options) {
o.heartbeatTimeout = timeout
}
}
func WithMaxPool(max int) Opt {
return func(o *Options) {
o.maxPool = max
}
}
func WithSnapshotRetention(retention int) Opt {
return func(o *Options) {
o.retainSnapshots = retention
}
}
func WithRaftDir(dir string) Opt {
return func(o *Options) {
o.raftDir = dir
}
}
func WithRestoreSnapshotOnRestart(restore bool) Opt {
return func(o *Options) {
o.restoreSnapshotOnRestart = restore
}
}
func WithAdvertiseAddr(addr net.Addr) Opt {
return func(o *Options) {
o.advertise = addr
}
}
func WithLeaderLeaseTimeout(timeout time.Duration) Opt {
return func(o *Options) {
o.leaseTimeout = timeout
}
}
func WithCommitTimeout(timeout time.Duration) Opt {
return func(o *Options) {
o.commitTimeout = timeout
}
}
func WithDebug(debug bool) Opt {
return func(o *Options) {
o.debug = debug
}
}