From a14cbee52f6c33ca8e69b18168c2b980ddb60f9e Mon Sep 17 00:00:00 2001 From: Alex Date: Thu, 1 May 2025 10:45:02 +0100 Subject: [PATCH] removed app context concurrent dictionary in favor of method lock object --- .../Context/ApplicationContext.cs | 55 ++++++++++--------- 1 file changed, 29 insertions(+), 26 deletions(-) diff --git a/Libraries/SPTarkov.Server.Core/Context/ApplicationContext.cs b/Libraries/SPTarkov.Server.Core/Context/ApplicationContext.cs index 59911333..9bcb4951 100644 --- a/Libraries/SPTarkov.Server.Core/Context/ApplicationContext.cs +++ b/Libraries/SPTarkov.Server.Core/Context/ApplicationContext.cs @@ -11,7 +11,8 @@ public class ApplicationContext private static ApplicationContext? _applicationContext; private readonly ISptLogger _logger; - private readonly ConcurrentDictionary values)> _variables = new(); + private readonly Dictionary> _variables = new(); + private readonly object _lockObject = new(); /// /// When ApplicationContext gets created by the DI container we store the singleton reference so we can provide it @@ -30,59 +31,61 @@ public class ApplicationContext public ContextVariable? GetLatestValue(ContextVariableType type) { - if (_variables.TryGetValue(type, out var savedValues)) + lock (_lockObject) { - lock (savedValues.lockObject) + if (_variables.TryGetValue(type, out var savedValues)) { - return savedValues.values.Last!.Value; + return savedValues.Last!.Value; } - } - return null; + return null; + } } public ICollection GetValues(ContextVariableType type) { - var values = new List(); - if (_variables.TryGetValue(type, out var savedValues)) + lock (_lockObject) { - lock (savedValues.lockObject) + var values = new List(); + if (_variables.TryGetValue(type, out var savedValues)) { - values.AddRange(savedValues.Item2); + values.AddRange(savedValues); } - } - return values; + return values; + } } public void AddValue(ContextVariableType type, object value) { - if (!_variables.TryGetValue(type, out var savedValues)) + lock (_lockObject) { - savedValues = new ValueTuple>(); - savedValues.lockObject = new Lock(); - savedValues.values = []; - if (!_variables.TryAdd(type, savedValues)) + if (!_variables.TryGetValue(type, out var savedValues)) { - _logger.Error($"Unable to add context variable type: {type}"); + savedValues = new LinkedList(); + if (!_variables.TryAdd(type, savedValues)) + { + _logger.Error($"Unable to add context variable type: {type}"); + } } - } - lock (savedValues.lockObject) - { - if (savedValues.values.Count >= MaxSavedValues) + if (savedValues.Count >= MaxSavedValues) { - savedValues.values.RemoveFirst(); + savedValues.RemoveFirst(); } - savedValues.values.AddLast(new ContextVariable(value, type)); + + savedValues.AddLast(new ContextVariable(value, type)); } } public void ClearValues(ContextVariableType type) { - if (!_variables.Remove(type, out _)) + lock (_lockObject) { - _logger.Error($"Unable to clear context variable type: {type}"); + if (!_variables.Remove(type, out _)) + { + _logger.Error($"Unable to clear context variable type: {type}"); + } } } }