From: Matt Joiner Date: Wed, 21 May 2014 07:47:42 +0000 (+1000) Subject: Support individual peer max requests X-Git-Tag: v1.0.0~1744 X-Git-Url: http://www.git.stargrave.org/?a=commitdiff_plain;h=d16f444fa8ddd85a6a77f068e45781880906ece5;p=btrtrc.git Support individual peer max requests --- diff --git a/client.go b/client.go index 4902a196..206cdd7c 100644 --- 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 diff --git a/connection.go b/connection.go index 5c153e19..13652087 100644 --- a/connection.go +++ b/connection.go @@ -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 8e1072f8..2e5f02bb 100644 --- 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