Improve Server packet management.

This commit is contained in:
Christian
2022-04-21 20:48:52 +02:00
parent 920728e77a
commit fcf276dd1a
8 changed files with 101 additions and 53 deletions
+32 -7
View File
@@ -20,12 +20,12 @@ namespace MQTTnet.Internal
new LinkedList<MqttPacketBusItem>()
};
readonly SemaphoreSlim _semaphore = new SemaphoreSlim(0);
readonly SemaphoreSlim _semaphore = new SemaphoreSlim(0, 1);
readonly object _syncRoot = new object();
int _activePartition = (int)MqttPacketBusPartition.Health;
public int ItemsCount
public int TotalItemsCount
{
get
{
@@ -55,12 +55,26 @@ namespace MQTTnet.Internal
{
for (var i = 0; i < 3; i++)
{
// Iterate through the partitions in order to ensure processing of health packets
// even if lots of data packets are enqueued.
// Partition | Messages (left = oldest).
// DATA | [#]#########################
// CONTROL | [#]#############
// HEALTH | [#]####
// In this sample the 3 oldest messages from the partitions are processed in a row.
// Then the next 3 from all 3 partitions.
MoveActivePartition();
if (_partitions[_activePartition].Count > 0)
var activePartition = _partitions[_activePartition];
if (activePartition.First != null)
{
var item = _partitions[_activePartition].First;
_partitions[_activePartition].RemoveFirst();
var item = activePartition.First;
activePartition.RemoveFirst();
return item.Value;
}
}
@@ -104,9 +118,12 @@ namespace MQTTnet.Internal
lock (_syncRoot)
{
_partitions[(int)partition].AddLast(item);
}
_semaphore.Release();
if (_semaphore.CurrentCount == 0)
{
_semaphore.Release();
}
}
}
public List<MqttPacket> ExportPackets(MqttPacketBusPartition partition)
@@ -117,6 +134,14 @@ namespace MQTTnet.Internal
}
}
public int ItemsCount(MqttPacketBusPartition partition)
{
lock (_syncRoot)
{
return _partitions[(int)partition].Count;
}
}
public int PartitionItemsCount(MqttPacketBusPartition partition)
{
lock (_syncRoot)