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
6c16cea2
Commit
6c16cea2
authored
Dec 13, 2017
by
Steven Allen
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
don't assume that precisely one second has elapsed.
The go schedule isn't that accurate.
parent
15ce074f
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
18 additions
and
6 deletions
+18
-6
sweeper.go
sweeper.go
+18
-6
No files found.
sweeper.go
View file @
6c16cea2
...
...
@@ -24,6 +24,7 @@ type sweeper struct {
sweepOnce
sync
.
Once
meters
[]
*
Meter
mutex
sync
.
RWMutex
lastUpdateTime
time
.
Time
registerChannel
chan
*
Meter
}
...
...
@@ -49,6 +50,8 @@ func (sw *sweeper) register(m *Meter) {
func
(
sw
*
sweeper
)
runActive
()
{
ticker
:=
time
.
NewTicker
(
time
.
Second
)
defer
ticker
.
Stop
()
sw
.
lastUpdateTime
=
time
.
Now
()
for
len
(
sw
.
meters
)
>
0
{
// Scale back allocation.
if
len
(
sw
.
meters
)
*
2
<
cap
(
sw
.
meters
)
{
...
...
@@ -58,8 +61,8 @@ func (sw *sweeper) runActive() {
}
select
{
case
t
:=
<-
ticker
.
C
:
sw
.
update
(
t
)
case
<-
ticker
.
C
:
sw
.
update
()
case
m
:=
<-
sw
.
registerChannel
:
sw
.
register
(
m
)
}
...
...
@@ -68,18 +71,27 @@ func (sw *sweeper) runActive() {
// Till next time.
}
func
(
sw
*
sweeper
)
update
(
t
time
.
Time
)
{
func
(
sw
*
sweeper
)
update
()
{
sw
.
mutex
.
Lock
()
defer
sw
.
mutex
.
Unlock
()
now
:=
time
.
Now
()
tdiff
:=
now
.
Sub
(
sw
.
lastUpdateTime
)
if
tdiff
<=
0
{
return
}
sw
.
lastUpdateTime
=
now
timeMultiplier
:=
float64
(
time
.
Second
)
/
float64
(
tdiff
)
for
i
:=
0
;
i
<
len
(
sw
.
meters
);
i
++
{
m
:=
sw
.
meters
[
i
]
total
:=
atomic
.
LoadUint64
(
&
m
.
accumulator
)
diff
:=
total
-
m
.
snapshot
.
Total
instant
:=
timeMultiplier
*
float64
(
total
-
m
.
snapshot
.
Total
)
if
m
.
snapshot
.
Rate
==
0
{
m
.
snapshot
.
Rate
=
float64
(
diff
)
m
.
snapshot
.
Rate
=
instant
}
else
{
m
.
snapshot
.
Rate
+=
alpha
*
(
float64
(
diff
)
-
m
.
snapshot
.
Rate
)
m
.
snapshot
.
Rate
+=
alpha
*
(
instant
-
m
.
snapshot
.
Rate
)
}
m
.
snapshot
.
Total
=
total
...
...
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