Skip to content

Commit

Permalink
Merge pull request #542 from MindscapeHQ/ph/idle-tasks
Browse files Browse the repository at this point in the history
Updated ThrottledBackgroundMessageProcessor to dynamically create task workers
  • Loading branch information
phillip-haydon authored Aug 25, 2024
2 parents f2650f5 + 508d60f commit b9c7331
Show file tree
Hide file tree
Showing 7 changed files with 345 additions and 82 deletions.
3 changes: 3 additions & 0 deletions CHANGE-LOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
### v11.0.4
- Fix issue with `RaygunClientBase` where `SendInBackground` deferred building the message until late, losing HttpContext
- See: https://github.com/MindscapeHQ/raygun4net/pull/540
- Fix issue with `ThrottledBackgroundMessageProcessor` where it would hold up to 8 task threads even when idle
- Task Workers are now created as needed and are disposed when not needed or idle
- See: https://github.com/MindscapeHQ/raygun4net/pull/542

### v11.0.3
- Update `RaygunHttpModule` (Raygun4Net ASP.NET Framework) to use a singleton `RaygunClient` instance
Expand Down
22 changes: 20 additions & 2 deletions Mindscape.Raygun4Net.NetCore.Common/Offline/OfflineStoreBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Net.Http;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;

Expand All @@ -13,6 +15,7 @@ public abstract class OfflineStoreBase
{
private readonly IBackgroundSendStrategy _backgroundSendStrategy;
protected SendHandler? SendCallback { get; set; }
private static readonly Regex HttpStatusCodeRegex = new("(?<statusCode>4[0-9]{2}) \\(.+\\)");

protected OfflineStoreBase(IBackgroundSendStrategy backgroundSendStrategy)
{
Expand Down Expand Up @@ -42,16 +45,31 @@ protected virtual async Task ProcessOfflineCrashReports()
await SendCallback(crashReport.MessagePayload, crashReport.ApiKey, CancellationToken.None);
await Remove(crashReport.Id, CancellationToken.None);
}
catch (HttpRequestException hrex)
{
var statusCode = HttpStatusCodeRegex.Match(hrex.Message).Groups["statusCode"]?.Value ?? "0";

if (int.TryParse(statusCode, out var code) && code is >= 400 and < 500)
{
Trace.WriteLine($"Crash report returned {code} error, removing from cache");
// Client error, remove the crash report
await Remove(crashReport.Id, CancellationToken.None);
continue;
}

Trace.WriteLine($"Crash report returned {statusCode} error, keeping in cache for retry");
throw;
}
catch (Exception ex)
{
Debug.WriteLine($"Exception sending offline error [{crashReport.Id}]: {ex}");
Trace.WriteLine($"Exception sending offline error [{crashReport.Id}]: {ex}");
throw;
}
}
}
catch (Exception ex)
{
Debug.WriteLine($"Exception sending offline errors: {ex}");
Trace.WriteLine($"Exception sending offline errors: {ex}");
}
}

Expand Down
2 changes: 1 addition & 1 deletion Mindscape.Raygun4Net.NetCore.Common/RaygunClientBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ protected RaygunClientBase(RaygunSettingsBase settings, HttpClient client, IRayg
{
_client = client ?? DefaultClient;
_settings = settings;
_backgroundMessageProcessor = new ThrottledBackgroundMessageProcessor(settings.BackgroundMessageQueueMax, _settings.BackgroundMessageWorkerCount, Send);
_backgroundMessageProcessor = new ThrottledBackgroundMessageProcessor(settings.BackgroundMessageQueueMax, _settings.BackgroundMessageWorkerCount, _settings.BackgroundMessageWorkerBreakpoint, Send);
_userProvider = userProvider;

_wrapperExceptions.Add(typeof(TargetInvocationException));
Expand Down
14 changes: 11 additions & 3 deletions Mindscape.Raygun4Net.NetCore.Common/RaygunSettingsBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ public RaygunSettingsBase()
public string ApplicationVersion { get; set; }

/// <summary>
/// If set to true will automatically setup handlers to catch Unhandled Exceptions
/// If set to true will automatically set up handlers to catch Unhandled Exceptions
/// </summary>
/// <remarks>
/// Currently defaults to false. This may be change in future releases.
/// Currently defaults to false. This may be changed in future releases.
/// </remarks>
public bool CatchUnhandledExceptions { get; set; } = false;

Expand All @@ -45,13 +45,21 @@ public RaygunSettingsBase()
public int BackgroundMessageQueueMax { get; } = ushort.MaxValue;

/// <summary>
/// Controls the number of background threads used to process the raygun message queue
/// Controls the maximum number of background threads used to process the raygun message queue
/// </summary>
/// <remarks>
/// Defaults to Environment.ProcessorCount * 2 &gt;= 8 ? 8 : Environment.ProcessorCount * 2
/// </remarks>
public int BackgroundMessageWorkerCount { get; set; } = Environment.ProcessorCount * 2 >= 8 ? 8 : Environment.ProcessorCount * 2;

/// <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;

/// <summary>
/// A list of Environment Variables to include with the message.
/// </summary>
Expand Down
Loading

0 comments on commit b9c7331

Please sign in to comment.