Skip to content

Commit

Permalink
Misc tidying
Browse files Browse the repository at this point in the history
  • Loading branch information
anacrolix committed Feb 8, 2024
1 parent 8ca2573 commit 8c3338f
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 27 deletions.
2 changes: 1 addition & 1 deletion peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -719,7 +719,7 @@ func (c *Peer) receiveChunk(msg *pp.Message) error {

cl.event.Broadcast()
// We do this because we've written a chunk, and may change PieceState.Partial.
t.publishPieceChange(pieceIndex(ppReq.Index))
t.publishPieceStateChange(pieceIndex(ppReq.Index))

return nil
}
Expand Down
12 changes: 10 additions & 2 deletions peerconn.go
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ func (cn *PeerConn) peerSentBitfield(bf []bool) error {
return nil
}

func (cn *PeerConn) onPeerHasAllPieces() {
func (cn *PeerConn) onPeerHasAllPiecesNoTriggers() {
t := cn.t
if t.haveInfo() {
cn._peerPieces.Iterate(func(x uint32) bool {
Expand All @@ -458,6 +458,14 @@ func (cn *PeerConn) onPeerHasAllPieces() {
t.addConnWithAllPieces(&cn.Peer)
cn.peerSentHaveAll = true
cn._peerPieces.Clear()
}

func (cn *PeerConn) onPeerHasAllPieces() {
cn.onPeerHasAllPiecesNoTriggers()
cn.peerHasAllPiecesTriggers()
}

func (cn *PeerConn) peerHasAllPiecesTriggers() {
if !cn.t._pendingPieces.IsEmpty() {
cn.updateRequests("Peer.onPeerHasAllPieces")
}
Expand Down Expand Up @@ -1044,7 +1052,7 @@ func (c *PeerConn) sendChunk(r Request, msg func(pp.Message) bool, state *peerRe
})
}

func (c *PeerConn) setTorrent(t *Torrent) {
func (c *Peer) setTorrent(t *Torrent) {
if c.t != nil {
panic("connection already associated with a torrent")
}
Expand Down
6 changes: 3 additions & 3 deletions peerconn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ func TestSendBitfieldThenHave(t *testing.T) {
var cl Client
cl.init(TestingConfig(t))
cl.initLogger()
qtc := qt.New(t)
c := cl.newConnection(nil, newConnectionOpts{network: "io.Pipe"})
c.setTorrent(cl.newTorrent(metainfo.Hash{}, nil))
if err := c.t.setInfo(&metainfo.Info{Pieces: make([]byte, metainfo.HashSize*3)}); err != nil {
t.Log(err)
}
err := c.t.setInfo(&metainfo.Info{Pieces: make([]byte, metainfo.HashSize*3)})
qtc.Assert(err, qt.IsNil)
r, w := io.Pipe()
// c.r = r
c.w = w
Expand Down
3 changes: 2 additions & 1 deletion request-strategy-impls.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package torrent

import (
g "github.com/anacrolix/generics"
"github.com/anacrolix/torrent/metainfo"
request_strategy "github.com/anacrolix/torrent/request-strategy"
"github.com/anacrolix/torrent/storage"
Expand All @@ -12,7 +13,7 @@ type requestStrategyInput struct {
}

func (r requestStrategyInput) Torrent(ih metainfo.Hash) request_strategy.Torrent {
return requestStrategyTorrent{r.cl.torrents[ih]}
return requestStrategyTorrent{g.MapMustGet(r.cl.torrents, ih)}
}

func (r requestStrategyInput) Capacity() (int64, bool) {
Expand Down
3 changes: 0 additions & 3 deletions request-strategy/order.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package requestStrategy

import (
"bytes"
"expvar"

g "github.com/anacrolix/generics"
"github.com/anacrolix/multiless"
Expand Down Expand Up @@ -41,8 +40,6 @@ func pieceOrderLess(i, j *pieceRequestOrderItem) multiless.Computation {
})
}

var packageExpvarMap = expvar.NewMap("request-strategy")

// Calls f with requestable pieces in order.
func GetRequestablePieces(
input Input, pro *PieceRequestOrder,
Expand Down
35 changes: 18 additions & 17 deletions torrent.go
Original file line number Diff line number Diff line change
Expand Up @@ -1166,7 +1166,7 @@ type PieceStateChange struct {
PieceState
}

func (t *Torrent) publishPieceChange(piece pieceIndex) {
func (t *Torrent) publishPieceStateChange(piece pieceIndex) {
t.cl._mu.Defer(func() {
cur := t.pieceState(piece)
p := &t.pieces[piece]
Expand Down Expand Up @@ -1230,7 +1230,7 @@ func (t *Torrent) maybeNewConns() {
t.openNewConns()
}

func (t *Torrent) piecePriorityChanged(piece pieceIndex, reason string) {
func (t *Torrent) onPiecePendingTriggers(piece pieceIndex, reason string) {
if t._pendingPieces.Contains(uint32(piece)) {
t.iterPeers(func(c *Peer) {
// if c.requestState.Interested {
Expand All @@ -1249,10 +1249,10 @@ func (t *Torrent) piecePriorityChanged(piece pieceIndex, reason string) {
})
}
t.maybeNewConns()
t.publishPieceChange(piece)
t.publishPieceStateChange(piece)
}

func (t *Torrent) updatePiecePriority(piece pieceIndex, reason string) {
func (t *Torrent) updatePiecePriorityNoTriggers(piece pieceIndex) (pendingChanged bool) {
if !t.closed.IsSet() {
// It would be possible to filter on pure-priority changes here to avoid churning the piece
// request order.
Expand All @@ -1262,15 +1262,16 @@ func (t *Torrent) updatePiecePriority(piece pieceIndex, reason string) {
newPrio := p.uncachedPriority()
// t.logger.Printf("torrent %p: piece %d: uncached priority: %v", t, piece, newPrio)
if newPrio == PiecePriorityNone {
if !t._pendingPieces.CheckedRemove(uint32(piece)) {
return
}
return t._pendingPieces.CheckedRemove(uint32(piece))
} else {
if !t._pendingPieces.CheckedAdd(uint32(piece)) {
return
}
return t._pendingPieces.CheckedAdd(uint32(piece))
}
}

func (t *Torrent) updatePiecePriority(piece pieceIndex, reason string) {
if t.updatePiecePriorityNoTriggers(piece) {
t.onPiecePendingTriggers(piece, reason)
}
t.piecePriorityChanged(piece, reason)
}

func (t *Torrent) updateAllPiecePriorities(reason string) {
Expand Down Expand Up @@ -1420,7 +1421,7 @@ func (t *Torrent) updatePieceCompletion(piece pieceIndex) bool {
t.logger.Printf("marked piece %v complete but still has dirtiers", piece)
}
if changed {
log.Fstr("piece %d completion changed: %+v -> %+v", piece, cached, uncached).LogLevel(log.Debug, t.logger)
t.logger.Levelf(log.Debug, "piece %d completion changed: %+v -> %+v", piece, cached, uncached)
t.pieceCompletionChanged(piece, "Torrent.updatePieceCompletion")
}
return changed
Expand Down Expand Up @@ -1957,7 +1958,7 @@ func (t *Torrent) numTotalPeers() int {

// Reconcile bytes transferred before connection was associated with a
// torrent.
func (t *Torrent) reconcileHandshakeStats(c *PeerConn) {
func (t *Torrent) reconcileHandshakeStats(c *Peer) {
if c._stats != (ConnStats{
// Handshakes should only increment these fields:
BytesWritten: c._stats.BytesWritten,
Expand Down Expand Up @@ -2123,10 +2124,10 @@ func (t *Torrent) pieceHashed(piece pieceIndex, passed bool, hashIoErr error) {
}

p.marking = true
t.publishPieceChange(piece)
t.publishPieceStateChange(piece)
defer func() {
p.marking = false
t.publishPieceChange(piece)
t.publishPieceStateChange(piece)
}()

if passed {
Expand Down Expand Up @@ -2268,7 +2269,7 @@ func (t *Torrent) tryCreatePieceHasher() bool {
p := t.piece(pi)
t.piecesQueuedForHash.Remove(bitmap.BitIndex(pi))
p.hashing = true
t.publishPieceChange(pi)
t.publishPieceStateChange(pi)
t.updatePiecePriority(pi, "Torrent.tryCreatePieceHasher")
t.storageLock.RLock()
t.activePieceHashes++
Expand Down Expand Up @@ -2362,7 +2363,7 @@ func (t *Torrent) queuePieceCheck(pieceIndex pieceIndex) {
return
}
t.piecesQueuedForHash.Add(bitmap.BitIndex(pieceIndex))
t.publishPieceChange(pieceIndex)
t.publishPieceStateChange(pieceIndex)
t.updatePiecePriority(pieceIndex, "Torrent.queuePieceCheck")
t.tryCreateMorePieceHashers()
}
Expand Down

0 comments on commit 8c3338f

Please sign in to comment.