Clean up race condition in BridgeController content logging disabling

This commit is contained in:
Jordan Dominion
2024-01-09 18:44:48 -05:00
parent 5e5987c365
commit e377a2ed98
2 changed files with 15 additions and 8 deletions
@@ -36,7 +36,12 @@ namespace Tgstation.Server.Host.Controllers
/// <summary>
/// If the content of bridge requests and responses should be logged.
/// </summary>
internal static bool LogContent { get; set; }
static bool LogContent => logContentDisableCounter == 0;
/// <summary>
/// Counter which, if not zero, indicates content logging should be disabled.
/// </summary>
static uint logContentDisableCounter;
/// <summary>
/// Static counter for the number of requests processed.
@@ -54,12 +59,14 @@ namespace Tgstation.Server.Host.Controllers
readonly ILogger<BridgeController> logger;
/// <summary>
/// Initializes static members of the <see cref="BridgeController"/> class.
/// Temporarily disable content logging. Must be followed up with a call to <see cref="ReenableContentLogging"/>.
/// </summary>
static BridgeController()
{
LogContent = true;
}
internal static void TemporarilyDisableContentLogging() => Interlocked.Increment(ref logContentDisableCounter);
/// <summary>
/// Reenable content logging. Must be preceeded with a call to <see cref="TemporarilyDisableContentLogging"/>.
/// </summary>
internal static void ReenableContentLogging() => Interlocked.Decrement(ref logContentDisableCounter);
/// <summary>
/// Initializes a new instance of the <see cref="BridgeController"/> class.
@@ -893,7 +893,7 @@ namespace Tgstation.Server.Tests.Live.Instance
{
// first check the bridge limits
var bridgeTestsTcs = new TaskCompletionSource();
BridgeController.LogContent = false;
BridgeController.TemporarilyDisableContentLogging();
using (var loggerFactory = LoggerFactory.Create(builder =>
{
builder.AddConsole();
@@ -914,7 +914,7 @@ namespace Tgstation.Server.Tests.Live.Instance
await bridgeTestsTcs.Task.WaitAsync(cancellationToken);
}
BridgeController.LogContent = true;
BridgeController.ReenableContentLogging();
// Time for DD to revert the bridge access identifier change
await Task.Delay(TimeSpan.FromSeconds(1), cancellationToken);