Initial torrent inactivity purging
This commit is contained in:
parent
7636608725
commit
ea72e9e10c
4 changed files with 72 additions and 0 deletions
|
@ -206,3 +206,24 @@ func (c *Conn) DeleteClient(peerID string) error {
|
|||
|
||||
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
|
||||
}
|
||||
|
|
44
drivers/tracker/routines.go
Normal file
44
drivers/tracker/routines.go
Normal 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
|
||||
}
|
|
@ -9,6 +9,7 @@ package tracker
|
|||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/chihaya/chihaya/config"
|
||||
"github.com/chihaya/chihaya/models"
|
||||
|
@ -79,6 +80,7 @@ type Conn interface {
|
|||
DeleteLeecher(infohash, peerkey string) error
|
||||
PutSeeder(infohash string, p *models.Peer) error
|
||||
DeleteSeeder(infohash, peerkey string) error
|
||||
PurgeInactiveTorrents(before time.Time) error
|
||||
|
||||
// User interactions
|
||||
FindUser(passkey string) (*models.User, error)
|
||||
|
|
|
@ -31,6 +31,11 @@ func NewTracker(cfg *config.Config) (*Tracker, error) {
|
|||
return nil, err
|
||||
}
|
||||
|
||||
err = tracker.StartPurgingRoutines(tp, &cfg.Tracker)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
bc, err := backend.Open(&cfg.Backend)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
|
Loading…
Reference in a new issue