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
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() {
// 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) {
})
}
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
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