-
Notifications
You must be signed in to change notification settings - Fork 3
/
options.go
132 lines (111 loc) · 3.65 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
124
125
126
127
128
129
130
131
132
package raft
import (
"errors"
"time"
"github.com/jmsadair/raft/logging"
)
const (
defaultElectionTimeout = time.Duration(300 * time.Millisecond)
defaultHeartbeat = time.Duration(50 * time.Millisecond)
defaultLeaseDuration = time.Duration(100 * time.Millisecond)
)
type options struct {
// Minimum election timeout in milliseconds. A random time
// between electionTimeout and 2 * electionTimeout will be
// chosen to determine when a server will hold an election.
electionTimeout time.Duration
// The interval in milliseconds between AppendEntries RPCs that
// the leader will send to the followers.
heartbeatInterval time.Duration
// The duration that a lease remains valid upon renewal.
leaseDuration time.Duration
// The level of logged messages.
logLevel logging.Level
// Indicates if log level was set or not.
levelSet bool
// A provided log that can be used by raft.
log Log
// A provided state storage that can be used by raft.
stateStorage StateStorage
// A provided snapshot storage that can be used by raft.
snapshotStorage SnapshotStorage
// A provided network transport that can be used by raft.
transport Transport
}
// Option is a function that updates the options associated with Raft.
type Option func(options *options) error
// WithElectionTimeout sets the election timeout for raft.
func WithElectionTimeout(time time.Duration) Option {
return func(options *options) error {
options.electionTimeout = time
return nil
}
}
// WithHeartbeatInterval sets the heartbeat interval for raft.
func WithHeartbeatInterval(time time.Duration) Option {
return func(options *options) error {
options.heartbeatInterval = time
return nil
}
}
// WithLeaseDuration sets the duration for which a lease remains valid upon
// renewal. The lease should generally remain valid for a much smaller amount of
// time than the election timeout.
func WithLeaseDuration(leaseDuration time.Duration) Option {
return func(options *options) error {
options.leaseDuration = leaseDuration
return nil
}
}
// WithLogger sets the log level used by raft.
func WithLogLevel(level logging.Level) Option {
return func(options *options) error {
options.logLevel = level
options.levelSet = true
return nil
}
}
// WithLog sets the log that will be used by raft. This is useful
// if you wish to use your own implementation of a log.
func WithLog(log Log) Option {
return func(options *options) error {
if log == nil {
return errors.New("log must not be nil")
}
options.log = log
return nil
}
}
// WithStateStorage sets the state storage that will be used by raft.
// This is useful if you wish to use your own implementation of a state storage.
func WithStateStorage(stateStorage StateStorage) Option {
return func(options *options) error {
if stateStorage == nil {
return errors.New("state storage must not be nil")
}
options.stateStorage = stateStorage
return nil
}
}
// WithSnapshotStorage sets the snapshot storage that will be used by raft.
// This is useful if you wish to use your own implementation of a snapshot storage.
func WithSnapshotStorage(snapshotStorage SnapshotStorage) Option {
return func(options *options) error {
if snapshotStorage == nil {
return errors.New("snapshot storage must not be nil")
}
options.snapshotStorage = snapshotStorage
return nil
}
}
// WithTransport sets the network transport that will be used by raft.
// This is useful if you wish to use your own implementation of a transport.
func WithTransport(transport Transport) Option {
return func(options *options) error {
if transport == nil {
return errors.New("transport must not be nil")
}
options.transport = transport
return nil
}
}