-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathelection_option.go
50 lines (42 loc) · 1.23 KB
/
election_option.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
package prophet
import (
"fmt"
)
// ElectorOption elector option
type ElectorOption func(*electorOptions)
type electorOptions struct {
leaderPath, lockPath string
leaseSec int64
lockIfBecomeLeader bool
}
func (opts *electorOptions) adjust() {
if opts.leaderPath == "" || opts.lockPath == "" {
opts.leaderPath = "/electors/leader"
opts.lockPath = "/electors/lock"
}
if opts.leaseSec <= 0 {
opts.leaseSec = 5
}
}
// WithPrefix set data prefix in embed etcd server
func WithPrefix(value string) ElectorOption {
return func(opts *electorOptions) {
opts.leaderPath = fmt.Sprintf("%s/leader", value)
opts.lockPath = fmt.Sprintf("%s/lock", value)
}
}
// WithLeaderLeaseSeconds set leader lease in seconds
func WithLeaderLeaseSeconds(value int64) ElectorOption {
return func(opts *electorOptions) {
opts.leaseSec = value
}
}
// WithLockIfBecomeLeader set lock enable flag if become leader,
// If true, will add a distributed lock, and will unlock on become follower,
// ensure that the other nodes can be changed to leaders after the previous
// leader has processed the role changes.
func WithLockIfBecomeLeader(value bool) ElectorOption {
return func(opts *electorOptions) {
opts.lockIfBecomeLeader = value
}
}