Skip to content

Commit 1fb7faf

Browse files
authored
Merge pull request #23 from swiftwave-org/mergify/bp/v2/pr-22
fix: Close open http client after request ended (backport #22)
2 parents b5ed072 + 8c2e209 commit 1fb7faf

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

main.go

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -182,17 +182,28 @@ func sendStats(submissionEndpoint string, authorizationHeaderVal string, jsonDat
182182
if authorizationHeaderVal != "" {
183183
req.Header.Set("Authorization", authorizationHeaderVal)
184184
}
185+
186+
// create a client with custom transport
187+
transport := &http.Transport{}
188+
httpClient := &http.Client{
189+
Transport: transport,
190+
}
191+
185192
// send the request
186-
httpClient := &http.Client{}
187193
resp, err := httpClient.Do(req)
188194
if err != nil {
189-
fmt.Println("Error sending stats to endpoint: ", err.Error())
190-
fmt.Println("data tried to be sent: ", string(jsonData))
195+
fmt.Println("Error sending stats to endpoint:", err)
196+
fmt.Println("Data tried to be sent:", string(jsonData))
197+
transport.CloseIdleConnections() // make sure connections are released even on error
191198
return err
192199
}
200+
193201
// close the response body
194-
defer func(Body io.ReadCloser) {
195-
_ = Body.Close()
196-
}(resp.Body)
202+
defer resp.Body.Close()
203+
204+
// close any idle connections after the request
205+
transport.CloseIdleConnections()
206+
197207
return nil
198208
}
209+

0 commit comments

Comments
 (0)