Defer closing connections to prevent leaking them

This commit is contained in:
Justin Li 2014-07-21 10:57:00 -04:00
parent af57763882
commit 7a849e6f20
2 changed files with 7 additions and 13 deletions

View file

@ -18,6 +18,8 @@ func (tkr *Tracker) HandleAnnounce(ann *models.Announce, w Writer) error {
return err
}
defer conn.Close()
if tkr.cfg.Whitelist {
err = conn.FindClient(ann.ClientID())
if err == ErrClientUnapproved {
@ -86,12 +88,7 @@ func (tkr *Tracker) HandleAnnounce(ann *models.Announce, w Writer) error {
conn.PurgeInactiveTorrent(torrent.Infohash)
}
err = w.WriteAnnounce(newAnnounceResponse(ann, peer, torrent))
if err != nil {
return err
}
return conn.Close()
return w.WriteAnnounce(newAnnounceResponse(ann, peer, torrent))
}
// updateSwarm handles the changes to a torrent's swarm given an announce.

View file

@ -8,12 +8,14 @@ import "github.com/chihaya/chihaya/tracker/models"
// HandleScrape encapsulates all the logic of handling a BitTorrent client's
// scrape without being coupled to any transport protocol.
func (tkr *Tracker) HandleScrape(scrape *models.Scrape, w Writer) error {
func (tkr *Tracker) HandleScrape(scrape *models.Scrape, w Writer) (err error) {
conn, err := tkr.Pool.Get()
if err != nil {
return err
}
defer conn.Close()
if tkr.cfg.Private {
_, err = conn.FindUser(scrape.Passkey)
if err == ErrUserDNE {
@ -36,10 +38,5 @@ func (tkr *Tracker) HandleScrape(scrape *models.Scrape, w Writer) error {
torrents = append(torrents, torrent)
}
err = w.WriteScrape(&models.ScrapeResponse{torrents})
if err != nil {
return err
}
return conn.Close()
return w.WriteScrape(&models.ScrapeResponse{torrents})
}