Skip to content

Commit

Permalink
Handle early wakeup of counter loop (#57170)
Browse files Browse the repository at this point in the history
* Fix #56695
* prevent a negative delta value

* PR feedback + bug fix
* bug fix is preexisting: if a user other than the one who is listening sets the interval to 0 while the counter is enabled, it makes the counter continuously submit values. Changed this so that an interval value of <= 0 results in the counter being disabled

* Add assert after offline conversation
  • Loading branch information
John Salem authored Aug 13, 2021
1 parent afbb7df commit 3042bd7
Showing 1 changed file with 4 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ private void EnableTimer(float pollingIntervalInSeconds)
Debug.Assert(Monitor.IsEntered(s_counterGroupLock));
if (pollingIntervalInSeconds <= 0)
{
_pollingIntervalInMilliseconds = 0;
DisableTimer();
}
else if (_pollingIntervalInMilliseconds == 0 || pollingIntervalInSeconds * 1000 < _pollingIntervalInMilliseconds)
{
Expand Down Expand Up @@ -187,6 +187,7 @@ private void EnableTimer(float pollingIntervalInSeconds)

private void DisableTimer()
{
Debug.Assert(Monitor.IsEntered(s_counterGroupLock));
_pollingIntervalInMilliseconds = 0;
s_counterGroupEnabledList?.Remove(this);
}
Expand Down Expand Up @@ -252,7 +253,8 @@ private void OnTimer()
{
_timeStampSinceCollectionStarted = now;
TimeSpan delta = now - _nextPollingTimeStamp;
if (delta > TimeSpan.Zero && _pollingIntervalInMilliseconds > 0)
delta = _pollingIntervalInMilliseconds > delta.TotalMilliseconds ? TimeSpan.FromMilliseconds(_pollingIntervalInMilliseconds) : delta;
if (_pollingIntervalInMilliseconds > 0)
_nextPollingTimeStamp += TimeSpan.FromMilliseconds(_pollingIntervalInMilliseconds * Math.Ceiling(delta.TotalMilliseconds / _pollingIntervalInMilliseconds));
}
}
Expand Down

0 comments on commit 3042bd7

Please sign in to comment.