Purge peers that have not announced for twice the announce interval

This commit is contained in:
Justin Li
2014-07-16 19:38:51 -04:00
parent 7fe6dc3b4e
commit cd6204f13d
6 changed files with 97 additions and 4 deletions
+45
View File
@@ -5,6 +5,7 @@
package memory
import (
"runtime"
"time"
"github.com/chihaya/chihaya/drivers/tracker"
@@ -223,3 +224,47 @@ func (c *Conn) DeleteClient(peerID string) error {
return nil
}
func (c *Conn) PurgeInactivePeers(purgeEmptyTorrents bool, before time.Time) error {
unixtime := before.Unix()
// Build array of map keys to operate on.
c.torrentsM.RLock()
index := 0
keys := make([]string, len(c.torrents))
for infohash, _ := range c.torrents {
keys[index] = infohash
index++
}
c.torrentsM.RUnlock()
// Process keys.
for _, infohash := range keys {
runtime.Gosched() // Let other goroutines run, since this is low priority.
c.torrentsM.Lock()
torrent := c.torrents[infohash]
for key, peer := range torrent.Seeders {
if peer.LastAnnounce < unixtime {
delete(torrent.Seeders, key)
}
}
for key, peer := range torrent.Leechers {
if peer.LastAnnounce < unixtime {
delete(torrent.Leechers, key)
}
}
peers := torrent.PeerCount()
c.torrentsM.Unlock()
if purgeEmptyTorrents && peers == 0 {
c.PurgeInactiveTorrent(infohash)
}
}
return nil
}
+30
View File
@@ -0,0 +1,30 @@
// 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"
)
func PurgeInactivePeers(p Pool, purgeEmptyTorrents bool, threshold, interval time.Duration) {
for _ = range time.NewTicker(interval).C {
before := time.Now().Add(-threshold)
glog.V(0).Infof("Purging peers with no announces since %s", before)
conn, err := p.Get()
if err != nil {
glog.Error("Unable to get connection for a routine")
continue
}
err = conn.PurgeInactivePeers(purgeEmptyTorrents, before)
if err != nil {
glog.Errorf("Error purging torrents: %s", err)
}
}
}
+3
View File
@@ -9,6 +9,7 @@ package tracker
import (
"errors"
"fmt"
"time"
"github.com/chihaya/chihaya/config"
"github.com/chihaya/chihaya/models"
@@ -79,7 +80,9 @@ type Conn interface {
DeleteLeecher(infohash, peerkey string) error
PutSeeder(infohash string, p *models.Peer) error
DeleteSeeder(infohash, peerkey string) error
PurgeInactiveTorrent(infohash string) error
PurgeInactivePeers(purgeEmptyTorrents bool, before time.Time) error
// User interactions
FindUser(passkey string) (*models.User, error)