Skip to content

Commit

Permalink
Fixed a possible goroutine leak
Browse files Browse the repository at this point in the history
  • Loading branch information
Mihonarium authored Feb 1, 2021
1 parent d0f0459 commit e166576
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion examples/StreamTracksToSpotifyPlaylist/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,8 @@ func writeResult(song audd.RecognitionResult) {
return
}
req.Header.Set("Content-Type", "application/json")
_, err = spotifyClient.h.Do(req)
resp, err := spotifyClient.h.Do(req)
defer closeBody(resp)
if capture(err) {
return
}
Expand All @@ -241,6 +242,16 @@ func writeResult(song audd.RecognitionResult) {
fmt.Printf("Added a song to the playlist (%s - %s, %s)\n", song.Artist, song.Title, song.SongLink)
}

func closeBody(resp *http.Response) {
if resp == nil {
return
}
if resp.Body == nil {
return
}
_ = resp.Body.Close()
}

func capture(err error) bool {
if err == nil {
return false
Expand Down

0 comments on commit e166576

Please sign in to comment.