Skip to content

Commit

Permalink
fix: #558 ported ThrottledBackgroundMessageProcessor from NetCore to …
Browse files Browse the repository at this point in the history
…Core (#562)
  • Loading branch information
miquelbeltran authored Feb 25, 2025
1 parent 82d2431 commit cff43c7
Show file tree
Hide file tree
Showing 6 changed files with 293 additions and 67 deletions.
8 changes: 8 additions & 0 deletions Mindscape.Raygun4Net.Core/RaygunSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@ public int BackgroundMessageWorkerCount
set { this["backgroundMessageWorkerCount"] = value; }
}

/// <summary>
/// Used to determine how many messages are in the queue before the background processor will add another worker to help process the queue.
/// </summary>
/// <remarks>
/// Defaults to 25, workers will be added for every 25 messages in the queue, until the BackgroundMessageWorkerCount is reached.
/// </remarks>
public int BackgroundMessageWorkerBreakpoint { get; set; } = 25;

[ConfigurationProperty("apikey", IsRequired = true, DefaultValue = "")]
[StringValidator]
public string ApiKey
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ public void ThrottledBackgroundMessageProcessor_CancellationRequested_IsCaughtAn
}

[Test]
public void Things_Throwing()
public void ThrottledBackgroundMessageProcessor_Enqueue_SingleMessage()
{
var secondMessageWasProcessed = false;

Expand All @@ -183,7 +183,7 @@ public void Things_Throwing()
}

