Skip to content

Commit

Permalink
add some judicious locking
Browse files Browse the repository at this point in the history
  • Loading branch information
semick-dev committed May 3, 2024
1 parent 0f10c38 commit f9d6098
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ public ModifiableRecordSession(RecordSession session, SanitizerDictionary saniti

public List<ResponseTransform> AdditionalTransforms { get; } = new List<ResponseTransform>();

public readonly object SanitizerLock = new object();

public List<string> AppliedSanitizers { get; set; } = new List<string>();
public List<string> ForRemoval { get; } = new List<string>();

Expand All @@ -44,13 +46,16 @@ public ModifiableRecordSession(RecordSession session, SanitizerDictionary saniti

public void ResetExtensions(SanitizerDictionary sanitizerDictionary)
{
AdditionalTransforms.Clear();
AppliedSanitizers = new List<string>();
AppliedSanitizers.AddRange(sanitizerDictionary.SessionSanitizers);
ForRemoval.Clear();

CustomMatcher = null;
Client = null;
lock (SanitizerLock)
{
AdditionalTransforms.Clear();
AppliedSanitizers = new List<string>();
AppliedSanitizers.AddRange(sanitizerDictionary.SessionSanitizers);
ForRemoval.Clear();

CustomMatcher = null;
Client = null;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ public class SanitizerDictionary
// apply only the sanitizers that have been registered at the global level
public List<string> SessionSanitizers = new List<string>();

public readonly object SessionSanitizerLock = new object();

public SanitizerDictionary() {
ResetSessionSanitizers();
}
Expand Down Expand Up @@ -693,20 +695,23 @@ public SanitizerDictionary() {
/// </summary>
public void ResetSessionSanitizers()
{
var expectedSanitizers = DefaultSanitizerList;

for (int i = 0; i < expectedSanitizers.Count; i++)
lock (SessionSanitizerLock)
{
var id = expectedSanitizers[i].Id;
var sanitizer = expectedSanitizers[i].Sanitizer;
var expectedSanitizers = DefaultSanitizerList;

if (!Sanitizers.ContainsKey(id))
for (int i = 0; i < expectedSanitizers.Count; i++)
{
_register(sanitizer, id);
var id = expectedSanitizers[i].Id;
var sanitizer = expectedSanitizers[i].Sanitizer;

if (!Sanitizers.ContainsKey(id))
{
_register(sanitizer, id);
}
}
}

SessionSanitizers = DefaultSanitizerList.Select(x => x.Id).ToList();
SessionSanitizers = DefaultSanitizerList.Select(x => x.Id).ToList();
}
}

/// <summary>
Expand Down Expand Up @@ -736,11 +741,19 @@ public List<RecordedTestSanitizer> GetSanitizers()
public List<RegisteredSanitizer> GetRegisteredSanitizers(ModifiableRecordSession session)
{
var sanitizers = new List<RegisteredSanitizer>();
foreach (var id in session.AppliedSanitizers)

lock (session.SanitizerLock)
{
if (Sanitizers.TryGetValue(id, out RegisteredSanitizer sanitizer))
foreach (var id in session.AppliedSanitizers)
{
sanitizers.Add(sanitizer);
if (Sanitizers.TryGetValue(id, out RegisteredSanitizer sanitizer))
{
sanitizers.Add(sanitizer);
}
else
{
DebugLogger.LogError($"Failed to get a sanitizer with id {id}");
}
}
}

Expand All @@ -754,11 +767,14 @@ public List<RegisteredSanitizer> GetRegisteredSanitizers(ModifiableRecordSession
public List<RegisteredSanitizer> GetRegisteredSanitizers()
{
var sanitizers = new List<RegisteredSanitizer>();
foreach (var id in SessionSanitizers)
lock (SessionSanitizerLock)
{
if (Sanitizers.TryGetValue(id, out RegisteredSanitizer sanitizer))
foreach (var id in SessionSanitizers)
{
sanitizers.Add(sanitizer);
if (Sanitizers.TryGetValue(id, out RegisteredSanitizer sanitizer))
{
sanitizers.Add(sanitizer);
}
}
}

Expand Down Expand Up @@ -788,10 +804,13 @@ public string Register(RecordedTestSanitizer sanitizer)
{
var strCurrent = IdFactory.GetNextId().ToString();

if (_register(sanitizer, strCurrent))
lock (SessionSanitizerLock)
{
SessionSanitizers.Add(strCurrent);
return strCurrent;
if (_register(sanitizer, strCurrent))
{
SessionSanitizers.Add(strCurrent);
return strCurrent;
}
}
throw new HttpException(System.Net.HttpStatusCode.InternalServerError, $"Unable to register global sanitizer id \"{strCurrent}\" with value '{JsonSerializer.Serialize(sanitizer)}'");
}
Expand All @@ -806,12 +825,17 @@ public string Register(RecordedTestSanitizer sanitizer)
public string Register(ModifiableRecordSession session, RecordedTestSanitizer sanitizer)
{
var strCurrent = IdFactory.GetNextId().ToString();
if (_register(sanitizer, strCurrent))

lock (session.SanitizerLock)
{
session.AppliedSanitizers.Add(strCurrent);
session.ForRemoval.Add(strCurrent);

return strCurrent;
if (_register(sanitizer, strCurrent))
{
session.AppliedSanitizers.Add(strCurrent);
session.ForRemoval.Add(strCurrent);

return strCurrent;
}
}

return string.Empty;
Expand All @@ -825,10 +849,13 @@ public string Register(ModifiableRecordSession session, RecordedTestSanitizer sa
/// <exception cref="HttpException"></exception>
public string Unregister(string sanitizerId)
{
if (SessionSanitizers.Contains(sanitizerId))
lock (SessionSanitizerLock)
{
SessionSanitizers.Remove(sanitizerId);
return sanitizerId;
if (SessionSanitizers.Contains(sanitizerId))
{
SessionSanitizers.Remove(sanitizerId);
return sanitizerId;
}
}

throw new HttpException(System.Net.HttpStatusCode.BadRequest, $"The requested sanitizer for removal \"{sanitizerId}\" is not active at the session level.");
Expand All @@ -843,10 +870,13 @@ public string Unregister(string sanitizerId)
/// <exception cref="HttpException"></exception>
public string Unregister(string sanitizerId, ModifiableRecordSession session)
{
if (session.AppliedSanitizers.Contains(sanitizerId))
lock (session.SanitizerLock)
{
session.AppliedSanitizers.Remove(sanitizerId);
return sanitizerId;
if (session.AppliedSanitizers.Contains(sanitizerId))
{
session.AppliedSanitizers.Remove(sanitizerId);
return sanitizerId;
}
}

throw new HttpException(System.Net.HttpStatusCode.BadRequest, $"The requested sanitizer for removal \"{sanitizerId}\" is not active on recording/playback with id \"{session.SessionId}\".");
Expand All @@ -869,7 +899,10 @@ public void Cleanup(ModifiableRecordSession session)
/// </summary>
public void Clear()
{
SessionSanitizers.Clear();
lock (SessionSanitizerLock)
{
SessionSanitizers.Clear();
}
}
}
}

0 comments on commit f9d6098

Please sign in to comment.