fix(filtering): use local converter for thread-safe compilation
- commit
- 0eecc37a138096273150bf0f17ceca64f05b84a0
- author
- bnema · · 2025-11-29 13:38 UTC
- parent
- 55e3159
- signature
- GPG Unverified
2B82207D370127A0The key is unknown
1 file · +8 -12
M internal/filtering/compiler.go +8 -12
| @@ -12,15 +12,11 @@ import ( | |||
| 12 | 12 | ) | |
| 13 | 13 | ||
| 14 | 14 | // DefaultFilterCompiler implements FilterCompiler using the converter package | |
| 15 | - | type DefaultFilterCompiler struct { | |
| 16 | - | converter *converter.FilterConverter | |
| 17 | - | } | |
| 15 | + | type DefaultFilterCompiler struct{} | |
| 18 | 16 | ||
| 19 | 17 | // NewDefaultFilterCompiler creates a new default filter compiler | |
| 20 | 18 | func NewDefaultFilterCompiler() *DefaultFilterCompiler { | |
| 21 | - | return &DefaultFilterCompiler{ | |
| 22 | - | converter: converter.NewFilterConverter(), | |
| 23 | - | } | |
| 19 | + | return &DefaultFilterCompiler{} | |
| 24 | 20 | } | |
| 25 | 21 | ||
| 26 | 22 | // CompileFromSources downloads and compiles filters from multiple URLs | |
| @@ -53,8 +49,8 @@ func (dfc *DefaultFilterCompiler) CompileFromSources(ctx context.Context, source | |||
| 53 | 49 | func (dfc *DefaultFilterCompiler) CompileFromData(data []byte) (*CompiledFilters, error) { | |
| 54 | 50 | compiled := NewCompiledFilters() | |
| 55 | 51 | ||
| 56 | - | // Reset converter for new compilation | |
| 57 | - | dfc.converter = converter.NewFilterConverter() | |
| 52 | + | // Use local converter for thread safety (avoid shared state) | |
| 53 | + | conv := converter.NewFilterConverter() | |
| 58 | 54 | ||
| 59 | 55 | // Parse line by line | |
| 60 | 56 | scanner := bufio.NewScanner(strings.NewReader(string(data))) | |
| @@ -65,7 +61,7 @@ func (dfc *DefaultFilterCompiler) CompileFromData(data []byte) (*CompiledFilters | |||
| 65 | 61 | lineCount++ | |
| 66 | 62 | ||
| 67 | 63 | // Convert each line | |
| 68 | - | if err := dfc.converter.ConvertEasyListLine(line); err != nil { | |
| 64 | + | if err := conv.ConvertEasyListLine(line); err != nil { | |
| 69 | 65 | logging.Debug(fmt.Sprintf("Failed to convert filter line: %s, error: %v", line, err)) | |
| 70 | 66 | continue | |
| 71 | 67 | } | |
| @@ -76,9 +72,9 @@ func (dfc *DefaultFilterCompiler) CompileFromData(data []byte) (*CompiledFilters | |||
| 76 | 72 | } | |
| 77 | 73 | ||
| 78 | 74 | // Get compiled rules from converter | |
| 79 | - | compiled.NetworkRules = dfc.converter.GetNetworkRules() | |
| 80 | - | compiled.CosmeticRules = dfc.converter.GetCosmeticRules() | |
| 81 | - | compiled.GenericHiding = dfc.converter.GetGenericHiding() | |
| 75 | + | compiled.NetworkRules = conv.GetNetworkRules() | |
| 76 | + | compiled.CosmeticRules = conv.GetCosmeticRules() | |
| 77 | + | compiled.GenericHiding = conv.GetGenericHiding() | |
| 82 | 78 | compiled.CompiledAt = time.Now() | |
| 83 | 79 | compiled.Version = fmt.Sprintf("data-compile-%d", time.Now().Unix()) | |
| 84 | 80 | ||
| @@ -12,15 +12,11 @@ import ( | |||||
| 12 | ) | 12 | ) | ||
| 13 | 13 | ||||
| 14 | // DefaultFilterCompiler implements FilterCompiler using the converter package | 14 | // DefaultFilterCompiler implements FilterCompiler using the converter package | ||
| 15 | - | type DefaultFilterCompiler struct { | 15 | + | type DefaultFilterCompiler struct{} |
| 16 | - | converter *converter.FilterConverter | |||
| 17 | - | } | |||
| 18 | 16 | ||||
| 19 | // NewDefaultFilterCompiler creates a new default filter compiler | 17 | // NewDefaultFilterCompiler creates a new default filter compiler | ||
| 20 | func NewDefaultFilterCompiler() *DefaultFilterCompiler { | 18 | func NewDefaultFilterCompiler() *DefaultFilterCompiler { | ||
| 21 | - | return &DefaultFilterCompiler{ | 19 | + | return &DefaultFilterCompiler{} |
| 22 | - | converter: converter.NewFilterConverter(), | |||
| 23 | - | } | |||
| 24 | } | 20 | } | ||
| 25 | 21 | ||||
| 26 | // CompileFromSources downloads and compiles filters from multiple URLs | 22 | // CompileFromSources downloads and compiles filters from multiple URLs | ||
| @@ -53,8 +49,8 @@ func (dfc *DefaultFilterCompiler) CompileFromSources(ctx context.Context, source | |||||
| 53 | func (dfc *DefaultFilterCompiler) CompileFromData(data []byte) (*CompiledFilters, error) { | 49 | func (dfc *DefaultFilterCompiler) CompileFromData(data []byte) (*CompiledFilters, error) { | ||
| 54 | compiled := NewCompiledFilters() | 50 | compiled := NewCompiledFilters() | ||
| 55 | 51 | ||||
| 56 | - | // Reset converter for new compilation | 52 | + | // Use local converter for thread safety (avoid shared state) |
| 57 | - | dfc.converter = converter.NewFilterConverter() | 53 | + | conv := converter.NewFilterConverter() |
| 58 | 54 | ||||
| 59 | // Parse line by line | 55 | // Parse line by line | ||
| 60 | scanner := bufio.NewScanner(strings.NewReader(string(data))) | 56 | scanner := bufio.NewScanner(strings.NewReader(string(data))) | ||
| @@ -65,7 +61,7 @@ func (dfc *DefaultFilterCompiler) CompileFromData(data []byte) (*CompiledFilters | |||||
| 65 | lineCount++ | 61 | lineCount++ | ||
| 66 | 62 | ||||
| 67 | // Convert each line | 63 | // Convert each line | ||
| 68 | - | if err := dfc.converter.ConvertEasyListLine(line); err != nil { | 64 | + | if err := conv.ConvertEasyListLine(line); err != nil { |
| 69 | logging.Debug(fmt.Sprintf("Failed to convert filter line: %s, error: %v", line, err)) | 65 | logging.Debug(fmt.Sprintf("Failed to convert filter line: %s, error: %v", line, err)) | ||
| 70 | continue | 66 | continue | ||
| 71 | } | 67 | } | ||
| @@ -76,9 +72,9 @@ func (dfc *DefaultFilterCompiler) CompileFromData(data []byte) (*CompiledFilters | |||||
| 76 | } | 72 | } | ||
| 77 | 73 | ||||
| 78 | // Get compiled rules from converter | 74 | // Get compiled rules from converter | ||
| 79 | - | compiled.NetworkRules = dfc.converter.GetNetworkRules() | 75 | + | compiled.NetworkRules = conv.GetNetworkRules() |
| 80 | - | compiled.CosmeticRules = dfc.converter.GetCosmeticRules() | 76 | + | compiled.CosmeticRules = conv.GetCosmeticRules() |
| 81 | - | compiled.GenericHiding = dfc.converter.GetGenericHiding() | 77 | + | compiled.GenericHiding = conv.GetGenericHiding() |
| 82 | compiled.CompiledAt = time.Now() | 78 | compiled.CompiledAt = time.Now() | ||
| 83 | compiled.Version = fmt.Sprintf("data-compile-%d", time.Now().Unix()) | 79 | compiled.Version = fmt.Sprintf("data-compile-%d", time.Now().Unix()) | ||
| 84 | 80 | ||||