Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
p2p
go-flow-metrics
Commits
f120cc70
Commit
f120cc70
authored
Nov 01, 2019
by
Roman Proskuryakov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix data race in Meter::Reset
parent
b52bb208
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
22 additions
and
2 deletions
+22
-2
meter.go
meter.go
+1
-1
meter_test.go
meter_test.go
+20
-0
sweeper.go
sweeper.go
+1
-1
No files found.
meter.go
View file @
f120cc70
...
@@ -59,7 +59,7 @@ func (m *Meter) Reset() {
...
@@ -59,7 +59,7 @@ func (m *Meter) Reset() {
defer
globalSweeper
.
snapshotMu
.
Unlock
()
defer
globalSweeper
.
snapshotMu
.
Unlock
()
atomic
.
StoreUint64
(
&
m
.
accumulator
,
0
)
atomic
.
StoreUint64
(
&
m
.
accumulator
,
0
)
m
.
snapshot
.
Rate
=
0
m
.
snapshot
.
Rate
=
0
m
.
snapshot
.
Total
=
0
atomic
.
StoreUint64
(
&
m
.
snapshot
.
Total
,
0
)
m
.
snapshot
.
LastUpdate
=
time
.
Now
()
m
.
snapshot
.
LastUpdate
=
time
.
Now
()
}
}
...
...
meter_test.go
View file @
f120cc70
...
@@ -3,6 +3,7 @@ package flow
...
@@ -3,6 +3,7 @@ package flow
import
(
import
(
"fmt"
"fmt"
"math"
"math"
"sync"
"testing"
"testing"
"time"
"time"
)
)
...
@@ -48,6 +49,25 @@ func TestResetMeter(t *testing.T) {
...
@@ -48,6 +49,25 @@ func TestResetMeter(t *testing.T) {
}
}
}
}
func
TestMarkResetMeterMulti
(
t
*
testing
.
T
)
{
var
wg
sync
.
WaitGroup
wg
.
Add
(
2
)
meter
:=
new
(
Meter
)
go
func
(
meter
*
Meter
)
{
meter
.
Mark
(
30
)
meter
.
Mark
(
30
)
wg
.
Done
()
}(
meter
)
go
func
(
meter
*
Meter
)
{
meter
.
Reset
()
wg
.
Done
()
}(
meter
)
wg
.
Wait
()
}
func
roundTens
(
x
float64
)
int64
{
func
roundTens
(
x
float64
)
int64
{
return
int64
(
math
.
Floor
(
x
/
10
+
0.5
))
*
10
return
int64
(
math
.
Floor
(
x
/
10
+
0.5
))
*
10
}
}
sweeper.go
View file @
f120cc70
...
@@ -45,7 +45,7 @@ func (sw *sweeper) run() {
...
@@ -45,7 +45,7 @@ func (sw *sweeper) run() {
func
(
sw
*
sweeper
)
register
(
m
*
Meter
)
{
func
(
sw
*
sweeper
)
register
(
m
*
Meter
)
{
// Add back the snapshot total. If we unregistered this
// Add back the snapshot total. If we unregistered this
// one, we set it to zero.
// one, we set it to zero.
atomic
.
AddUint64
(
&
m
.
accumulator
,
m
.
snapshot
.
Total
)
atomic
.
AddUint64
(
&
m
.
accumulator
,
atomic
.
LoadUint64
(
&
m
.
snapshot
.
Total
)
)
sw
.
meters
=
append
(
sw
.
meters
,
m
)
sw
.
meters
=
append
(
sw
.
meters
,
m
)
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment