connection.go | 13 ++++++++++++- torrent.go | 15 +++++++++++++++ diff --git a/connection.go b/connection.go index 40d02abab34d7ca8f57417bc66a23a664c0690f8..aea9d49d544123c7844068a9bbe08d9135269564 100644 --- a/connection.go +++ b/connection.go @@ -359,6 +359,7 @@ if !cn.PeerHasPiece(r.Index.Int()) { panic("requesting piece peer doesn't have") } cn.requests[r] = struct{}{} + cn.t.pendingRequests[r]++ return mw(pp.Message{ Type: pp.Request, Index: r.Index, @@ -846,7 +847,7 @@ messageTypesReceived.Add(strconv.FormatInt(int64(msg.Type), 10), 1) switch msg.Type { case pp.Choke: c.PeerChoked = true - c.requests = nil + c.deleteAllRequests() // We can then reset our interest. c.updateRequests() case pp.Reject: @@ -1211,7 +1212,17 @@ if _, ok := c.requests[r]; !ok { return false } delete(c.requests, r) + c.t.pendingRequests[r]-- return true +} + +func (c *connection) deleteAllRequests() { + for r := range c.requests { + c.deleteRequest(r) + } + // for c := range c.t.conns { + // c.tickleWriter() + // } } func (c *connection) tickleWriter() { diff --git a/torrent.go b/torrent.go index 935fb164a9593affdda6aefdee6cf60ef889a1b3..0814dcee1f1649c4fb4ae661b769ce07248beafc 100644 --- a/torrent.go +++ b/torrent.go @@ -133,6 +133,8 @@ // different pieces. connPieceInclinationPool sync.Pool // Torrent-level statistics. stats TorrentStats + + pendingRequests map[request]int } // Returns a channel that is closed when the Torrent is closed. @@ -381,6 +383,7 @@ } t.cl.event.Broadcast() t.gotMetainfo.Set() t.updateWantPeersEvent() + t.pendingRequests = make(map[request]int) } // Called when metadata for a torrent becomes available. @@ -1170,7 +1173,19 @@ // Returns true if connection is removed from torrent.Conns. func (t *Torrent) deleteConnection(c *connection) (ret bool) { _, ret = t.conns[c] delete(t.conns, c) + c.deleteAllRequests() + if len(t.conns) == 0 { + t.assertNoPendingRequests() + } return +} + +func (t *Torrent) assertNoPendingRequests() { + for _, num := range t.pendingRequests { + if num != 0 { + panic(num) + } + } } func (t *Torrent) dropConnection(c *connection) {