dotnet - add locking since the simulator seems to run a lot in parallel
change the port to match what the simulator uses by default add serilog as part of debugging
This commit is contained in:
+23
-15
@@ -16,6 +16,7 @@ public sealed class KnownDevices
|
||||
|
||||
public sealed class DeviceHeartBeatHistory
|
||||
{
|
||||
private readonly Lock _lock = new();
|
||||
|
||||
public enum AddResult { Added, OutOfSequence }
|
||||
|
||||
@@ -26,36 +27,43 @@ public sealed class DeviceHeartBeatHistory
|
||||
|
||||
public AddResult Add(DateTimeOffset sentAt)
|
||||
{
|
||||
// Reject heartbeats that arrive before the latest registered time.
|
||||
// Gaps are fine: uptime can be derived from first, last, and count.
|
||||
if (LastHeartBeat is null)
|
||||
lock (_lock)
|
||||
{
|
||||
FirstHeartBeat = sentAt;
|
||||
// Reject heartbeats that arrive before the latest registered time.
|
||||
// Gaps are fine: uptime can be derived from first, last, and count.
|
||||
if (LastHeartBeat is null)
|
||||
{
|
||||
FirstHeartBeat = sentAt;
|
||||
LastHeartBeat = sentAt;
|
||||
NumberOfHeartBeats = 1;
|
||||
return AddResult.Added;
|
||||
}
|
||||
|
||||
if (sentAt < LastHeartBeat)
|
||||
return AddResult.OutOfSequence;
|
||||
|
||||
// note: does not account for rejecting heartbeats that come between the one minute window.
|
||||
LastHeartBeat = sentAt;
|
||||
NumberOfHeartBeats = 1;
|
||||
NumberOfHeartBeats++;
|
||||
return AddResult.Added;
|
||||
}
|
||||
|
||||
if (sentAt < LastHeartBeat)
|
||||
return AddResult.OutOfSequence;
|
||||
|
||||
// note: does not account for rejecting heartbeats that come between the one minute window.
|
||||
LastHeartBeat = sentAt;
|
||||
NumberOfHeartBeats++;
|
||||
return AddResult.Added;
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class HeartBeatHistories
|
||||
{
|
||||
private readonly ConcurrentDictionary<DeviceId, DeviceHeartBeatHistory> _histories = new();
|
||||
private readonly Lock _lock = new ();
|
||||
|
||||
public DeviceHeartBeatHistory.AddResult Add(DeviceId deviceId, DateTimeOffset sentAt)
|
||||
{
|
||||
if (!_histories.TryGetValue(deviceId, out var history))
|
||||
{
|
||||
history = new();
|
||||
_histories[deviceId] = history;
|
||||
lock (_lock)
|
||||
{
|
||||
history = new();
|
||||
_histories[deviceId] = history;
|
||||
}
|
||||
}
|
||||
|
||||
return history.Add(sentAt);
|
||||
|
||||
Reference in New Issue
Block a user