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).
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
}
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
if !me.deleteRequest(r) {
panic("request not existing should have been guarded")
}
- if me.actualRequestState.Requests.IsEmpty() {
+ if me.isLowOnRequests() {
me.updateRequests("Peer.cancel")
}
}
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)
if !c.peerChoking {
c._chunksReceivedWhileExpecting++
}
- if c.actualRequestState.Requests.IsEmpty() {
+ if c.isLowOnRequests() {
c.updateRequests("Peer.receiveChunk deleted request")
}
} else {
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
pc, ok := p.peerImpl.(*PeerConn)
return pc, ok
}
+
+func (pc *PeerConn) isLowOnRequests() bool {
+ return pc.actualRequestState.Requests.IsEmpty()
+}
if c.actualRequestState.Interested {
return
}
- if !c.actualRequestState.Requests.IsEmpty() {
+ if !c.isLowOnRequests() {
return
}
if !c.peerHasPiece(piece) {
Url: url,
},
activeRequests: make(map[Request]webseed.Request, maxRequests),
+ maxRequests: maxRequests,
}
ws.peer.initUpdateRequestsTimer()
ws.requesterCond.L = t.cl.locker()
activeRequests map[Request]webseed.Request
requesterCond sync.Cond
peer Peer
+ // Number of requester routines.
+ maxRequests int
}
var _ peerImpl = (*webseedPeer)(nil)
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")
}
}
}
}
}
+
+func (me *webseedPeer) isLowOnRequests() bool {
+ return me.peer.actualRequestState.Requests.GetCardinality() < uint64(me.maxRequests)
+}