Memory leak fixes and optimisations.

This commit is contained in:
2025-08-28 09:59:47 +01:00
parent c878784f1e
commit e2e2be53c1
6 changed files with 342 additions and 43 deletions
+8 -2
View File
@@ -140,10 +140,16 @@ func (t *TraefikOidc) exchangeTokens(ctx context.Context, grantType string, code
if err != nil {
return nil, fmt.Errorf("failed to exchange tokens: %w", err)
}
defer resp.Body.Close()
defer func() {
// Always drain the body before closing to ensure connection can be reused
io.Copy(io.Discard, resp.Body)
resp.Body.Close()
}()
if resp.StatusCode != http.StatusOK {
bodyBytes, _ := io.ReadAll(resp.Body)
// Limit body read to prevent memory issues
limitReader := io.LimitReader(resp.Body, 1024*10) // 10KB limit
bodyBytes, _ := io.ReadAll(limitReader)
return nil, fmt.Errorf("token endpoint returned status %d: %s", resp.StatusCode, string(bodyBytes))
}