[Test]
public void Things_Throwing_Many()
public void ThrottledBackgroundMessageProcessor_Enqueue_ManyMessages()
{
var secondMessageWasProcessed = false;
for (int j = 0; j < 100; j++)
Expand Down
2 changes: 2 additions & 0 deletions Mindscape.Raygun4Net.WebApi/RaygunWebApiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public RaygunWebApiClient()
_backgroundMessageProcessor = new ThrottledBackgroundMessageProcessor(
RaygunSettings.Settings.BackgroundMessageQueueMax,
RaygunSettings.Settings.BackgroundMessageWorkerCount,
RaygunSettings.Settings.BackgroundMessageWorkerBreakpoint,
Send);

Init();
Expand All @@ -87,6 +88,7 @@ public RaygunWebApiClient(string apiKey)
_backgroundMessageProcessor = new ThrottledBackgroundMessageProcessor(
RaygunSettings.Settings.BackgroundMessageQueueMax,
RaygunSettings.Settings.BackgroundMessageWorkerCount,
RaygunSettings.Settings.BackgroundMessageWorkerBreakpoint,
Send);

Init();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class ThrottledBackgroundMessageProcessorTests
[Test]
public void ThrottledBackgroundMessageProcessor_WithQueueSpace_AcceptsMessages()
{
var cut = new ThrottledBackgroundMessageProcessor(1, 0, _ => { });
var cut = new ThrottledBackgroundMessageProcessor(1, 0, 25, _ => { });
var enqueued = cut.Enqueue(new RaygunMessage());

Assert.That(enqueued, Is.True);
Expand All @@ -21,7 +21,7 @@ public void ThrottledBackgroundMessageProcessor_WithQueueSpace_AcceptsMessages()
[Test]
public void ThrottledBackgroundMessageProcessor_WithFullQueue_DropsMessages()
{
var cut = new ThrottledBackgroundMessageProcessor(1, 0, _ => { });
var cut = new ThrottledBackgroundMessageProcessor(1, 0, 25, _ => { });
cut.Enqueue(new RaygunMessage());
var second = cut.Enqueue(new RaygunMessage());

Expand All @@ -34,7 +34,7 @@ public void ThrottledBackgroundMessageProcessor_WithFullQueue_DropsMessages()
public void ThrottledBackgroundMessageProcessor_WithNoWorkers_DoesNotProcessMessages()
{
var processed = false;
var cut = new ThrottledBackgroundMessageProcessor(1, 0, _ => { processed = true; });
var cut = new ThrottledBackgroundMessageProcessor(1, 0, 25, _ => { processed = true; });

cut.Enqueue(new RaygunMessage());

Expand All @@ -49,7 +49,7 @@ public void ThrottledBackgroundMessageProcessor_WithAtLeastOneWorker_DoesProcess
{
var processed = false;
var resetEventSlim = new ManualResetEventSlim();
var cut = new ThrottledBackgroundMessageProcessor(1, 1, _ =>
var cut = new ThrottledBackgroundMessageProcessor(1, 1, 25, _ =>
{
processed = true;
resetEventSlim.Set();
Expand All @@ -68,7 +68,7 @@ public void ThrottledBackgroundMessageProcessor_WithAtLeastOneWorker_DoesProcess
[Test]
public void ThrottledBackgroundMessageProcessor_CallingDisposeTwice_DoesNotExplode()
{
var cut = new ThrottledBackgroundMessageProcessor(1, 0, _ => { });
var cut = new ThrottledBackgroundMessageProcessor(1, 0, 25, _ => { });

Assert.DoesNotThrow(() =>
{
Expand All @@ -85,7 +85,7 @@ public void ThrottledBackgroundMessageProcessor_ExceptionInProcess_KillsWorkerTh
var secondMessageWasProcessed = false;
var resetEventSlim = new ManualResetEventSlim();

var cut = new ThrottledBackgroundMessageProcessor(1, 1, _ =>
var cut = new ThrottledBackgroundMessageProcessor(1, 1, 25, _ =>
{
if (shouldThrow)
{
Expand Down Expand Up @@ -118,7 +118,7 @@ public void ThrottledBackgroundMessageProcessor_CancellationRequested_IsCaughtAn
var secondMessageWasProcessed = false;
var resetEventSlim = new ManualResetEventSlim();

var cut = new ThrottledBackgroundMessageProcessor(1, 1, _ =>
var cut = new ThrottledBackgroundMessageProcessor(1, 1, 25, _ =>
{
if (shouldThrow)
{
Expand All @@ -143,5 +143,41 @@ public void ThrottledBackgroundMessageProcessor_CancellationRequested_IsCaughtAn

Assert.That(secondMessageWasProcessed, Is.True);
}

[Test]
public void ThrottledBackgroundMessageProcessor_Enqueue_ManyMessages()
{
var secondMessageWasProcessed = false;
for (int j = 0; j < 100; j++)
{
var count = 0;
var resetEventSlim = new ManualResetEventSlim();

var cut = new ThrottledBackgroundMessageProcessor(100_000, 8, 25, _ =>
{
Interlocked.Increment(ref count);
if (count == 100)
{
secondMessageWasProcessed = true;
resetEventSlim.Set();
}

Console.WriteLine($"Sent {count}");
});


for (int i = 0; i < 100; i++)
{
cut.Enqueue(new RaygunMessage());
Console.WriteLine(i);
}

resetEventSlim.Wait(TimeSpan.FromSeconds(10));

cut.Dispose();
}

Assert.That(secondMessageWasProcessed, Is.True);
}
}
}
9 changes: 5 additions & 4 deletions Mindscape.Raygun4Net4/RaygunClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,11 @@ public RaygunClient(string apiKey)

UseXmlRawDataFilter = RaygunSettings.Settings.UseXmlRawDataFilter;
UseKeyValuePairRawDataFilter = RaygunSettings.Settings.UseKeyValuePairRawDataFilter;

_backgroundMessageProcessor = new ThrottledBackgroundMessageProcessor(
RaygunSettings.Settings.BackgroundMessageQueueMax,
RaygunSettings.Settings.BackgroundMessageWorkerCount,
RaygunSettings.Settings.BackgroundMessageWorkerBreakpoint,
Send);

ThreadPool.QueueUserWorkItem(state => { SendStoredMessages(); });
Expand Down Expand Up @@ -391,7 +392,7 @@ public void SendInBackground(Exception exception, IList<string> tags, IDictionar
try
{
var currentTime = DateTime.UtcNow;

StripAndSendInBackground(exception, tags, userCustomData, userInfo, currentTime);
}
catch (Exception)
Expand All @@ -403,7 +404,7 @@ public void SendInBackground(Exception exception, IList<string> tags, IDictionar
throw;
}
}

FlagAsSent(exception);
}
}
Expand All @@ -427,7 +428,7 @@ public void SendInBackground(RaygunMessage raygunMessage)
{
SendInBackground(() => raygunMessage);
}

public void SendInBackground(Func<RaygunMessage> raygunMessage)
{
if (!_backgroundMessageProcessor.Enqueue(raygunMessage))
Expand Down
Loading

0 comments on commit cff43c7

Please sign in to comment.