From: Matt Joiner Date: Mon, 18 Oct 2021 08:06:33 +0000 (+1100) Subject: Improvements to request refreshing X-Git-Tag: v1.34.0^2~27 X-Git-Url: http://www.git.stargrave.org/?a=commitdiff_plain;h=ccce2dba13be2b86a8daa8804e7cbc04b475aa8b;p=btrtrc.git Improvements to request refreshing --- diff --git a/client.go b/client.go index af18b6d0..179031d0 100644 --- a/client.go +++ b/client.go @@ -957,15 +957,7 @@ func (cl *Client) runHandshookConn(c *PeerConn, t *Torrent) error { defer t.dropConnection(c) c.startWriter() cl.sendInitialMessages(c, t) - c.updateRequestsTimer = time.AfterFunc(math.MaxInt64, func() { - if c.needRequestUpdate != "" { - return - } - if c.actualRequestState.Requests.IsEmpty() { - panic("updateRequestsTimer should have been stopped") - } - c.updateRequests("updateRequestsTimer") - }) + c.updateRequestsTimer = time.AfterFunc(math.MaxInt64, c.updateRequestsTimerFunc) c.updateRequestsTimer.Stop() err := c.mainReadLoop() if err != nil { @@ -974,6 +966,18 @@ func (cl *Client) runHandshookConn(c *PeerConn, t *Torrent) error { return nil } +func (c *PeerConn) updateRequestsTimerFunc() { + c.locker().Lock() + defer c.locker().Unlock() + if c.needRequestUpdate != "" { + return + } + if c.actualRequestState.Requests.IsEmpty() { + panic("updateRequestsTimer should have been stopped") + } + c.updateRequests("updateRequestsTimer") +} + // Maximum pending requests we allow peers to send us. If peer requests are buffered on read, this // instructs the amount of memory that might be used to cache pending writes. Assuming 512KiB // (1<<19) cached for sending, for 16KiB (1<<14) chunks. diff --git a/peerconn.go b/peerconn.go index 545e29f1..36c07044 100644 --- a/peerconn.go +++ b/peerconn.go @@ -1084,7 +1084,10 @@ func (c *PeerConn) mainReadLoop() (err error) { if preservedCount != 0 { // TODO: Yes this is a debug log but I'm not happy with the state of the logging lib // right now. - log.Printf("%v requests were preserved while being choked", preservedCount) + log.Printf( + "%v requests were preserved while being choked (fast=%v)", + preservedCount, + c.fastEnabled()) torrent.Add("requestsPreservedThroughChoking", int64(preservedCount)) } c.updateRequests("unchoked") diff --git a/requesting.go b/requesting.go index 7d0c8f2f..b551a48f 100644 --- a/requesting.go +++ b/requesting.go @@ -273,11 +273,12 @@ func (p *Peer) applyRequestState(next requestState) bool { return true } if maxRequests(current.Requests.GetCardinality()) >= p.nominalMaxRequests() { - log.Printf("not assigning all requests [desired=%v, cancelled=%v, max=%v]", - next.Requests.GetCardinality(), - p.cancelledRequests.GetCardinality(), - p.nominalMaxRequests(), - ) + //log.Printf("not assigning all requests [desired=%v, cancelled=%v, current=%v, max=%v]", + // next.Requests.GetCardinality(), + // p.cancelledRequests.GetCardinality(), + // current.Requests.GetCardinality(), + // p.nominalMaxRequests(), + //) return false } var err error @@ -287,11 +288,10 @@ func (p *Peer) applyRequestState(next requestState) bool { } return more }) + p.updateRequestsTimer.Stop() if more { p.needRequestUpdate = "" - if current.Requests.IsEmpty() { - p.updateRequestsTimer.Stop() - } else { + if !current.Requests.IsEmpty() { p.updateRequestsTimer.Reset(time.Second) } }