Commit 9d71e2ae authored by Christopher Schinnerl's avatar Christopher Schinnerl

Close Downloader if chunk download is completed

parent 8804d79d
......@@ -65,11 +65,9 @@ func (s *streamer) Read(p []byte) (n int, err error) {
}
// Calculate how much we can download. We never download more than a single chunk.
chunkSize := s.file.staticChunkSize()
remainingData := uint64(fileSize - s.offset)
requestedData := uint64(len(p))
remainingChunk := chunkSize - uint64(s.offset)%chunkSize
length := min(remainingData, requestedData, remainingChunk)
length := min(remainingData, requestedData)
// Download data
buffer := bytes.NewBuffer([]byte{})
......
......@@ -2,6 +2,7 @@ package proto
import (
"net"
"strings"
"sync"
"time"
......@@ -83,10 +84,12 @@ func (hd *Downloader) Sector(root crypto.Hash) (_ modules.RenterContract, _ []by
// Increase Successful/Failed interactions accordingly
defer func() {
if err != nil {
// Ignore ErrStopResponse and closed network connecton errors since
// they are not considered a failed interaction with the host.
if err != nil && err != modules.ErrStopResponse && !strings.Contains(err.Error(), "use of closed network connection") {
hd.hdb.IncrementFailedInteractions(contract.HostPublicKey())
err = errors.Extend(err, modules.ErrHostFault)
} else if err == nil {
} else {
hd.hdb.IncrementSuccessfulInteractions(contract.HostPublicKey())
}
}()
......@@ -101,10 +104,10 @@ func (hd *Downloader) Sector(root crypto.Hash) (_ modules.RenterContract, _ []by
extendDeadline(hd.conn, connTimeout)
signedTxn, err := negotiateRevision(hd.conn, rev, contract.SecretKey, hd.height)
if err == modules.ErrStopResponse {
// if host gracefully closed, close our connection as well; this will
// cause the next download to fail. However, we must delay closing
// until we've finished downloading the sector.
defer hd.conn.Close()
// if host gracefully closed, close our connection as well. We return
// ErrStopResponse to avoid penalizing the host.
_ = hd.conn.Close()
return modules.RenterContract{}, nil, err
} else if err != nil {
return modules.RenterContract{}, nil, err
}
......
......@@ -34,6 +34,17 @@ func (w *worker) managedDownload(udc *unfinishedDownloadChunk) {
return
}
defer d.Close()
// If we the download of the chunk is marked as done or if we are shutting
// down, we close the downloader early to interrupt the download.
go func() {
select {
case <-w.renter.tg.StopChan():
case <-udc.download.completeChan:
}
d.Close()
}()
pieceData, err := d.Sector(udc.staticChunkMap[string(w.contract.HostPublicKey.Key)].root)
if err != nil {
w.renter.log.Debugln("worker failed to download sector:", err)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment