Memory management optimisation

24 bytes per Put < 256-4096 bytes per buffer allocation avoided (10-170x difference)
This commit is contained in:
2025-09-30 10:05:50 +01:00
parent 6d758276b5
commit 913264c0b7
4 changed files with 74 additions and 25 deletions
+13 -3
View File
@@ -5,6 +5,7 @@ import (
"compress/gzip"
"context"
"crypto/rand"
"crypto/subtle"
"encoding/base64"
"encoding/hex"
"fmt"
@@ -19,6 +20,15 @@ import (
"github.com/gorilla/sessions"
)
// constantTimeStringCompare performs a constant-time comparison of two strings
// to prevent timing attacks. Returns true if the strings are equal.
func constantTimeStringCompare(a, b string) bool {
if len(a) != len(b) {
return false
}
return subtle.ConstantTimeCompare([]byte(a), []byte(b)) == 1
}
// min returns the minimum of two integers.
// This is a utility function used throughout the session management code.
// Parameters:
@@ -1331,7 +1341,7 @@ func (sd *SessionData) SetAccessToken(token string) {
}
currentAccessToken := sd.getAccessTokenUnsafe()
if currentAccessToken == token {
if constantTimeStringCompare(currentAccessToken, token) {
return
}
sd.dirty = true
@@ -1547,7 +1557,7 @@ func (sd *SessionData) SetRefreshToken(token string) {
}
}
}
if currentRefreshToken == token {
if constantTimeStringCompare(currentRefreshToken, token) {
return
}
sd.dirty = true
@@ -2060,7 +2070,7 @@ func (sd *SessionData) SetIDToken(token string) {
return
}
currentIDToken := sd.getIDTokenUnsafe()
if currentIDToken == token {
if constantTimeStringCompare(currentIDToken, token) {
return
}
sd.dirty = true