Skip to content

Commit

Permalink
fix SlugifyName concurrent access
Browse files Browse the repository at this point in the history
Signed-off-by: Mike Hotan <mike@union.ai>
  • Loading branch information
mhotan committed May 22, 2024
1 parent 52acbd5 commit 7ffc97c
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
1 change: 1 addition & 0 deletions USERS.md
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,7 @@ Currently, the following organizations are **officially** using Argo CD:
1. [UFirstGroup](https://www.ufirstgroup.com/en/)
1. [ungleich.ch](https://ungleich.ch/)
1. [Unifonic Inc](https://www.unifonic.com/)
1. [Union Inc](https://www.union.ai/)
1. [Universidad Mesoamericana](https://www.umes.edu.gt/)
1. [Upsider Inc.](https://up-sider.com/lp/)
1. [Urbantz](https://urbantz.com/)
Expand Down
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 7ffc97c

Please sign in to comment.