mirror of
https://github.com/lukaszraczylo/traefikoidc.git
synced 2026-10-13 04:07:37 +00:00
- TransportPool configKey: write conn-count ints verbatim (strconv) instead of byte() truncation — a config value >=256 previously truncated and made two distinct configs collide on one key, silently sharing a transport with the wrong settings (real fix, gosec G115). - Document design-intentional gosec findings with //nolint:gosec + rationale: G117 (clients_secret intentionally marshaled into stored creds JSON), G704 (tokenURL is the trusted discovery endpoint), G124 (delete/test cookie, Secure set conditionally), G118 (long-lived background refresh) and pool utils short-ID rune, testutil mock server redirects. Result: full-tree golangci-lint now reports 0 issues (was ~12). Build, vet, and the full test suite remain green.
71 lines
1.6 KiB
Go
71 lines
1.6 KiB
Go
// Package pool provides centralized memory pool management utilities
|
|
package pool
|
|
|
|
import (
|
|
"strings"
|
|
)
|
|
|
|
// BuildSessionName efficiently builds session names using pooled string builders
|
|
func BuildSessionName(baseName string, index int) string {
|
|
sb := StringBuilder()
|
|
defer ReturnStringBuilder(sb)
|
|
|
|
sb.WriteString(baseName)
|
|
sb.WriteRune('_')
|
|
// Efficient int to string conversion
|
|
if index < 10 {
|
|
sb.WriteRune('0' + rune(index)) //nolint:gosec // index is bounded by the short alphabet size
|
|
} else {
|
|
sb.WriteString(intToString(index))
|
|
}
|
|
|
|
return sb.String()
|
|
}
|
|
|
|
// BuildCacheKey efficiently builds cache keys using pooled string builders
|
|
func BuildCacheKey(parts ...string) string {
|
|
sb := StringBuilder()
|
|
defer ReturnStringBuilder(sb)
|
|
|
|
for i, part := range parts {
|
|
if i > 0 {
|
|
sb.WriteRune(':')
|
|
}
|
|
sb.WriteString(part)
|
|
}
|
|
|
|
return sb.String()
|
|
}
|
|
|
|
// FormatString efficiently formats a string using a pooled string builder
|
|
func FormatString(format func(*strings.Builder)) string {
|
|
sb := StringBuilder()
|
|
defer ReturnStringBuilder(sb)
|
|
format(sb)
|
|
return sb.String()
|
|
}
|
|
|
|
// intToString converts int to string without allocation (for small numbers)
|
|
func intToString(n int) string {
|
|
if n < 0 {
|
|
return "-" + intToString(-n)
|
|
}
|
|
if n < 10 {
|
|
return string(rune('0' + n))
|
|
}
|
|
if n < 100 {
|
|
return string(rune('0'+n/10)) + string(rune('0'+n%10))
|
|
}
|
|
// Fall back to standard conversion for larger numbers
|
|
buf := make([]byte, 0, 20)
|
|
for n > 0 {
|
|
buf = append(buf, byte('0'+n%10))
|
|
n /= 10
|
|
}
|
|
// Reverse the buffer
|
|
for i, j := 0, len(buf)-1; i < j; i, j = i+1, j-1 {
|
|
buf[i], buf[j] = buf[j], buf[i]
|
|
}
|
|
return string(buf)
|
|
}
|