Skip to content

Commit

Permalink
Fix possible race condition when generating analytics token
Browse files Browse the repository at this point in the history
Fixes #108
  • Loading branch information
const-cloudinary authored Jan 29, 2025
1 parent db54e9a commit 6f8c8c1
Showing 1 changed file with 9 additions and 11 deletions.
20 changes: 9 additions & 11 deletions asset/analytics.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
const binaryPadSize = 6

var charCodes = map[string]string{}
var analyticsSignature = ""
var initialize sync.Once

var mutex = &sync.Mutex{}
var analyticsSignature = ""

func sdkAnalyticsSignature() string {
if analyticsSignature != "" {
Expand Down Expand Up @@ -77,16 +77,14 @@ func encodeVersion(version string) (string, error) {
return strings.Join(encodedChars, ""), nil
}

func getKey(binaryValue string) string {
if len(charCodes) == 0 {
mutex.Lock()
if len(charCodes) == 0 {
for i, char := range chars {
charCodes[intToPaddedBin(i, binaryPadSize)] = string(char)
}
}
mutex.Unlock()
func initializeCharCodes() {
for i, char := range chars {
charCodes[intToPaddedBin(i, binaryPadSize)] = string(char)
}
}

func getKey(binaryValue string) string {
initialize.Do(initializeCharCodes)

return charCodes[binaryValue]
}
Expand Down

0 comments on commit 6f8c8c1

Please sign in to comment.