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