Commit 780f9e1
Eric Bower
·
2025-12-15 23:01:27 -0500 EST
parent fbdea17
fix(pgs): memory leaks All changes replace defer with immediate Close() calls to ensure resources are released promptly rather than accumulating until function return.
1 files changed,
+5,
-9
+5,
-9
| ... | ... | @@ -51,10 +51,8 @@ func (h *ApiAssetHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { | |
| 51 | 51 | logger.Info("_redirects not found in lru cache", "key", redirectsCacheKey) | |
| 52 | 52 | redirectFp, redirectInfo, err := h.Cfg.Storage.GetObject(h.Bucket, filepath.Join(h.ProjectDir, "_redirects")) | |
| 53 | 53 | if err == nil { | |
| 54 | - | defer func() { | |
| 55 | - | _ = redirectFp.Close() | |
| 56 | - | }() | |
| 57 | 54 | if redirectInfo != nil && redirectInfo.Size > h.Cfg.MaxSpecialFileSize { | |
| 55 | + | _ = redirectFp.Close() | |
| 58 | 56 | errMsg := fmt.Sprintf("_redirects file is too large (%d > %d)", redirectInfo.Size, h.Cfg.MaxSpecialFileSize) | |
| 59 | 57 | logger.Error(errMsg) | |
| 60 | 58 | http.Error(w, errMsg, http.StatusInternalServerError) |
| ... | ... | @@ -63,6 +61,7 @@ func (h *ApiAssetHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { | |
| 63 | 61 | buf := new(strings.Builder) | |
| 64 | 62 | lr := io.LimitReader(redirectFp, h.Cfg.MaxSpecialFileSize) | |
| 65 | 63 | _, err := io.Copy(buf, lr) | |
| 64 | + | _ = redirectFp.Close() | |
| 66 | 65 | if err != nil { | |
| 67 | 66 | logger.Error("io copy", "err", err.Error()) | |
| 68 | 67 | http.Error(w, "cannot read _redirects file", http.StatusInternalServerError) |
| ... | ... | @@ -109,9 +108,7 @@ func (h *ApiAssetHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { | |
| 109 | 108 | if err != nil { | |
| 110 | 109 | continue | |
| 111 | 110 | } | |
| 112 | - | defer func() { | |
| 113 | - | _ = obj.Close() | |
| 114 | - | }() | |
| 111 | + | _ = obj.Close() | |
| 115 | 112 | } | |
| 116 | 113 | logger.Info( | |
| 117 | 114 | "redirecting request", |
| ... | ... | @@ -219,10 +216,8 @@ func (h *ApiAssetHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { | |
| 219 | 216 | logger.Info("_headers not found in lru cache", "key", headersCacheKey) | |
| 220 | 217 | headersFp, headersInfo, err := h.Cfg.Storage.GetObject(h.Bucket, filepath.Join(h.ProjectDir, "_headers")) | |
| 221 | 218 | if err == nil { | |
| 222 | - | defer func() { | |
| 223 | - | _ = headersFp.Close() | |
| 224 | - | }() | |
| 225 | 219 | if headersInfo != nil && headersInfo.Size > h.Cfg.MaxSpecialFileSize { | |
| 220 | + | _ = headersFp.Close() | |
| 226 | 221 | errMsg := fmt.Sprintf("_headers file is too large (%d > %d)", headersInfo.Size, h.Cfg.MaxSpecialFileSize) | |
| 227 | 222 | logger.Error(errMsg) | |
| 228 | 223 | http.Error(w, errMsg, http.StatusInternalServerError) |
| ... | ... | @@ -231,6 +226,7 @@ func (h *ApiAssetHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { | |
| 231 | 226 | buf := new(strings.Builder) | |
| 232 | 227 | lr := io.LimitReader(headersFp, h.Cfg.MaxSpecialFileSize) | |
| 233 | 228 | _, err := io.Copy(buf, lr) | |
| 229 | + | _ = headersFp.Close() | |
| 234 | 230 | if err != nil { | |
| 235 | 231 | logger.Error("io copy", "err", err.Error()) | |
| 236 | 232 | http.Error(w, "cannot read _headers file", http.StatusInternalServerError) |