-
Notifications
You must be signed in to change notification settings - Fork 211
/
clock_options.go
82 lines (67 loc) · 1.73 KB
/
clock_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
package timesync
import (
"errors"
"time"
"github.com/jonboulle/clockwork"
"go.uber.org/zap"
)
type option struct {
clock clockwork.Clock
genesisTime time.Time
layerDuration time.Duration
tickInterval time.Duration
log *zap.Logger
}
func (o *option) validate() error {
if o.genesisTime.IsZero() {
return errors.New("bad configuration: genesis time is zero")
}
if o.layerDuration == 0 {
return errors.New("bad configuration: layer duration is zero")
}
if o.tickInterval == 0 {
return errors.New("bad configuration: tick interval is zero")
}
if o.tickInterval < 0 || o.tickInterval > o.layerDuration {
return errors.New("bad configuration: tick interval must be between 0 and layer duration")
}
if o.log == nil {
return errors.New("bad configuration: logger is nil")
}
return nil
}
type OptionFunc func(*option) error
// withClock specifies which clock the NodeClock should use. Defaults to the real clock.
func withClock(clock clockwork.Clock) OptionFunc {
return func(opts *option) error {
opts.clock = clock
return nil
}
}
// WithGenesisTime sets the genesis time for the NodeClock.
func WithGenesisTime(genesis time.Time) OptionFunc {
return func(opts *option) error {
opts.genesisTime = genesis
return nil
}
}
// WithLayerDuration sets the layer duration for the NodeClock.
func WithLayerDuration(d time.Duration) OptionFunc {
return func(opts *option) error {
opts.layerDuration = d
return nil
}
}
func WithTickInterval(d time.Duration) OptionFunc {
return func(opts *option) error {
opts.tickInterval = d
return nil
}
}
// WithLogger sets the logger for the NodeClock.
func WithLogger(logger *zap.Logger) OptionFunc {
return func(opts *option) error {
opts.log = logger
return nil
}
}