Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIXED] Leaking memory on usage of getHash() #4329

Merged
merged 1 commit into from Jul 20, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
34 changes: 7 additions & 27 deletions server/events.go
Expand Up @@ -843,35 +843,15 @@ func getHash(name string) string {
return getHashSize(name, sysHashLen)
}

var nameToHashSize8 = sync.Map{}
var nameToHashSize6 = sync.Map{}

// Computes a hash for the given `name`. The result will be `size` characters long.
func getHashSize(name string, size int) string {
compute := func() string {
sha := sha256.New()
sha.Write([]byte(name))
b := sha.Sum(nil)
for i := 0; i < size; i++ {
b[i] = digits[int(b[i]%base)]
}
return string(b[:size])
}
var m *sync.Map
switch size {
case 8:
m = &nameToHashSize8
case 6:
m = &nameToHashSize6
default:
return compute()
}
if v, ok := m.Load(name); ok {
return v.(string)
}
h := compute()
m.Store(name, h)
return h
sha := sha256.New()
sha.Write([]byte(name))
b := sha.Sum(nil)
for i := 0; i < size; i++ {
b[i] = digits[int(b[i]%base)]
}
return string(b[:size])
}

// Returns the node name for this server which is a hash of the server name.
Expand Down