Initial torrent inactivity purging

This commit is contained in:
Justin Li 2014-07-16 13:24:44 -04:00
parent 7636608725
commit ea72e9e10c
4 changed files with 72 additions and 0 deletions

View file

@ -206,3 +206,24 @@ func (c *Conn) DeleteClient(peerID string) error {
return nil return nil
} }
func (c *Conn) PurgeInactiveTorrents(before time.Time) error {
unix := before.Unix()
var queue []string
c.torrentsM.RLock()
for key, torrent := range c.torrents {
if torrent.LastAction < unix {
queue = append(queue, key)
}
}
c.torrentsM.RUnlock()
c.torrentsM.Lock()
for _, key := range queue {
delete(c.torrents, key)
}
c.torrentsM.Unlock()
return nil
}

View file

@ -0,0 +1,44 @@
// 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) {
for _ = range time.NewTicker(time.Minute).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: ", err)
}
}
}
func StartPurgingRoutines(p Pool, cfg *config.DriverConfig) error {
if interval := cfg.Params["purge_after"]; interval != "" {
threshold, err := time.ParseDuration(interval)
if err != nil {
return err
}
go purgeTorrents(p, threshold)
}
return nil
}

View file

@ -9,6 +9,7 @@ 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"
@ -79,6 +80,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
// User interactions // User interactions
FindUser(passkey string) (*models.User, error) FindUser(passkey string) (*models.User, error)

View file

@ -31,6 +31,11 @@ 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