From ea72e9e10cb03d3ab376430429dde6e05f0c139b Mon Sep 17 00:00:00 2001 From: Justin Li Date: Wed, 16 Jul 2014 13:24:44 -0400 Subject: [PATCH] Initial torrent inactivity purging --- drivers/tracker/memory/conn.go | 21 ++++++++++++++++ drivers/tracker/routines.go | 44 ++++++++++++++++++++++++++++++++++ drivers/tracker/tracker.go | 2 ++ http/http.go | 5 ++++ 4 files changed, 72 insertions(+) create mode 100644 drivers/tracker/routines.go diff --git a/drivers/tracker/memory/conn.go b/drivers/tracker/memory/conn.go index dbec472..6a1bfee 100644 --- a/drivers/tracker/memory/conn.go +++ b/drivers/tracker/memory/conn.go @@ -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 +} diff --git a/drivers/tracker/routines.go b/drivers/tracker/routines.go new file mode 100644 index 0000000..d3c52f9 --- /dev/null +++ b/drivers/tracker/routines.go @@ -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 +} diff --git a/drivers/tracker/tracker.go b/drivers/tracker/tracker.go index c61152d..d1937d1 100644 --- a/drivers/tracker/tracker.go +++ b/drivers/tracker/tracker.go @@ -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) diff --git a/http/http.go b/http/http.go index 8dd3f98..ee9a48d 100644 --- a/http/http.go +++ b/http/http.go @@ -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