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:
2026-08-06 15:54:07 -05:00
parent 9a1664bdbb
commit 459b90dcd7
6 changed files with 117 additions and 29 deletions
+23 -15
View File
@@ -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);