Fix download deadlock

This commit is contained in:
Tony Blyler 2016-05-14 10:33:04 -04:00
parent 95ce7fccee
commit 93cf0597ed
No known key found for this signature in database
GPG key ID: 25C9D3A655D1A65C

View file

@ -236,7 +236,9 @@ func (q *Queue) downloadTorrents(torrents []rtorrent.Torrent) {
q.lock.RLock() q.lock.RLock()
defer q.lock.RUnlock() defer q.lock.RUnlock()
done := make(chan bool, q.config.ConcurrentDownloads) // this will keep track of the torrent hashes that have finished
// will return an empty string on a failed download
done := make(chan string, q.config.ConcurrentDownloads)
var running uint var running uint
for _, torrent := range torrents { for _, torrent := range torrents {
if !torrent.Completed { if !torrent.Completed {
@ -251,7 +253,15 @@ func (q *Queue) downloadTorrents(torrents []rtorrent.Torrent) {
} }
if running >= q.config.ConcurrentDownloads { if running >= q.config.ConcurrentDownloads {
<-done finishedHash := <-done
if finishedHash != "" {
q.lock.RUnlock()
q.lock.Lock()
delete(q.downloadQueue, finishedHash)
q.lock.Unlock()
q.lock.RLock()
}
running-- running--
} }
@ -282,7 +292,7 @@ func (q *Queue) downloadTorrents(torrents []rtorrent.Torrent) {
err := q.sftpClient.Mirror(torrent.Path, downloadPath) err := q.sftpClient.Mirror(torrent.Path, downloadPath)
if err != nil { if err != nil {
q.logger.Printf("Failed to download '%s' to '%s' error '%s'", torrent.Path, downloadPath, err) q.logger.Printf("Failed to download '%s' to '%s' error '%s'", torrent.Path, downloadPath, err)
done <- false done <- ""
return return
} }
@ -294,7 +304,7 @@ func (q *Queue) downloadTorrents(torrents []rtorrent.Torrent) {
err = os.Rename(downFullPath, destFullPath) err = os.Rename(downFullPath, destFullPath)
if err != nil { if err != nil {
q.logger.Printf("Failed to move temp path '%s' to dest path '%s' error '%s'", downFullPath, destFullPath, err) q.logger.Printf("Failed to move temp path '%s' to dest path '%s' error '%s'", downFullPath, destFullPath, err)
done <- false done <- ""
return return
} }
} }
@ -315,18 +325,20 @@ func (q *Queue) downloadTorrents(torrents []rtorrent.Torrent) {
q.logger.Printf("Successfully downloaded '%s' (%s)", torrent.Name, torrentFilePath) q.logger.Printf("Successfully downloaded '%s' (%s)", torrent.Name, torrentFilePath)
q.lock.RUnlock() done <- torrent.Hash
q.lock.Lock()
delete(q.downloadQueue, torrent.Hash)
q.lock.Unlock()
q.lock.RLock()
done <- true
}(torrentFilePath, downloadPath, torrent) }(torrentFilePath, downloadPath, torrent)
running++ running++
} }
for running > 0 { for running > 0 {
<-done finishedHash := <-done
if finishedHash != "" {
q.lock.RUnlock()
q.lock.Lock()
delete(q.downloadQueue, finishedHash)
q.lock.Unlock()
}
running-- running--
} }
} }