Allow purge interval to be set, and fix variable naming

This commit is contained in:
Justin Li 2014-07-16 13:54:10 -04:00
parent 84e1c169c0
commit 1236a5ee5b
2 changed files with 16 additions and 7 deletions

View file

@ -208,12 +208,12 @@ func (c *Conn) DeleteClient(peerID string) error {
} }
func (c *Conn) PurgeInactiveTorrents(before time.Time) error { func (c *Conn) PurgeInactiveTorrents(before time.Time) error {
unix := before.Unix() unixtime := before.Unix()
var queue []string var queue []string
c.torrentsM.RLock() c.torrentsM.RLock()
for key, torrent := range c.torrents { for key, torrent := range c.torrents {
if torrent.LastAction < unix { if torrent.LastAction < unixtime {
queue = append(queue, key) queue = append(queue, key)
} }
} }

View file

@ -12,8 +12,8 @@ import (
"github.com/chihaya/chihaya/config" "github.com/chihaya/chihaya/config"
) )
func purgeTorrents(p Pool, threshold time.Duration) { func purgeTorrents(p Pool, threshold time.Duration, interval time.Duration) {
for _ = range time.NewTicker(time.Minute).C { for _ = range time.NewTicker(interval).C {
before := time.Now().Add(-threshold) before := time.Now().Add(-threshold)
glog.V(0).Infof("Purging torrents before %s", before) glog.V(0).Infof("Purging torrents before %s", before)
@ -32,13 +32,22 @@ func purgeTorrents(p Pool, threshold time.Duration) {
} }
func StartPurgingRoutines(p Pool, cfg *config.DriverConfig) error { func StartPurgingRoutines(p Pool, cfg *config.DriverConfig) error {
if interval := cfg.Params["purge_after"]; interval != "" { if purgeThreshold := cfg.Params["purge_inactive"]; purgeThreshold != "" {
threshold, err := time.ParseDuration(interval) threshold, err := time.ParseDuration(purgeThreshold)
if err != nil { if err != nil {
return err return err
} }
go purgeTorrents(p, threshold) 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 return nil
} }