Skip to content

Commit

Permalink
mutex
Browse files Browse the repository at this point in the history
  • Loading branch information
TiraelSedai committed Oct 14, 2024
1 parent 08bce88 commit 1650a03
Showing 1 changed file with 18 additions and 9 deletions.
27 changes: 18 additions & 9 deletions ClubDoorman/BadMessageManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace ClubDoorman;
internal sealed class BadMessageManager
{
private const string Path = "data/bad-messages.txt";
private readonly SemaphoreSlim _semaphore = new(1);
private readonly SemaphoreSlim _fileLock = new(1);

// with our data size, we would never need O(1), in fact if this ever bloats I'd rather limit this to ~2048 last messages
// space savings are very moderate, base64 string takes up ~128 bytes while byte[] takes only 64, but then again it's
Expand All @@ -15,22 +15,31 @@ internal sealed class BadMessageManager

public BadMessageManager()
{
foreach (var item in File.ReadAllLines(Path))
_bad.Add(Convert.FromBase64String(item));
lock (_bad)
{
foreach (var item in File.ReadAllLines(Path))
_bad.Add(Convert.FromBase64String(item));
}
}

public bool KnownBadMessage(string message) => _bad.Contains(ComputeHash(message));
public bool KnownBadMessage(string message)
{
lock (_bad)
return _bad.Contains(ComputeHash(message));
}

public async ValueTask MarkAsBad(string message)
{
if (string.IsNullOrWhiteSpace(message))
return;
var hash = ComputeHash(message);
if (_bad.Add(hash))
{
using var token = await SemaphoreHelper.AwaitAsync(_semaphore);
await File.AppendAllLinesAsync(Path, [Convert.ToBase64String(hash)]);
}
bool added;
lock (_bad)
added = _bad.Add(hash);
if (!added)
return;
using var token = await SemaphoreHelper.AwaitAsync(_fileLock);
await File.AppendAllLinesAsync(Path, [Convert.ToBase64String(hash)]);
}

private static byte[] ComputeHash(string message) => SHA512.HashData(Encoding.UTF8.GetBytes(message));
Expand Down

0 comments on commit 1650a03

Please sign in to comment.