From e1ab8c4c0d42dead4669df3a6acb22ecd17e0f1d Mon Sep 17 00:00:00 2001 From: Matt Joiner Date: Thu, 15 May 2025 14:55:32 +1000 Subject: [PATCH] Misc tidy --- misc.go | 3 ++- peer.go | 8 ++++++-- requesting.go | 2 +- torrent.go | 1 + types/types.go | 1 + webseed-request.go | 1 + 6 files changed, 12 insertions(+), 4 deletions(-) create mode 100644 webseed-request.go diff --git a/misc.go b/misc.go index b657d104..12a454de 100644 --- a/misc.go +++ b/misc.go @@ -15,6 +15,7 @@ import ( ) type ( + // TODO: Make this private. Use types.Request in the (one?) place it's exposed here. Request = types.Request ChunkSpec = types.ChunkSpec PiecePriority = types.PiecePriority @@ -104,7 +105,7 @@ func validateInfo(info *metainfo.Info) error { } func chunkIndexSpec(index, pieceLength, chunkSize pp.Integer) ChunkSpec { - ret := ChunkSpec{pp.Integer(index) * chunkSize, chunkSize} + ret := ChunkSpec{index * chunkSize, chunkSize} if ret.Begin+ret.Length > pieceLength { ret.Length = pieceLength - ret.Begin } diff --git a/peer.go b/peer.go index 649634b2..69681e92 100644 --- a/peer.go +++ b/peer.go @@ -388,7 +388,8 @@ var ( // The actual value to use as the maximum outbound requests. func (cn *Peer) nominalMaxRequests() maxRequests { - return maxInt(1, minInt(cn.PeerMaxRequests, cn.peakRequests*2, maxLocalToRemoteRequests)) + // TODO: This should differ for webseeds... + return max(1, min(cn.PeerMaxRequests, cn.peakRequests*2, maxLocalToRemoteRequests)) } func (cn *Peer) totalExpectingTime() (ret time.Duration) { @@ -592,6 +593,7 @@ func (c *Peer) remoteRejectedRequest(r RequestIndex) bool { if c.deleteRequest(r) { c.decPeakRequests() } else if !c.requestState.Cancelled.CheckedRemove(r) { + // The request was already cancelled. return false } if c.isLowOnRequests() { @@ -616,7 +618,8 @@ func (c *Peer) doChunkReadStats(size int64) { c.allStats(func(cs *ConnStats) { cs.receivedChunk(size) }) } -// Handle a received chunk from a peer. +// Handle a received chunk from a peer. TODO: Break this out into non-wire protocol specific +// handling. Avoid shoehorning into a pp.Message. func (c *Peer) receiveChunk(msg *pp.Message) error { ChunksReceived.Add("total", 1) @@ -795,6 +798,7 @@ func (c *Peer) deleteRequest(r RequestIndex) bool { f(PeerRequestEvent{c, c.t.requestIndexToRequest(r)}) } c.updateExpectingChunks() + // TODO: Can't this happen if a request is stolen? if c.t.requestingPeer(r) != c { panic("only one peer should have a given request at a time") } diff --git a/requesting.go b/requesting.go index cf69f08b..958e11b8 100644 --- a/requesting.go +++ b/requesting.go @@ -75,7 +75,7 @@ func (p *peerId) GobDecode(b []byte) error { type ( // A request index is a chunk indexed across the entire torrent. It's a single integer and can - // be converted to a protocol request. + // be converted to a protocol request. TODO: This should be private. RequestIndex = requestStrategy.RequestIndex chunkIndexType = requestStrategy.ChunkIndex ) diff --git a/torrent.go b/torrent.go index bc6a5977..b59202a8 100644 --- a/torrent.go +++ b/torrent.go @@ -3010,6 +3010,7 @@ func (t *Torrent) peerIsActive(p *Peer) (active bool) { return } +// TODO: It's more of a RequestStruct really. func (t *Torrent) requestIndexToRequest(ri RequestIndex) Request { index := t.pieceIndexOfRequestIndex(ri) return Request{ diff --git a/types/types.go b/types/types.go index 977f1f55..d87844a4 100644 --- a/types/types.go +++ b/types/types.go @@ -10,6 +10,7 @@ import ( type PieceIndex = int +// TODO: Where is the doc/spec calling it this? type ChunkSpec struct { Begin, Length pp.Integer } diff --git a/webseed-request.go b/webseed-request.go new file mode 100644 index 00000000..10cbafc7 --- /dev/null +++ b/webseed-request.go @@ -0,0 +1 @@ +package torrent -- 2.51.0