diff --git a/README.md b/README.md index ec81f36..ac15bf7 100644 --- a/README.md +++ b/README.md @@ -185,7 +185,7 @@ services: example* // file name starts with "example" *example* // file name has "example" in the name example // file name is exactly the word "example" - // regex +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 `` starts from 0: ``` diff --git a/code/Infrastructure/Verticals/ContentBlocker/BlocklistProvider.cs b/code/Infrastructure/Verticals/ContentBlocker/BlocklistProvider.cs index c823fb6..e2934c0 100644 --- a/code/Infrastructure/Verticals/ContentBlocker/BlocklistProvider.cs +++ b/code/Infrastructure/Verticals/ContentBlocker/BlocklistProvider.cs @@ -1,4 +1,5 @@ -using System.Diagnostics; +using System.Collections.Concurrent; +using System.Diagnostics; using System.Text.RegularExpressions; using Common.Configuration.ContentBlocker; using Domain.Enums; @@ -15,9 +16,9 @@ public sealed class BlocklistProvider public BlocklistType BlocklistType { get; } - public List Patterns { get; } = []; + public ConcurrentBag Patterns { get; } = []; - public List Regexes { get; } = []; + public ConcurrentBag Regexes { get; } = []; public BlocklistProvider( ILogger logger, @@ -75,9 +76,18 @@ public sealed class BlocklistProvider long startTime = Stopwatch.GetTimestamp(); ParallelOptions options = new() { MaxDegreeOfParallelism = 5 }; + const string regexId = "regex:"; Parallel.ForEach(patterns, options, pattern => { + if (!pattern.StartsWith(regexId)) + { + Patterns.Add(pattern); + return; + } + + pattern = pattern[regexId.Length..]; + try { Regex regex = new(pattern, RegexOptions.Compiled); @@ -85,7 +95,7 @@ public sealed class BlocklistProvider } catch (ArgumentException) { - Patterns.Add(pattern); + _logger.LogWarning("invalid regex | {pattern}", pattern); } });