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 {
unix := before.Unix()
unixtime := before.Unix()
var queue []string
c.torrentsM.RLock()
for key, torrent := range c.torrents {
if torrent.LastAction < unix {
if torrent.LastAction < unixtime {
queue = append(queue, key)
}
}

View file

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