Merge pull request #266 from VladimirAkopyan/master

Limiting the growth of PendingMessagesQueue to a configurable Value
This commit is contained in:
Christian
2018-05-11 20:05:36 +02:00
committed by GitHub
5 changed files with 25 additions and 1 deletions
@@ -5,6 +5,8 @@ namespace MQTTnet.Server
public interface IMqttServerOptions public interface IMqttServerOptions
{ {
int ConnectionBacklog { get; } int ConnectionBacklog { get; }
int MaxPendingMessagesPerClient { get; }
TimeSpan DefaultCommunicationTimeout { get; } TimeSpan DefaultCommunicationTimeout { get; }
Action<MqttConnectionValidatorContext> ConnectionValidator { get; } Action<MqttConnectionValidatorContext> ConnectionValidator { get; }
@@ -50,6 +50,17 @@ namespace MQTTnet.Server
} }
} }
public async Task DropPacket()
{
MqttBasePacket packet = null;
await _queueWaitSemaphore.WaitAsync().ConfigureAwait(false);
if (!_queue.TryDequeue(out packet))
{
throw new InvalidOperationException(); // should not happen
}
_queueWaitSemaphore.Release();
}
public void Enqueue(MqttBasePacket packet) public void Enqueue(MqttBasePacket packet)
{ {
if (packet == null) throw new ArgumentNullException(nameof(packet)); if (packet == null) throw new ArgumentNullException(nameof(packet));
@@ -147,7 +147,10 @@ namespace MQTTnet.Server
{ {
publishPacket.PacketIdentifier = _packetIdentifierProvider.GetNewPacketIdentifier(); publishPacket.PacketIdentifier = _packetIdentifierProvider.GetNewPacketIdentifier();
} }
if (_options.MaxPendingMessagesPerClient <= PendingMessagesQueue.Count)
{
await PendingMessagesQueue.DropPacket();
}
PendingMessagesQueue.Enqueue(publishPacket); PendingMessagesQueue.Enqueue(publishPacket);
} }
@@ -9,6 +9,8 @@ namespace MQTTnet.Server
public MqttServerTlsEndpointOptions TlsEndpointOptions { get; } = new MqttServerTlsEndpointOptions(); public MqttServerTlsEndpointOptions TlsEndpointOptions { get; } = new MqttServerTlsEndpointOptions();
public int ConnectionBacklog { get; set; } = 10; public int ConnectionBacklog { get; set; } = 10;
public int MaxPendingMessagesPerClient { get; set; } = 250;
public TimeSpan DefaultCommunicationTimeout { get; set; } = TimeSpan.FromSeconds(15); public TimeSpan DefaultCommunicationTimeout { get; set; } = TimeSpan.FromSeconds(15);
@@ -13,6 +13,12 @@ namespace MQTTnet.Server
return this; return this;
} }
public MqttServerOptionsBuilder WithMaxPendingMessagesPerClient(int value)
{
_options.MaxPendingMessagesPerClient = value;
return this;
}
public MqttServerOptionsBuilder WithDefaultCommunicationTimeout(TimeSpan value) public MqttServerOptionsBuilder WithDefaultCommunicationTimeout(TimeSpan value)
{ {
_options.DefaultCommunicationTimeout = value; _options.DefaultCommunicationTimeout = value;