]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Support individual peer max requests
authorMatt Joiner <anacrolix@gmail.com>
Wed, 21 May 2014 07:47:42 +0000 (17:47 +1000)
committerMatt Joiner <anacrolix@gmail.com>
Wed, 21 May 2014 07:47:42 +0000 (17:47 +1000)
client.go
connection.go
misc.go

index 4902a1962457177fefef38bbd650e3dbf13344fd..206cdd7c31268eb9add3743b9d08f72937b0f4c5 100644 (file)
--- a/client.go
+++ b/client.go
@@ -280,11 +280,12 @@ func (me *Client) initiateConn(peer Peer, torrent *torrent) {
 
 func (me *Client) runConnection(sock net.Conn, torrent *torrent) (err error) {
        conn := &connection{
-               Socket:     sock,
-               Choked:     true,
-               PeerChoked: true,
-               write:      make(chan []byte),
-               post:       make(chan encoding.BinaryMarshaler),
+               Socket:          sock,
+               Choked:          true,
+               PeerChoked:      true,
+               write:           make(chan []byte),
+               post:            make(chan encoding.BinaryMarshaler),
+               PeerMaxRequests: 250,
        }
        defer func() {
                // There's a lock and deferred unlock later in this function. The
index 5c153e19b810ff0ea9e679401480c827a151f1d9..136520874c83e11648b2895572aa1db712e841d8 100644 (file)
@@ -25,12 +25,13 @@ type connection struct {
        Requests   map[request]struct{}
 
        // Stuff controlled by the remote peer.
-       PeerId         [20]byte
-       PeerInterested bool
-       PeerChoked     bool
-       PeerRequests   map[request]struct{}
-       PeerExtensions [8]byte
-       PeerPieces     []bool
+       PeerId          [20]byte
+       PeerInterested  bool
+       PeerChoked      bool
+       PeerRequests    map[request]struct{}
+       PeerExtensions  [8]byte
+       PeerPieces      []bool
+       PeerMaxRequests int // Maximum pending requests the peer allows.
 }
 
 func (c *connection) Close() {
@@ -63,7 +64,7 @@ func (c *connection) Post(msg encoding.BinaryMarshaler) {
 
 // Returns true if more requests can be sent.
 func (c *connection) Request(chunk request) bool {
-       if len(c.Requests) >= maxRequests {
+       if len(c.Requests) >= c.PeerMaxRequests {
                return false
        }
        if !c.PeerHasPiece(chunk.Index) {
@@ -82,7 +83,7 @@ func (c *connection) Request(chunk request) bool {
                })
        }
        if c.Requests == nil {
-               c.Requests = make(map[request]struct{}, maxRequests)
+               c.Requests = make(map[request]struct{}, c.PeerMaxRequests)
        }
        c.Requests[chunk] = struct{}{}
        return true
diff --git a/misc.go b/misc.go
index 8e1072f81cf73c069a2b8e02cf67ba4db7017a60..2e5f02bb8a24c0c86fb3baae343ba06ea4c8cd5e 100644 (file)
--- a/misc.go
+++ b/misc.go
@@ -15,7 +15,7 @@ import (
 
 const (
        pieceHash   = crypto.SHA1
-       maxRequests = 250
+       maxRequests = 250 // Maximum pending requests we allow peers to send us.
        chunkSize   = 0x4000     // 16KiB
        BEP20       = "-GT0000-" // Peer ID client identifier prefix
        dialTimeout = time.Second * 15