From 67a8473f6e9bab8b12e84d5edd70e9f7e3943e45 Mon Sep 17 00:00:00 2001 From: Justin Li Date: Thu, 17 Jul 2014 01:14:50 -0400 Subject: [PATCH] Clean up types more --- http/announce_private_test.go | 4 ++-- tracker/announce.go | 10 +++++----- tracker/conn.go | 4 ++-- tracker/models/models.go | 5 +++-- 4 files changed, 12 insertions(+), 11 deletions(-) diff --git a/http/announce_private_test.go b/http/announce_private_test.go index 340cf8e..a1129d0 100644 --- a/http/announce_private_test.go +++ b/http/announce_private_test.go @@ -95,8 +95,8 @@ func loadTestData(tkr *tracker.Tracker) error { torrent := &models.Torrent{ ID: 1, Infohash: infoHash, - Seeders: make(map[string]models.Peer), - Leechers: make(map[string]models.Peer), + Seeders: models.PeerMap{}, + Leechers: models.PeerMap{}, } return conn.PutTorrent(torrent) diff --git a/tracker/announce.go b/tracker/announce.go index c2246d2..85705d9 100644 --- a/tracker/announce.go +++ b/tracker/announce.go @@ -41,8 +41,8 @@ func (t *Tracker) HandleAnnounce(ann *models.Announce, w Writer) error { case !t.cfg.Private && err == ErrTorrentDNE: torrent = &models.Torrent{ Infohash: ann.Infohash, - Seeders: make(map[string]models.Peer), - Leechers: make(map[string]models.Peer), + Seeders: models.PeerMap{}, + Leechers: models.PeerMap{}, } err = conn.PutTorrent(torrent) @@ -149,7 +149,7 @@ func handleEvent(c Conn, ann *models.Announce, p *models.Peer, u *models.User, t t.Snatches++ if t.InLeecherPool(p) { - err = LeecherFinished(c, t.Infohash, p) + err = leecherFinished(c, t.Infohash, p) if err != nil { return } @@ -157,7 +157,7 @@ func handleEvent(c Conn, ann *models.Announce, p *models.Peer, u *models.User, t case t.InLeecherPool(p) && ann.Left == 0: // A leecher completed but the event was never received. - err = LeecherFinished(c, t.Infohash, p) + err = leecherFinished(c, t.Infohash, p) if err != nil { return } @@ -198,7 +198,7 @@ func getPeers(ann *models.Announce, announcer *models.Peer, t *models.Torrent, w return appendPeers(ipv4s, ipv6s, announcer, t.Leechers, wanted-len(ipv4s)-len(ipv6s)) } -func appendPeers(ipv4s, ipv6s models.PeerList, announcer *models.Peer, peers map[string]models.Peer, wanted int) (models.PeerList, models.PeerList) { +func appendPeers(ipv4s, ipv6s models.PeerList, announcer *models.Peer, peers models.PeerMap, wanted int) (models.PeerList, models.PeerList) { count := 0 for _, peer := range peers { diff --git a/tracker/conn.go b/tracker/conn.go index acb8636..e8f3d59 100644 --- a/tracker/conn.go +++ b/tracker/conn.go @@ -93,8 +93,8 @@ type Conn interface { DeleteClient(clientID string) error } -// LeecherFinished moves a peer from the leeching pool to the seeder pool. -func LeecherFinished(c Conn, infohash string, p *models.Peer) error { +// leecherFinished moves a peer from the leeching pool to the seeder pool. +func leecherFinished(c Conn, infohash string, p *models.Peer) error { err := c.DeleteLeecher(infohash, p.Key()) if err != nil { return err diff --git a/tracker/models/models.go b/tracker/models/models.go index 642b40e..594cba4 100644 --- a/tracker/models/models.go +++ b/tracker/models/models.go @@ -35,6 +35,7 @@ type Peer struct { } type PeerList []Peer +type PeerMap map[string]Peer // NewPeer returns the Peer representation of an Announce. When provided nil // for the announce parameter, it panics. When provided nil for the user or @@ -79,8 +80,8 @@ type Torrent struct { ID uint64 `json:"id"` Infohash string `json:"infohash"` - Seeders map[string]Peer `json:"seeders"` - Leechers map[string]Peer `json:"leechers"` + Seeders PeerMap `json:"seeders"` + Leechers PeerMap `json:"leechers"` Snatches uint64 `json:"snatches"` UpMultiplier float64 `json:"up_multiplier"`