From: Matt Joiner Date: Wed, 20 Oct 2021 23:48:43 +0000 (+1100) Subject: Add low requests check X-Git-Tag: v1.34.0^2~14 X-Git-Url: http://www.git.stargrave.org/?a=commitdiff_plain;h=ff3c186396fd0702db915eadcb39a127050c75bd;p=btrtrc.git Add low requests check This is an optimization for webseeds, which have 10 synchronous request routines, and if the request count dips below 10, some sit idle. There is probably something similar to be done with PeerConns, which won't update until there are zero requests, but there there is a timer to refresh updates, and the queues are very long (typically 512-2048). --- diff --git a/client.go b/client.go index 72013b99..142213c0 100644 --- a/client.go +++ b/client.go @@ -983,7 +983,7 @@ func (c *Peer) updateRequestsTimerFunc() { if c.needRequestUpdate != "" { return } - if c.actualRequestState.Requests.IsEmpty() { + if c.isLowOnRequests() { // If there are no outstanding requests, then a request update should have already run. return } diff --git a/peer-impl.go b/peer-impl.go index 4dbc6b4c..b5cf028e 100644 --- a/peer-impl.go +++ b/peer-impl.go @@ -10,6 +10,8 @@ import ( type peerImpl interface { // Trigger the actual request state to get updated handleUpdateRequests() + // Whether the outstanding local request cardinality is low enough to warrant an update. + isLowOnRequests() bool writeInterested(interested bool) bool // Neither of these return buffer room anymore, because they're currently both posted. There's diff --git a/peerconn.go b/peerconn.go index a9d0baad..005c6b7d 100644 --- a/peerconn.go +++ b/peerconn.go @@ -630,7 +630,7 @@ func (me *PeerConn) _cancel(r RequestIndex) bool { if !me.deleteRequest(r) { panic("request not existing should have been guarded") } - if me.actualRequestState.Requests.IsEmpty() { + if me.isLowOnRequests() { me.updateRequests("Peer.cancel") } } @@ -1195,7 +1195,7 @@ func (c *PeerConn) mainReadLoop() (err error) { func (c *Peer) remoteRejectedRequest(r RequestIndex) { if c.deleteRequest(r) { - if c.actualRequestState.Requests.IsEmpty() { + if c.isLowOnRequests() { c.updateRequests("Peer.remoteRejectedRequest") } c.decExpectedChunkReceive(r) @@ -1334,7 +1334,7 @@ func (c *Peer) receiveChunk(msg *pp.Message) error { if !c.peerChoking { c._chunksReceivedWhileExpecting++ } - if c.actualRequestState.Requests.IsEmpty() { + if c.isLowOnRequests() { c.updateRequests("Peer.receiveChunk deleted request") } } else { @@ -1542,9 +1542,6 @@ func (c *Peer) deleteAllRequests() { if !c.actualRequestState.Requests.IsEmpty() { panic(c.actualRequestState.Requests.GetCardinality()) } - // for c := range c.t.conns { - // c.tickleWriter() - // } } // This is called when something has changed that should wake the writer, such as putting stuff into @@ -1671,3 +1668,7 @@ func (p *Peer) TryAsPeerConn() (*PeerConn, bool) { pc, ok := p.peerImpl.(*PeerConn) return pc, ok } + +func (pc *PeerConn) isLowOnRequests() bool { + return pc.actualRequestState.Requests.IsEmpty() +} diff --git a/torrent.go b/torrent.go index 92277342..f594c9f6 100644 --- a/torrent.go +++ b/torrent.go @@ -1085,7 +1085,7 @@ func (t *Torrent) piecePriorityChanged(piece pieceIndex, reason string) { if c.actualRequestState.Interested { return } - if !c.actualRequestState.Requests.IsEmpty() { + if !c.isLowOnRequests() { return } if !c.peerHasPiece(piece) { @@ -2225,6 +2225,7 @@ func (t *Torrent) addWebSeed(url string) { Url: url, }, activeRequests: make(map[Request]webseed.Request, maxRequests), + maxRequests: maxRequests, } ws.peer.initUpdateRequestsTimer() ws.requesterCond.L = t.cl.locker() diff --git a/webseed-peer.go b/webseed-peer.go index 68fbc3e7..fb981585 100644 --- a/webseed-peer.go +++ b/webseed-peer.go @@ -21,6 +21,8 @@ type webseedPeer struct { activeRequests map[Request]webseed.Request requesterCond sync.Cond peer Peer + // Number of requester routines. + maxRequests int } var _ peerImpl = (*webseedPeer)(nil) @@ -49,7 +51,7 @@ func (ws *webseedPeer) _cancel(r RequestIndex) bool { if !ws.peer.deleteRequest(r) { panic("cancelled webseed request should exist") } - if ws.peer.actualRequestState.Requests.IsEmpty() { + if ws.peer.isLowOnRequests() { ws.peer.updateRequests("webseedPeer._cancel") } } @@ -158,3 +160,7 @@ func (ws *webseedPeer) requestResultHandler(r Request, webseedRequest webseed.Re } } } + +func (me *webseedPeer) isLowOnRequests() bool { + return me.peer.actualRequestState.Requests.GetCardinality() < uint64(me.maxRequests) +}