-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfig.go
88 lines (69 loc) · 1.55 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
87
88
package master
import (
"time"
"github.com/changsongl/master-election/log"
)
const (
defaultHeartBeat = time.Second * 6
defaultVersion = "default"
defaultHeartBeatMultiplier = 3
)
type config struct {
Heartbeat time.Duration
HeartbeatMultiplier int
Version string
MasterStartHook func(epoch uint64)
MasterEndHook func(epoch uint64)
DefaultLoggerLogLevel log.Level
Logger log.Logger
}
func newDefaultConfig() *config {
return &config{
Heartbeat: defaultHeartBeat,
Version: defaultVersion,
HeartbeatMultiplier: defaultHeartBeatMultiplier,
DefaultLoggerLogLevel: log.LevelInfo,
}
}
type Option interface {
apply(c *config)
}
type optFunc func(c *config)
func (f optFunc) apply(c *config) {
f(c)
}
func OptionHeartbeat(h time.Duration) Option {
return optFunc(func(c *config) {
c.Heartbeat = h
})
}
func OptionHeartBeatMultiplier(m int) Option {
return optFunc(func(c *config) {
c.HeartbeatMultiplier = m
})
}
func OptionVersion(version string) Option {
return optFunc(func(c *config) {
c.Version = version
})
}
func OptionMasterStartHook(f func(epoch uint64)) Option {
return optFunc(func(c *config) {
c.MasterStartHook = f
})
}
func OptionMasterEndHook(f func(epoch uint64)) Option {
return optFunc(func(c *config) {
c.MasterEndHook = f
})
}
func OptionLogger(l log.Logger) Option {
return optFunc(func(c *config) {
c.Logger = l
})
}
func OptionDefaultLoggerLogLevel(l log.Level) Option {
return optFunc(func(c *config) {
c.DefaultLoggerLogLevel = l
})
}