]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Move some peer request stuff out of Peer
authorMatt Joiner <anacrolix@gmail.com>
Thu, 3 Jul 2025 06:17:51 +0000 (16:17 +1000)
committerMatt Joiner <anacrolix@gmail.com>
Thu, 3 Jul 2025 06:17:51 +0000 (16:17 +1000)
client.go
peer-impl.go
peer.go
peerconn.go
torrent.go
webseed-peer.go

index a02c27d8dc1b2de1425985fde513c0166c62110d..8f856827081905588e2abbba2ad610c37f7b1355 100644 (file)
--- 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()
index f1130c0e4e7c927bda458bf041c9f5e873c98b5b..7cd2d57c8c66b4e4679e2de3c393103749f9cdbf 100644 (file)
@@ -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 b0fc2da8f2adb41a6b84b923a13658959e91d111..2d79a4bf0c070239a2baa493bfd45dd634465f94 100644 (file)
--- 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()
 }
 
index 893b947335ab632a2e495636edb6989269347308..95d7972c7788691fdf42439d94f5c3e35f052f8e 100644 (file)
@@ -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
index c0a404b49ec0e3810db991aefc22d2dd3707a7a9..d681c8684b2d2ca7591fb0136d06193c6cd12e40 100644 (file)
@@ -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)
index 7bcc240c74a9cc6cee0f75309804f337f6ff50c8..6fd865b2aad833f30fb2ba1deb35ebb454532661 100644 (file)
@@ -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")