1
0
mirror of https://github.com/veggiedefender/torrent-client.git synced 2025-11-06 09:29:16 +02:00

Set 30 second deadline for pieces to download

This commit is contained in:
Jesse
2019-12-29 21:43:58 -05:00
parent c4fbd50021
commit 67066ad77c
2 changed files with 9 additions and 2 deletions

View File

@@ -25,7 +25,7 @@ type client struct {
} }
func completeHandshake(conn net.Conn, r *bufio.Reader, infohash, peerID [20]byte) (*handshake.Handshake, error) { func completeHandshake(conn net.Conn, r *bufio.Reader, infohash, peerID [20]byte) (*handshake.Handshake, error) {
conn.SetDeadline(time.Now().Local().Add(3 * time.Second)) conn.SetDeadline(time.Now().Add(3 * time.Second))
defer conn.SetDeadline(time.Time{}) // Disable the deadline defer conn.SetDeadline(time.Time{}) // Disable the deadline
req := handshake.New(infohash, peerID) req := handshake.New(infohash, peerID)
@@ -42,7 +42,7 @@ func completeHandshake(conn net.Conn, r *bufio.Reader, infohash, peerID [20]byte
} }
func recvBitfield(conn net.Conn, r *bufio.Reader) (bitfield.Bitfield, error) { func recvBitfield(conn net.Conn, r *bufio.Reader) (bitfield.Bitfield, error) {
conn.SetDeadline(time.Now().Local().Add(5 * time.Second)) conn.SetDeadline(time.Now().Add(5 * time.Second))
defer conn.SetDeadline(time.Time{}) // Disable the deadline defer conn.SetDeadline(time.Time{}) // Disable the deadline
msg, err := message.Read(r) msg, err := message.Read(r)

View File

@@ -7,6 +7,7 @@ import (
"log" "log"
"net" "net"
"runtime" "runtime"
"time"
"github.com/veggiedefender/torrent-client/message" "github.com/veggiedefender/torrent-client/message"
) )
@@ -105,6 +106,11 @@ func attemptDownloadPiece(c *client, pw *pieceWork) ([]byte, error) {
buf: make([]byte, pw.length), buf: make([]byte, pw.length),
} }
// Setting a deadline helps get unresponsive peers unstuck.
// 30 seconds is more than enough time to download 16 Kb
c.conn.SetDeadline(time.Now().Add(30 * time.Second))
defer c.conn.SetDeadline(time.Time{}) // Disable the deadline
for state.downloaded < pw.length { for state.downloaded < pw.length {
// Block and consume messages until not choked // Block and consume messages until not choked
if c.choked { if c.choked {
@@ -133,6 +139,7 @@ func attemptDownloadPiece(c *client, pw *pieceWork) ([]byte, error) {
return nil, err return nil, err
} }
} }
return state.buf, nil return state.buf, nil
} }