Skip to content

Commit

Permalink
feat: limit consumer status history
Browse files Browse the repository at this point in the history
  • Loading branch information
BEagle1984 committed Mar 18, 2022
1 parent 0a255b0 commit f96dd9d
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project>
<PropertyGroup Label="Package information">
<BaseVersionSuffix>-beta.1</BaseVersionSuffix>
<BaseVersionSuffix>-beta.2</BaseVersionSuffix>
<BaseVersion>3.6.1$(BaseVersionSuffix)</BaseVersion>
<DatabasePackagesRevision>1</DatabasePackagesRevision>
<DatabasePackagesVersionSuffix>$(BaseVersionSuffix)</DatabasePackagesVersionSuffix>
Expand Down
1 change: 1 addition & 0 deletions docs/releases.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ uid: releases
### Fixes

* Handle race condition in [BatchSequence](xref:Silverback.Messaging.Sequences.Batch.BatchSequence) with timeout
* Limit consumer status history

# Releases

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ namespace Silverback.Messaging.Broker
{
internal sealed class ConsumerStatusInfo : IConsumerStatusInfo
{
private readonly List<IConsumerStatusChange> _history = new();
private const int MaxHistorySize = 10;

private readonly LinkedList<IConsumerStatusChange> _history = new();

public IReadOnlyCollection<IConsumerStatusChange> History => _history;

Expand Down Expand Up @@ -43,7 +45,11 @@ public void RecordConsumedMessage(IBrokerMessageIdentifier? brokerMessageIdentif
private void ChangeStatus(ConsumerStatus status)
{
Status = status;
_history.Add(new ConsumerStatusChange(status));

if (_history.Count == MaxHistorySize)
_history.RemoveFirst();

_history.AddLast(new ConsumerStatusChange(status));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright (c) 2020 Sergio Aquilini
// This code is licensed under MIT license (see LICENSE file for details)

using System.Linq;
using FluentAssertions;
using Silverback.Messaging.Broker;
using Xunit;

namespace Silverback.Tests.Integration.Messaging.Broker
{
public class ConsumerStatusInfoFixture
{
[Fact]
public void History_ShouldBeRolledOver()
{
ConsumerStatusInfo statusInfo = new();

for (int i = 0; i < 5; i++)
{
statusInfo.SetConnected();
statusInfo.SetDisconnected();
}

statusInfo.History.Should().HaveCount(10);
statusInfo.History.First().Status.Should().Be(ConsumerStatus.Connected);

statusInfo.SetConnected();

statusInfo.History.Should().HaveCount(10);
statusInfo.History.First().Status.Should().Be(ConsumerStatus.Disconnected);
}
}
}

0 comments on commit f96dd9d

Please sign in to comment.