From edad23460d1ed83ddbf985f41e3c66e2265904aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felipe=20Zipitr=C3=ADa?= <3012076+fzipi@users.noreply.github.com> Date: Tue, 31 Dec 2024 05:31:27 -0300 Subject: [PATCH] Revert "chore: Replace sync.Mutex with sync.Map" (#1262) Revert "chore: Replace sync.Mutex with sync.Map (#1197)" This reverts commit d5a0d6dd1db2a8a6bcb52e202ad37a454c7df3ca. --- internal/corazawaf/rule.go | 12 ++++++++---- internal/corazawaf/rule_test.go | 34 --------------------------------- 2 files changed, 8 insertions(+), 38 deletions(-) diff --git a/internal/corazawaf/rule.go b/internal/corazawaf/rule.go index bfc700f7b..4e04ff8de 100644 --- a/internal/corazawaf/rule.go +++ b/internal/corazawaf/rule.go @@ -586,18 +586,22 @@ func (r *Rule) AddVariableNegation(v variables.RuleVariable, key string) error { } var transformationIDToName = []string{""} -var transformationNameToID = sync.Map{} // map[string]int +var transformationNameToID = map[string]int{"": 0} +var transformationIDsLock = sync.Mutex{} func transformationID(currentID int, transformationName string) int { + transformationIDsLock.Lock() + defer transformationIDsLock.Unlock() + currName := transformationIDToName[currentID] nextName := fmt.Sprintf("%s+%s", currName, transformationName) - if id, ok := transformationNameToID.Load(nextName); ok { - return id.(int) + if id, ok := transformationNameToID[nextName]; ok { + return id } id := len(transformationIDToName) transformationIDToName = append(transformationIDToName, nextName) - transformationNameToID.Store(nextName, id) + transformationNameToID[nextName] = id return id } diff --git a/internal/corazawaf/rule_test.go b/internal/corazawaf/rule_test.go index f96eb4021..d999bb0c8 100644 --- a/internal/corazawaf/rule_test.go +++ b/internal/corazawaf/rule_test.go @@ -384,40 +384,6 @@ func TestAddTransformation(t *testing.T) { } } -func BenchmarkAddTransformationUnique(b *testing.B) { - transformation := func(input string) (string, bool, error) { - return "Test", true, nil - } - b.ResetTimer() - b.RunParallel(func(p *testing.PB) { - rule := NewRule() - for p.Next() { - transformationName := "transformation" + b.Name() - err := rule.AddTransformation(transformationName, transformation) - if err != nil { - b.Fatalf("Failed to add a transformation: %s", err.Error()) - } - } - }) -} - -func BenchmarkAddTransformationSame(b *testing.B) { - transformation := func(input string) (string, bool, error) { - return "Test", true, nil - } - b.ResetTimer() - b.RunParallel(func(p *testing.PB) { - for p.Next() { - rule := NewRule() - transformationName := "transformation" - err := rule.AddTransformation(transformationName, transformation) - if err != nil { - b.Fatalf("Failed to add a transformation: %s", err.Error()) - } - } - }) -} - func TestAddTransformationEmpty(t *testing.T) { rule := NewRule() transformationName := ""