From: Matt Joiner Date: Thu, 3 Jul 2025 06:17:51 +0000 (+1000) Subject: Move some peer request stuff out of Peer X-Git-Tag: v1.59.0~46 X-Git-Url: http://www.git.stargrave.org/?a=commitdiff_plain;h=13d2f9248af7e24063e6f5bf0ebf1fd5a0b16219;p=btrtrc.git Move some peer request stuff out of Peer --- diff --git a/client.go b/client.go index a02c27d8..8f856827 100644 --- a/client.go +++ b/client.go @@ -1181,7 +1181,7 @@ func (t *Torrent) runHandshookConn(pc *PeerConn) error { return nil } -func (p *Peer) initUpdateRequestsTimer() { +func (p *PeerConn) initUpdateRequestsTimer() { if check.Enabled { if p.updateRequestsTimer != nil { panic(p.updateRequestsTimer) @@ -1194,7 +1194,7 @@ func (p *Peer) initUpdateRequestsTimer() { const peerUpdateRequestsTimerReason = "updateRequestsTimer" -func (c *Peer) updateRequestsTimerFunc() { +func (c *PeerConn) updateRequestsTimerFunc() { c.locker().Lock() defer c.locker().Unlock() if c.closed.IsSet() { @@ -1701,18 +1701,18 @@ func (cl *Client) newConnection(nc net.Conn, opts newConnectionOpts) (c *PeerCon } c = &PeerConn{ Peer: Peer{ - outgoing: opts.outgoing, - choking: true, - peerChoking: true, - PeerMaxRequests: 250, + outgoing: opts.outgoing, + choking: true, + peerChoking: true, RemoteAddr: opts.remoteAddr, localPublicAddr: opts.localPublicAddr, Network: opts.network, callbacks: &cl.config.Callbacks, }, - connString: opts.connString, - conn: nc, + PeerMaxRequests: 250, + connString: opts.connString, + conn: nc, } c.peerRequestDataAllocLimiter.Max = int64(cl.config.MaxAllocPeerRequestDataPerConn) c.initRequestState() diff --git a/peer-impl.go b/peer-impl.go index f1130c0e..7cd2d57c 100644 --- a/peer-impl.go +++ b/peer-impl.go @@ -12,6 +12,10 @@ import ( // with legacy PeerConn methods. New methods and calls that are fixed up should be migrated over to // newHotPeerImpl. type legacyPeerImpl interface { + // Whether the peer should be told to update requests. Sometimes this is skipped if it's high + // priority adjustments to requests. This is kind of only relevant to PeerConn but hasn't been + // fully migrated over yet. + isLowOnRequests() bool // Notify that the peers requests should be updated for the provided reason. onNeedUpdateRequests(reason updateRequestReason) diff --git a/peer.go b/peer.go index b0fc2da8..2d79a4bf 100644 --- a/peer.go +++ b/peer.go @@ -93,8 +93,6 @@ type ( peerMinPieces pieceIndex peerAllowedFast typedRoaring.Bitmap[pieceIndex] - - PeerMaxRequests maxRequests // Maximum pending requests the peer allows. } PeerSource string @@ -879,7 +877,7 @@ func (p *Peer) uncancelledRequests() uint64 { type peerLocalPublicAddr = IpPort -func (p *Peer) isLowOnRequests() bool { +func (p *PeerConn) isLowOnRequests() bool { return p.requestState.Requests.IsEmpty() && p.requestState.Cancelled.IsEmpty() } diff --git a/peerconn.go b/peerconn.go index 893b9473..95d7972c 100644 --- a/peerconn.go +++ b/peerconn.go @@ -50,6 +50,7 @@ type PeerConn struct { // and implementation differences, we may receive chunks that are no longer in the set of // requests actually want. This could use a roaring.BSI if the memory use becomes noticeable. validReceiveChunks map[RequestIndex]int + PeerMaxRequests maxRequests // Maximum pending requests the peer allows. // Move to PeerConn? protocolLogger log.Logger diff --git a/torrent.go b/torrent.go index c0a404b4..d681c868 100644 --- a/torrent.go +++ b/torrent.go @@ -3060,7 +3060,6 @@ func (t *Torrent) addWebSeed(url string, opts ...AddWebSeedsOpt) bool { }, hostKey: t.deriveWebSeedHostKey(url), } - ws.peer.initRequestState() for _, opt := range opts { opt(&ws.client) } @@ -3071,10 +3070,6 @@ func (t *Torrent) addWebSeed(url string, opts ...AddWebSeedsOpt) bool { } } g.MakeMapWithCap(&ws.activeRequests, ws.client.MaxRequests) - // TODO: Implement an algorithm that assigns this based on sharing chunks across peers. For now - // we just allow 2 MiB worth of requests. See newHotPeerImpl.nominalMaxRequests. - ws.peer.PeerMaxRequests = maxRequests(intCeilDiv(8<<20, ws.peer.t.chunkSize.Uint32())) - ws.peer.initUpdateRequestsTimer() ws.locker = t.cl.locker() for _, f := range t.callbacks().NewPeer { f(&ws.peer) diff --git a/webseed-peer.go b/webseed-peer.go index 7bcc240c..6fd865b2 100644 --- a/webseed-peer.go +++ b/webseed-peer.go @@ -29,6 +29,11 @@ type webseedPeer struct { hostKey webseedHostKeyHandle } +func (me *webseedPeer) isLowOnRequests() bool { + // Updates globally instead. + return false +} + // Webseed requests are issued globally so per-connection reasons or handling make no sense. func (me *webseedPeer) onNeedUpdateRequests(updateRequestReason) {} @@ -224,8 +229,6 @@ func (cn *webseedPeer) ban() { } func (ws *webseedPeer) onClose() { - // Just deleting them means we would have to manually cancel active requests. - ws.peer.cancelAllRequests() ws.peer.t.iterPeers(func(p *Peer) { if p.isLowOnRequests() { p.onNeedUpdateRequests("webseedPeer.onClose")