fixed faulty regex detection and concurrent data accessing (#16)

This commit is contained in:
Marius Nechifor
2024-11-28 23:05:29 +02:00
committed by GitHub
parent 53adb6c1c1
commit a5a54e324d
2 changed files with 15 additions and 5 deletions
+1 -1
View File
@@ -185,7 +185,7 @@ services:
example* // file name starts with "example" example* // file name starts with "example"
*example* // file name has "example" in the name *example* // file name has "example" in the name
example // file name is exactly the word "example" example // file name is exactly the word "example"
<ANY_REGEX> // regex regex:<ANY_REGEX> // regex that needs to be marked at the start of the line with "regex:"
``` ```
5. Multiple Sonarr/Radarr instances can be specified using this format, where `<NUMBER>` starts from 0: 5. Multiple Sonarr/Radarr instances can be specified using this format, where `<NUMBER>` starts from 0:
``` ```
@@ -1,4 +1,5 @@
using System.Diagnostics; using System.Collections.Concurrent;
using System.Diagnostics;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using Common.Configuration.ContentBlocker; using Common.Configuration.ContentBlocker;
using Domain.Enums; using Domain.Enums;
@@ -15,9 +16,9 @@ public sealed class BlocklistProvider
public BlocklistType BlocklistType { get; } public BlocklistType BlocklistType { get; }
public List<string> Patterns { get; } = []; public ConcurrentBag<string> Patterns { get; } = [];
public List<Regex> Regexes { get; } = []; public ConcurrentBag<Regex> Regexes { get; } = [];
public BlocklistProvider( public BlocklistProvider(
ILogger<BlocklistProvider> logger, ILogger<BlocklistProvider> logger,
@@ -75,9 +76,18 @@ public sealed class BlocklistProvider
long startTime = Stopwatch.GetTimestamp(); long startTime = Stopwatch.GetTimestamp();
ParallelOptions options = new() { MaxDegreeOfParallelism = 5 }; ParallelOptions options = new() { MaxDegreeOfParallelism = 5 };
const string regexId = "regex:";
Parallel.ForEach(patterns, options, pattern => Parallel.ForEach(patterns, options, pattern =>
{ {
if (!pattern.StartsWith(regexId))
{
Patterns.Add(pattern);
return;
}
pattern = pattern[regexId.Length..];
try try
{ {
Regex regex = new(pattern, RegexOptions.Compiled); Regex regex = new(pattern, RegexOptions.Compiled);
@@ -85,7 +95,7 @@ public sealed class BlocklistProvider
} }
catch (ArgumentException) catch (ArgumentException)
{ {
Patterns.Add(pattern); _logger.LogWarning("invalid regex | {pattern}", pattern);
} }
}); });