Skip to content

Commit a6b435f

Browse files
authored
Revert "chore: Replace sync.Mutex with sync.Map (#1197)"
This reverts commit d5a0d6d.
1 parent d5a0d6d commit a6b435f

File tree

2 files changed

+8
-38
lines changed

2 files changed

+8
-38
lines changed

Diff for: internal/corazawaf/rule.go

+8-4
Original file line numberDiff line numberDiff line change
@@ -586,18 +586,22 @@ func (r *Rule) AddVariableNegation(v variables.RuleVariable, key string) error {
586586
}
587587

588588
var transformationIDToName = []string{""}
589-
var transformationNameToID = sync.Map{} // map[string]int
589+
var transformationNameToID = map[string]int{"": 0}
590+
var transformationIDsLock = sync.Mutex{}
590591

591592
func transformationID(currentID int, transformationName string) int {
593+
transformationIDsLock.Lock()
594+
defer transformationIDsLock.Unlock()
595+
592596
currName := transformationIDToName[currentID]
593597
nextName := fmt.Sprintf("%s+%s", currName, transformationName)
594-
if id, ok := transformationNameToID.Load(nextName); ok {
595-
return id.(int)
598+
if id, ok := transformationNameToID[nextName]; ok {
599+
return id
596600
}
597601

598602
id := len(transformationIDToName)
599603
transformationIDToName = append(transformationIDToName, nextName)
600-
transformationNameToID.Store(nextName, id)
604+
transformationNameToID[nextName] = id
601605
return id
602606
}
603607

Diff for: internal/corazawaf/rule_test.go

-34
Original file line numberDiff line numberDiff line change
@@ -384,40 +384,6 @@ func TestAddTransformation(t *testing.T) {
384384
}
385385
}
386386

387-
func BenchmarkAddTransformationUnique(b *testing.B) {
388-
transformation := func(input string) (string, bool, error) {
389-
return "Test", true, nil
390-
}
391-
b.ResetTimer()
392-
b.RunParallel(func(p *testing.PB) {
393-
rule := NewRule()
394-
for p.Next() {
395-
transformationName := "transformation" + b.Name()
396-
err := rule.AddTransformation(transformationName, transformation)
397-
if err != nil {
398-
b.Fatalf("Failed to add a transformation: %s", err.Error())
399-
}
400-
}
401-
})
402-
}
403-
404-
func BenchmarkAddTransformationSame(b *testing.B) {
405-
transformation := func(input string) (string, bool, error) {
406-
return "Test", true, nil
407-
}
408-
b.ResetTimer()
409-
b.RunParallel(func(p *testing.PB) {
410-
for p.Next() {
411-
rule := NewRule()
412-
transformationName := "transformation"
413-
err := rule.AddTransformation(transformationName, transformation)
414-
if err != nil {
415-
b.Fatalf("Failed to add a transformation: %s", err.Error())
416-
}
417-
}
418-
})
419-
}
420-
421387
func TestAddTransformationEmpty(t *testing.T) {
422388
rule := NewRule()
423389
transformationName := ""

0 commit comments

Comments
 (0)