-
Notifications
You must be signed in to change notification settings - Fork 1
/
option.go
65 lines (52 loc) · 1.1 KB
/
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package election
import (
"fmt"
"github.com/coreos/etcd/clientv3"
)
// Option option for election
type Option func(*options)
type options struct {
lockPath string
leaderPath string
leaseSec int64
client *clientv3.Client
}
func (opts *options) adjust() error {
if opts.client == nil {
return fmt.Errorf("missing etcd client")
}
if opts.leaderPath == "" {
opts.leaderPath = "/tmp/taas/election"
}
if opts.lockPath == "" {
opts.lockPath = "/tmp/taas/lock/election"
}
if opts.leaseSec == 0 {
opts.leaseSec = 5
}
return nil
}
// WithLeaderPath set leader path
func WithLeaderPath(value string) Option {
return func(opts *options) {
opts.leaderPath = value
}
}
// WithLockPath set lock path
func WithLockPath(value string) Option {
return func(opts *options) {
opts.lockPath = value
}
}
// WithLeaderLeaseSec set leader lease time in seconds
func WithLeaderLeaseSec(value int64) Option {
return func(opts *options) {
opts.leaseSec = value
}
}
// WithEtcd set etcd client
func WithEtcd(value *clientv3.Client) Option {
return func(opts *options) {
opts.client = value
}
}