Skip to content

Commit 1af867a

Browse files
committed
Take overflow into consideration.
1 parent 2e8ff5d commit 1af867a

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

projects/RabbitMQ.Client/client/impl/Connection.cs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,9 @@ internal sealed class Connection : IConnection
8686

8787
private TimeSpan _heartbeat = TimeSpan.Zero;
8888
private TimeSpan _heartbeatTimeSpan = TimeSpan.FromSeconds(0);
89-
private int _missedHeartbeats = 0;
89+
private int _missedHeartbeats;
9090
private int _heartbeatCounter;
91-
private int _lastHeartbeat;
91+
private int _lastHeartbeatCounter;
9292

9393
private Timer _heartbeatWriteTimer;
9494
private Timer _heartbeatReadTimer;
@@ -618,7 +618,12 @@ public void MainLoopIteration()
618618

619619
public void NotifyHeartbeatListener()
620620
{
621-
_heartbeatCounter++;
621+
// https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/checked-and-unchecked
622+
// No worries if this overflows. What matters is that the value changes.
623+
unchecked
624+
{
625+
_heartbeatCounter++;
626+
}
622627
}
623628

624629
public void NotifyReceivedCloseOk()
@@ -845,7 +850,7 @@ public void HeartbeatReadTimerCallback(object state)
845850
{
846851
if (!_closed)
847852
{
848-
if (_lastHeartbeat == _heartbeatCounter)
853+
if (_lastHeartbeatCounter == _heartbeatCounter)
849854
{
850855
_missedHeartbeats++;
851856
}
@@ -854,7 +859,7 @@ public void HeartbeatReadTimerCallback(object state)
854859
_missedHeartbeats = 0;
855860
}
856861

857-
_lastHeartbeat = _heartbeatCounter;
862+
_lastHeartbeatCounter = _heartbeatCounter;
858863

859864
// We check against 8 = 2 * 4 because we need to wait for at
860865
// least two complete heartbeat setting intervals before

0 commit comments

Comments
 (0)