forked from influxdata/kapacitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
expvar.go
226 lines (195 loc) · 4.19 KB
/
expvar.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
// This package is a fork of the golang expvar expvar.Var types.
// Adding extra support for deleting and accessing raw typed values.
package expvar
import (
"bytes"
"expvar"
"fmt"
"math"
"sort"
"strconv"
"sync"
"sync/atomic"
)
type IntVar interface {
IntValue() int64
}
type FloatVar interface {
FloatValue() float64
}
type StringVar interface {
StringValue() string
}
// Int is a 64-bit integer variable that satisfies the expvar.Var interface.
type Int struct {
i int64
}
func (v *Int) String() string {
return strconv.FormatInt(v.IntValue(), 10)
}
func (v *Int) Add(delta int64) {
atomic.AddInt64(&v.i, delta)
}
func (v *Int) Set(value int64) {
atomic.StoreInt64(&v.i, value)
}
func (v *Int) IntValue() int64 {
return atomic.LoadInt64(&v.i)
}
// Float is a 64-bit float variable that satisfies the expvar.Var interface.
type Float struct {
f uint64
}
func (v *Float) String() string {
return strconv.FormatFloat(v.FloatValue(), 'g', -1, 64)
}
func (v *Float) FloatValue() float64 {
return math.Float64frombits(atomic.LoadUint64(&v.f))
}
// Add adds delta to v.
func (v *Float) Add(delta float64) {
for {
cur := atomic.LoadUint64(&v.f)
curVal := math.Float64frombits(cur)
nxtVal := curVal + delta
nxt := math.Float64bits(nxtVal)
if atomic.CompareAndSwapUint64(&v.f, cur, nxt) {
return
}
}
}
// Set sets v to value.
func (v *Float) Set(value float64) {
atomic.StoreUint64(&v.f, math.Float64bits(value))
}
// Map is a string-to-expvar.Var map variable that satisfies the expvar.Var interface.
type Map struct {
mu sync.RWMutex
m map[string]expvar.Var
}
func (v *Map) String() string {
v.mu.RLock()
defer v.mu.RUnlock()
var b bytes.Buffer
fmt.Fprintf(&b, "{")
first := true
v.doLocked(func(kv expvar.KeyValue) {
if !first {
fmt.Fprintf(&b, ", ")
}
fmt.Fprintf(&b, "%q: %v", kv.Key, kv.Value)
first = false
})
fmt.Fprintf(&b, "}")
return b.String()
}
func (v *Map) Init() *Map {
v.m = make(map[string]expvar.Var)
return v
}
func (v *Map) Get(key string) expvar.Var {
v.mu.RLock()
defer v.mu.RUnlock()
return v.m[key]
}
func (v *Map) Set(key string, av expvar.Var) {
v.mu.Lock()
defer v.mu.Unlock()
v.m[key] = av
}
func (v *Map) Delete(key string) {
v.mu.Lock()
defer v.mu.Unlock()
delete(v.m, key)
}
func (v *Map) Add(key string, delta int64) {
v.mu.RLock()
av, ok := v.m[key]
v.mu.RUnlock()
if !ok {
// check again under the write lock
v.mu.Lock()
av, ok = v.m[key]
if !ok {
av = new(Int)
v.m[key] = av
}
v.mu.Unlock()
}
// Add to Int; ignore otherwise.
if iv, ok := av.(*Int); ok {
iv.Add(delta)
}
}
// AddFloat adds delta to the *Float value stored under the given map key.
func (v *Map) AddFloat(key string, delta float64) {
v.mu.RLock()
av, ok := v.m[key]
v.mu.RUnlock()
if !ok {
// check again under the write lock
v.mu.Lock()
av, ok = v.m[key]
if !ok {
av = new(Float)
v.m[key] = av
}
v.mu.Unlock()
}
// Add to Float; ignore otherwise.
if iv, ok := av.(*Float); ok {
iv.Add(delta)
}
}
// Do calls f for each entry in the map.
// The map is locked during the iteration,
// but existing entries may be concurrently updated.
func (v *Map) Do(f func(expvar.KeyValue)) {
v.mu.RLock()
defer v.mu.RUnlock()
v.doLocked(f)
}
// DoSorted calls f for each entry in the map in sorted order.
// The map is locked during the iteration,
// but existing entries may be concurrently updated.
func (v *Map) DoSorted(f func(expvar.KeyValue)) {
v.mu.RLock()
defer v.mu.RUnlock()
keys := make([]string, len(v.m))
i := 0
for key := range v.m {
keys[i] = key
i++
}
sort.Strings(keys)
for _, k := range keys {
f(expvar.KeyValue{k, v.m[k]})
}
}
// doLocked calls f for each entry in the map.
// v.mu must be held for reads.
func (v *Map) doLocked(f func(expvar.KeyValue)) {
for k, v := range v.m {
f(expvar.KeyValue{k, v})
}
}
// String is a string variable, and satisfies the expvar.Var interface.
type String struct {
mu sync.RWMutex
s string
}
func (v *String) String() string {
v.mu.RLock()
defer v.mu.RUnlock()
return strconv.Quote(v.s)
}
func (v *String) Set(value string) {
v.mu.Lock()
defer v.mu.Unlock()
v.s = value
}
func (v *String) StringValue() string {
v.mu.RLock()
defer v.mu.RUnlock()
return v.s
}