Purge peers that have not announced for twice the announce interval
This commit is contained in:
parent
7fe6dc3b4e
commit
cd6204f13d
6 changed files with 97 additions and 4 deletions
|
@ -50,6 +50,8 @@ type Config struct {
|
||||||
Freeleech bool `json:"freeleech"`
|
Freeleech bool `json:"freeleech"`
|
||||||
Whitelist bool `json:"whitelist"`
|
Whitelist bool `json:"whitelist"`
|
||||||
|
|
||||||
|
PurgeInactiveTorrents bool `json:"purge_inactive_torrents"`
|
||||||
|
|
||||||
Announce Duration `json:"announce"`
|
Announce Duration `json:"announce"`
|
||||||
MinAnnounce Duration `json:"min_announce"`
|
MinAnnounce Duration `json:"min_announce"`
|
||||||
RequestTimeout Duration `json:"request_timeout"`
|
RequestTimeout Duration `json:"request_timeout"`
|
||||||
|
@ -59,15 +61,21 @@ type Config struct {
|
||||||
// DefaultConfig is a configuration that can be used as a fallback value.
|
// DefaultConfig is a configuration that can be used as a fallback value.
|
||||||
var DefaultConfig = Config{
|
var DefaultConfig = Config{
|
||||||
Addr: "127.0.0.1:6881",
|
Addr: "127.0.0.1:6881",
|
||||||
|
|
||||||
Tracker: DriverConfig{
|
Tracker: DriverConfig{
|
||||||
Name: "memory",
|
Name: "memory",
|
||||||
},
|
},
|
||||||
|
|
||||||
Backend: DriverConfig{
|
Backend: DriverConfig{
|
||||||
Name: "noop",
|
Name: "noop",
|
||||||
},
|
},
|
||||||
Private: false,
|
|
||||||
Freeleech: false,
|
Private: false,
|
||||||
Whitelist: false,
|
Freeleech: false,
|
||||||
|
Whitelist: false,
|
||||||
|
|
||||||
|
PurgeInactiveTorrents: true,
|
||||||
|
|
||||||
Announce: Duration{30 * time.Minute},
|
Announce: Duration{30 * time.Minute},
|
||||||
MinAnnounce: Duration{15 * time.Minute},
|
MinAnnounce: Duration{15 * time.Minute},
|
||||||
RequestTimeout: Duration{10 * time.Second},
|
RequestTimeout: Duration{10 * time.Second},
|
||||||
|
|
|
@ -5,6 +5,7 @@
|
||||||
package memory
|
package memory
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"runtime"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/chihaya/chihaya/drivers/tracker"
|
"github.com/chihaya/chihaya/drivers/tracker"
|
||||||
|
@ -223,3 +224,47 @@ func (c *Conn) DeleteClient(peerID string) error {
|
||||||
return nil
|
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
drivers/tracker/routines.go
Normal file
30
drivers/tracker/routines.go
Normal 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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -9,6 +9,7 @@ package tracker
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/chihaya/chihaya/config"
|
"github.com/chihaya/chihaya/config"
|
||||||
"github.com/chihaya/chihaya/models"
|
"github.com/chihaya/chihaya/models"
|
||||||
|
@ -79,7 +80,9 @@ type Conn interface {
|
||||||
DeleteLeecher(infohash, peerkey string) error
|
DeleteLeecher(infohash, peerkey string) error
|
||||||
PutSeeder(infohash string, p *models.Peer) error
|
PutSeeder(infohash string, p *models.Peer) error
|
||||||
DeleteSeeder(infohash, peerkey string) error
|
DeleteSeeder(infohash, peerkey string) error
|
||||||
|
|
||||||
PurgeInactiveTorrent(infohash string) error
|
PurgeInactiveTorrent(infohash string) error
|
||||||
|
PurgeInactivePeers(purgeEmptyTorrents bool, before time.Time) error
|
||||||
|
|
||||||
// User interactions
|
// User interactions
|
||||||
FindUser(passkey string) (*models.User, error)
|
FindUser(passkey string) (*models.User, error)
|
||||||
|
|
|
@ -91,7 +91,7 @@ func (t *Tracker) ServeAnnounce(w http.ResponseWriter, r *http.Request, p httpro
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return http.StatusInternalServerError, err
|
return http.StatusInternalServerError, err
|
||||||
}
|
}
|
||||||
} else if torrent.PeerCount() == 0 {
|
} else if t.cfg.PurgeInactiveTorrents && torrent.PeerCount() == 0 {
|
||||||
// Rather than deleting the torrent explicitly, let the tracker driver
|
// Rather than deleting the torrent explicitly, let the tracker driver
|
||||||
// ensure there are no race conditions.
|
// ensure there are no race conditions.
|
||||||
conn.PurgeInactiveTorrent(torrent.Infohash)
|
conn.PurgeInactiveTorrent(torrent.Infohash)
|
||||||
|
|
|
@ -99,6 +99,13 @@ func Serve(cfg *config.Config) {
|
||||||
glog.Fatal("New: ", err)
|
glog.Fatal("New: ", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
go tracker.PurgeInactivePeers(
|
||||||
|
t.pool,
|
||||||
|
cfg.PurgeInactiveTorrents,
|
||||||
|
cfg.Announce.Duration*2,
|
||||||
|
cfg.Announce.Duration,
|
||||||
|
)
|
||||||
|
|
||||||
glog.V(0).Info("Starting on ", cfg.Addr)
|
glog.V(0).Info("Starting on ", cfg.Addr)
|
||||||
graceful.Run(cfg.Addr, cfg.RequestTimeout.Duration, NewRouter(t, cfg))
|
graceful.Run(cfg.Addr, cfg.RequestTimeout.Duration, NewRouter(t, cfg))
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue