Skip to content

Commit

Permalink
Revert "chore: Replace sync.Mutex with sync.Map" (#1262)
Browse files Browse the repository at this point in the history
Revert "chore: Replace sync.Mutex with sync.Map (#1197)"

This reverts commit d5a0d6d.
  • Loading branch information
fzipi authored Dec 31, 2024
1 parent d5a0d6d commit edad234
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 38 deletions.
12 changes: 8 additions & 4 deletions internal/corazawaf/rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
34 changes: 0 additions & 34 deletions internal/corazawaf/rule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 := ""
Expand Down

0 comments on commit edad234

Please sign in to comment.