Simpler, more efficient way of deleting inactive torrents
This commit is contained in:
parent
e29fb21edb
commit
7fe6dc3b4e
5 changed files with 21 additions and 80 deletions
|
@ -171,6 +171,22 @@ func (c *Conn) DeleteTorrent(infohash string) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *Conn) PurgeInactiveTorrent(infohash string) error {
|
||||||
|
c.torrentsM.Lock()
|
||||||
|
defer c.torrentsM.Unlock()
|
||||||
|
|
||||||
|
torrent, exists := c.torrents[infohash]
|
||||||
|
if !exists {
|
||||||
|
return tracker.ErrTorrentDNE
|
||||||
|
}
|
||||||
|
|
||||||
|
if torrent.PeerCount() == 0 {
|
||||||
|
delete(c.torrents, infohash)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (c *Conn) PutUser(u *models.User) error {
|
func (c *Conn) PutUser(u *models.User) error {
|
||||||
c.usersM.Lock()
|
c.usersM.Lock()
|
||||||
defer c.usersM.Unlock()
|
defer c.usersM.Unlock()
|
||||||
|
@ -207,23 +223,3 @@ func (c *Conn) DeleteClient(peerID string) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Conn) PurgeInactiveTorrents(before time.Time) error {
|
|
||||||
unixtime := before.Unix()
|
|
||||||
var queue []string
|
|
||||||
|
|
||||||
c.torrentsM.RLock()
|
|
||||||
for key, torrent := range c.torrents {
|
|
||||||
if torrent.LastAction < unixtime && torrent.PeerCount() == 0 {
|
|
||||||
queue = append(queue, key)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
c.torrentsM.RUnlock()
|
|
||||||
|
|
||||||
c.torrentsM.Lock()
|
|
||||||
for _, key := range queue {
|
|
||||||
delete(c.torrents, key)
|
|
||||||
}
|
|
||||||
c.torrentsM.Unlock()
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
|
@ -1,53 +0,0 @@
|
||||||
// Copyright 2014 The Chihaya Authors. All rights reserved.
|
|
||||||
// Use of this source code is governed by the BSD 2-Clause license,
|
|
||||||
// which can be found in the LICENSE file.
|
|
||||||
|
|
||||||
package tracker
|
|
||||||
|
|
||||||
import (
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/golang/glog"
|
|
||||||
|
|
||||||
"github.com/chihaya/chihaya/config"
|
|
||||||
)
|
|
||||||
|
|
||||||
func purgeTorrents(p Pool, threshold time.Duration, interval time.Duration) {
|
|
||||||
for _ = range time.NewTicker(interval).C {
|
|
||||||
before := time.Now().Add(-threshold)
|
|
||||||
glog.V(0).Infof("Purging torrents before %s", before)
|
|
||||||
|
|
||||||
conn, err := p.Get()
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
glog.Error("Unable to get connection for a routine")
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
err = conn.PurgeInactiveTorrents(before)
|
|
||||||
if err != nil {
|
|
||||||
glog.Errorf("Error purging torrents: %s", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func StartPurgingRoutines(p Pool, cfg *config.DriverConfig) error {
|
|
||||||
if purgeThreshold := cfg.Params["purge_inactive"]; purgeThreshold != "" {
|
|
||||||
threshold, err := time.ParseDuration(purgeThreshold)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
interval := time.Minute
|
|
||||||
|
|
||||||
if purgeInterval := cfg.Params["purge_interval"]; purgeInterval != "" {
|
|
||||||
interval, err = time.ParseDuration(purgeInterval)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
go purgeTorrents(p, threshold, interval)
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
|
@ -9,7 +9,6 @@ package tracker
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/chihaya/chihaya/config"
|
"github.com/chihaya/chihaya/config"
|
||||||
"github.com/chihaya/chihaya/models"
|
"github.com/chihaya/chihaya/models"
|
||||||
|
@ -80,7 +79,7 @@ type Conn interface {
|
||||||
DeleteLeecher(infohash, peerkey string) error
|
DeleteLeecher(infohash, peerkey string) error
|
||||||
PutSeeder(infohash string, p *models.Peer) error
|
PutSeeder(infohash string, p *models.Peer) error
|
||||||
DeleteSeeder(infohash, peerkey string) error
|
DeleteSeeder(infohash, peerkey string) error
|
||||||
PurgeInactiveTorrents(before time.Time) error
|
PurgeInactiveTorrent(infohash string) error
|
||||||
|
|
||||||
// User interactions
|
// User interactions
|
||||||
FindUser(passkey string) (*models.User, error)
|
FindUser(passkey string) (*models.User, error)
|
||||||
|
|
|
@ -91,6 +91,10 @@ func (t *Tracker) ServeAnnounce(w http.ResponseWriter, r *http.Request, p httpro
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return http.StatusInternalServerError, err
|
return http.StatusInternalServerError, err
|
||||||
}
|
}
|
||||||
|
} else if torrent.PeerCount() == 0 {
|
||||||
|
// Rather than deleting the torrent explicitly, let the tracker driver
|
||||||
|
// ensure there are no race conditions.
|
||||||
|
conn.PurgeInactiveTorrent(torrent.Infohash)
|
||||||
}
|
}
|
||||||
|
|
||||||
resp := newAnnounceResponse(ann, user, torrent)
|
resp := newAnnounceResponse(ann, user, torrent)
|
||||||
|
|
|
@ -31,11 +31,6 @@ func NewTracker(cfg *config.Config) (*Tracker, error) {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
err = tracker.StartPurgingRoutines(tp, &cfg.Tracker)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
bc, err := backend.Open(&cfg.Backend)
|
bc, err := backend.Open(&cfg.Backend)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|
Loading…
Add table
Reference in a new issue