client.go | 11 ++++++----- connection.go | 17 +++++++++-------- misc.go | 2 +- diff --git a/client.go b/client.go index 4902a1962457177fefef38bbd650e3dbf13344fd..206cdd7c31268eb9add3743b9d08f72937b0f4c5 100644 --- a/client.go +++ b/client.go @@ -280,11 +280,12 @@ } 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 diff --git a/connection.go b/connection.go index 5c153e19b810ff0ea9e679401480c827a151f1d9..136520874c83e11648b2895572aa1db712e841d8 100644 --- a/connection.go +++ b/connection.go @@ -25,12 +25,13 @@ Choked bool 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 @@ } // 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 @@ Length: chunk.Length, }) } 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 --- a/misc.go +++ b/misc.go @@ -15,7 +15,7 @@ ) 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