]> Sergey Matveev's repositories - btrtrc.git/blobdiff - client.go
Improvements to request refreshing
[btrtrc.git] / client.go
index af18b6d0604187cee61aa0652ff4b10b92acd3a8..179031d03e3a81b7b1bd21151a1769a05f9eb141 100644 (file)
--- 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.