-
Notifications
You must be signed in to change notification settings - Fork 0
/
duration.go
121 lines (105 loc) · 3.36 KB
/
duration.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
// Copyright 2017 The Cockroach Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License.
package settings
import (
"sync/atomic"
"time"
"github.com/pkg/errors"
)
// DurationSetting is the interface of a setting variable that will be
// updated automatically when the corresponding cluster-wide setting
// of type "duration" is updated.
type DurationSetting struct {
defaultValue time.Duration
v int64
validateFn func(time.Duration) error
}
var _ Setting = &DurationSetting{}
// Get retrieves the duration value in the setting.
func (d *DurationSetting) Get() time.Duration {
return time.Duration(atomic.LoadInt64(&d.v))
}
func (d *DurationSetting) String() string {
return EncodeDuration(d.Get())
}
// Typ returns the short (1 char) string denoting the type of setting.
func (*DurationSetting) Typ() string {
return "d"
}
// Validate that a value conforms with the validation function.
func (d *DurationSetting) Validate(v time.Duration) error {
if d.validateFn != nil {
if err := d.validateFn(v); err != nil {
return err
}
}
return nil
}
func (d *DurationSetting) set(v time.Duration) error {
if err := d.Validate(v); err != nil {
return err
}
atomic.StoreInt64(&d.v, int64(v))
return nil
}
func (d *DurationSetting) setToDefault() {
if err := d.set(d.defaultValue); err != nil {
panic(err)
}
}
// RegisterDurationSetting defines a new setting with type duration.
func RegisterDurationSetting(key, desc string, defaultValue time.Duration) *DurationSetting {
return RegisterValidatedDurationSetting(key, desc, defaultValue, nil)
}
// RegisterPositiveDurationSetting defines a new setting with type duration.
func RegisterPositiveDurationSetting(
key, desc string, defaultValue time.Duration,
) *DurationSetting {
return RegisterValidatedDurationSetting(key, desc, defaultValue, func(v time.Duration) error {
if v < 0 {
return errors.Errorf("cannot set %s to a negative duration: %s", key, v)
}
return nil
})
}
// RegisterValidatedDurationSetting defines a new setting with type duration.
func RegisterValidatedDurationSetting(
key, desc string, defaultValue time.Duration, validateFn func(time.Duration) error,
) *DurationSetting {
if validateFn != nil {
if err := validateFn(defaultValue); err != nil {
panic(errors.Wrap(err, "invalid default"))
}
}
setting := &DurationSetting{
defaultValue: defaultValue,
validateFn: validateFn,
}
register(key, desc, setting)
return setting
}
// TestingSetDuration returns a mock, unregistered string setting for testing.
// See TestingSetBool for more details.
func TestingSetDuration(s **DurationSetting, v time.Duration) func() {
saved := *s
*s = &DurationSetting{v: int64(v)}
return func() {
*s = saved
}
}
// TestingDuration returns a one off, unregistered duration setting for test use
// only.
func TestingDuration(v time.Duration) *DurationSetting {
return &DurationSetting{v: int64(v)}
}