From 414e14f82699ebfd2c6a9f0254bd8ec4a321f89e Mon Sep 17 00:00:00 2001 From: Justin Li Date: Thu, 17 Jul 2014 01:45:14 -0400 Subject: [PATCH] Remove a small file, and add comments --- backend/noop/driver.go | 2 +- tracker/routines.go | 30 ---------------------------- tracker/tracker.go | 44 +++++++++++++++++++++++++++++++++++++++--- 3 files changed, 42 insertions(+), 34 deletions(-) delete mode 100644 tracker/routines.go diff --git a/backend/noop/driver.go b/backend/noop/driver.go index 04750e8..8fba995 100644 --- a/backend/noop/driver.go +++ b/backend/noop/driver.go @@ -7,8 +7,8 @@ package noop import ( - "github.com/chihaya/chihaya/config" "github.com/chihaya/chihaya/backend" + "github.com/chihaya/chihaya/config" "github.com/chihaya/chihaya/tracker/models" ) diff --git a/tracker/routines.go b/tracker/routines.go deleted file mode 100644 index 4c5dd53..0000000 --- a/tracker/routines.go +++ /dev/null @@ -1,30 +0,0 @@ -// 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) - } - } -} diff --git a/tracker/tracker.go b/tracker/tracker.go index 5a715a4..5f47fee 100644 --- a/tracker/tracker.go +++ b/tracker/tracker.go @@ -7,17 +7,25 @@ package tracker import ( - "github.com/chihaya/chihaya/config" + "time" + + "github.com/golang/glog" + "github.com/chihaya/chihaya/backend" + "github.com/chihaya/chihaya/config" "github.com/chihaya/chihaya/tracker/models" ) +// Tracker represents the logic necessary to service BitTorrent announces, +// independently of the underlying data transports used. type Tracker struct { cfg *config.Config Pool Pool backend backend.Conn } +// New creates a new Tracker, and opens any necessary connections. +// Maintenance routines are automatically spawned in the background. func New(cfg *config.Config) (*Tracker, error) { pool, err := Open(&cfg.Tracker) if err != nil { @@ -29,7 +37,7 @@ func New(cfg *config.Config) (*Tracker, error) { return nil, err } - go PurgeInactivePeers( + go purgeInactivePeers( pool, cfg.PurgeInactiveTorrents, cfg.Announce.Duration*2, @@ -43,8 +51,38 @@ func New(cfg *config.Config) (*Tracker, error) { }, nil } +// Writer serializes a tracker's responses, and is implemented for each +// response transport used by the tracker. +// +// Note, data passed into any of these functions will not contain sensitive +// information, so it may be passed back the client freely. type Writer interface { - WriteError(error) error + WriteError(err error) error WriteAnnounce(*models.AnnounceResponse) error WriteScrape(*models.ScrapeResponse) error } + +// purgeInactivePeers periodically walks the torrent database and removes +// peers that haven't announced recently. +// +// The default interval is 2x the announce interval, which gives delayed +// peers a chance to stay alive, while ensuring the majority of responses +// contain active peers. +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) + } + } +}