Browse Source

NetClientUtils: Avoid using defer in a for loop, this causes resource leaks.

[email protected] 2 years ago
parent
commit
a15650d3e0
1 changed files with 4 additions and 1 deletions
  1. 4 1
      netclient/ncutils/netclientutils.go

+ 4 - 1
netclient/ncutils/netclientutils.go

@@ -166,13 +166,16 @@ func GetPublicIP(api string) (string, error) {
 		if err != nil {
 			continue
 		}
-		defer resp.Body.Close()
 		if resp.StatusCode == http.StatusOK {
 			var bodyBytes []byte
 			bodyBytes, err = io.ReadAll(resp.Body)
 			if err != nil {
+				if resp.Body != nil {
+					_ = resp.Body.Close()
+				}
 				continue
 			}
+			_ = resp.Body.Close()
 			endpoint = string(bodyBytes)
 			break
 		}