Skip to content

Commit

Permalink
fix: concurrent SlugifyName access
Browse files Browse the repository at this point in the history
  • Loading branch information
mhotan committed May 22, 2024
1 parent 52acbd5 commit 3afa1c0
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
6 changes: 6 additions & 0 deletions applicationset/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"regexp"
"sort"
"strings"
"sync"
"text/template"
"unsafe"

Expand All @@ -26,6 +27,7 @@ import (
)

var sprigFuncMap = sprig.GenericFuncMap() // a singleton for better performance
var slugifyNameMutex sync.Mutex

func init() {
// Avoid allowing the user to learn things about the environment.
Expand Down Expand Up @@ -454,6 +456,10 @@ func NormalizeBitbucketBasePath(basePath string) string {
// Returns:
// - string: The generated URL-friendly slug based on the input name and options.
func SlugifyName(args ...interface{}) string {
// Necessary mutex due to global shared state.
// https://github.com/argoproj/argo-cd/issues/18369
slugifyNameMutex.Lock()
defer slugifyNameMutex.Unlock()
// Default values for arguments
maxSize := 50
EnableSmartTruncate := true
Expand Down
23 changes: 23 additions & 0 deletions applicationset/utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"os"
"path"
"sync"
"testing"
"time"

Expand Down Expand Up @@ -1278,6 +1279,28 @@ func TestSlugify(t *testing.T) {
result := SlugifyName(c.length, c.smartTruncate, c.branch)
assert.Equal(t, c.expectedBasePath, result, c.branch)
}

t.Run("Managed concurrent access", func(t *testing.T) {
const numberOfRoutines = 2

for x := 0; x < 100; x++ {
var wg sync.WaitGroup
wg.Add(numberOfRoutines)
for i := 0; i < numberOfRoutines; i++ {
go func(length int) {
defer wg.Done()
// Call the function concurrently
result := SlugifyName(length, false, "test")
if length == 1 {
assert.Equal(t, "t", result)
} else {
assert.Equal(t, "te", result)
}
}(i + 1)
}
wg.Wait()
}
})
}

func TestGetTLSConfig(t *testing.T) {
Expand Down

0 comments on commit 3afa1c0

Please sign in to comment.