close tracker pool connections & some style changes
This commit is contained in:
parent
e78dc74990
commit
6b260b7352
5 changed files with 31 additions and 22 deletions
|
@ -12,13 +12,13 @@ import (
|
|||
|
||||
// HandleAnnounce encapsulates all of the logic of handling a BitTorrent
|
||||
// client's Announce without being coupled to any transport protocol.
|
||||
func (t *Tracker) HandleAnnounce(ann *models.Announce, w Writer) error {
|
||||
conn, err := t.Pool.Get()
|
||||
func (tkr *Tracker) HandleAnnounce(ann *models.Announce, w Writer) error {
|
||||
conn, err := tkr.Pool.Get()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if t.cfg.Whitelist {
|
||||
if tkr.cfg.Whitelist {
|
||||
err = conn.FindClient(ann.ClientID())
|
||||
if err == ErrClientUnapproved {
|
||||
w.WriteError(err)
|
||||
|
@ -29,7 +29,7 @@ func (t *Tracker) HandleAnnounce(ann *models.Announce, w Writer) error {
|
|||
}
|
||||
|
||||
var user *models.User
|
||||
if t.cfg.Private {
|
||||
if tkr.cfg.Private {
|
||||
user, err = conn.FindUser(ann.Passkey)
|
||||
if err == ErrUserDNE {
|
||||
w.WriteError(err)
|
||||
|
@ -42,7 +42,7 @@ func (t *Tracker) HandleAnnounce(ann *models.Announce, w Writer) error {
|
|||
var torrent *models.Torrent
|
||||
torrent, err = conn.FindTorrent(ann.Infohash)
|
||||
switch {
|
||||
case !t.cfg.Private && err == ErrTorrentDNE:
|
||||
case !tkr.cfg.Private && err == ErrTorrentDNE:
|
||||
torrent = &models.Torrent{
|
||||
Infohash: ann.Infohash,
|
||||
Seeders: models.PeerMap{},
|
||||
|
@ -54,7 +54,7 @@ func (t *Tracker) HandleAnnounce(ann *models.Announce, w Writer) error {
|
|||
return err
|
||||
}
|
||||
|
||||
case t.cfg.Private && err == ErrTorrentDNE:
|
||||
case tkr.cfg.Private && err == ErrTorrentDNE:
|
||||
w.WriteError(err)
|
||||
return nil
|
||||
|
||||
|
@ -74,19 +74,24 @@ func (t *Tracker) HandleAnnounce(ann *models.Announce, w Writer) error {
|
|||
return err
|
||||
}
|
||||
|
||||
if t.cfg.Private {
|
||||
if tkr.cfg.Private {
|
||||
delta := models.NewAnnounceDelta(ann, peer, user, torrent, created, snatched)
|
||||
err = t.backend.RecordAnnounce(delta)
|
||||
err = tkr.backend.RecordAnnounce(delta)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
} else if t.cfg.PurgeInactiveTorrents && torrent.PeerCount() == 0 {
|
||||
} else if tkr.cfg.PurgeInactiveTorrents && torrent.PeerCount() == 0 {
|
||||
// Rather than deleting the torrent explicitly, let the tracker driver
|
||||
// ensure there are no race conditions.
|
||||
conn.PurgeInactiveTorrent(torrent.Infohash)
|
||||
}
|
||||
|
||||
return w.WriteAnnounce(newAnnounceResponse(ann, peer, torrent))
|
||||
err = w.WriteAnnounce(newAnnounceResponse(ann, peer, torrent))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return conn.Close()
|
||||
}
|
||||
|
||||
// updateSwarm handles the changes to a torrent's swarm given an announce.
|
||||
|
@ -115,6 +120,7 @@ func updateSwarm(c Conn, ann *models.Announce, p *models.Peer, t *models.Torrent
|
|||
return
|
||||
}
|
||||
t.Seeders[p.ID] = *p
|
||||
|
||||
} else {
|
||||
err = c.PutLeecher(t.Infohash, p)
|
||||
if err != nil {
|
||||
|
@ -148,8 +154,6 @@ func handleEvent(c Conn, ann *models.Announce, p *models.Peer, u *models.User, t
|
|||
}
|
||||
|
||||
case ann.Event == "completed":
|
||||
snatched = true
|
||||
|
||||
err = c.IncrementTorrentSnatches(t.Infohash)
|
||||
if err != nil {
|
||||
return
|
||||
|
@ -170,6 +174,7 @@ func handleEvent(c Conn, ann *models.Announce, p *models.Peer, u *models.User, t
|
|||
return
|
||||
}
|
||||
}
|
||||
snatched = true
|
||||
|
||||
case t.InLeecherPool(p) && ann.Left == 0:
|
||||
// A leecher completed but the event was never received.
|
||||
|
|
|
@ -54,8 +54,7 @@ func Open(cfg *config.DriverConfig) (Pool, error) {
|
|||
cfg.Name,
|
||||
)
|
||||
}
|
||||
pool := driver.New(cfg)
|
||||
return pool, nil
|
||||
return driver.New(cfg), nil
|
||||
}
|
||||
|
||||
// Pool represents a thread-safe pool of connections to the data store
|
||||
|
|
|
@ -14,7 +14,7 @@ import (
|
|||
|
||||
type driver struct{}
|
||||
|
||||
func (d *driver) New(conf *config.DriverConfig) tracker.Pool {
|
||||
func (d *driver) New(cfg *config.DriverConfig) tracker.Pool {
|
||||
return &Pool{
|
||||
users: make(map[string]*models.User),
|
||||
torrents: make(map[string]*models.Torrent),
|
||||
|
|
|
@ -4,19 +4,17 @@
|
|||
|
||||
package tracker
|
||||
|
||||
import (
|
||||
"github.com/chihaya/chihaya/tracker/models"
|
||||
)
|
||||
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 (t *Tracker) HandleScrape(scrape *models.Scrape, w Writer) error {
|
||||
conn, err := t.Pool.Get()
|
||||
func (tkr *Tracker) HandleScrape(scrape *models.Scrape, w Writer) error {
|
||||
conn, err := tkr.Pool.Get()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if t.cfg.Private {
|
||||
if tkr.cfg.Private {
|
||||
_, err = conn.FindUser(scrape.Passkey)
|
||||
if err == ErrUserDNE {
|
||||
w.WriteError(err)
|
||||
|
@ -38,5 +36,10 @@ func (t *Tracker) HandleScrape(scrape *models.Scrape, w Writer) error {
|
|||
torrents = append(torrents, torrent)
|
||||
}
|
||||
|
||||
return w.WriteScrape(&models.ScrapeResponse{torrents})
|
||||
err = w.WriteScrape(&models.ScrapeResponse{torrents})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return conn.Close()
|
||||
}
|
||||
|
|
|
@ -102,5 +102,7 @@ func purgeInactivePeers(p Pool, purgeEmptyTorrents bool, threshold, interval tim
|
|||
if err != nil {
|
||||
glog.Errorf("Error purging torrents: %s", err)
|
||||
}
|
||||
|
||||
conn.Close()
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue