Fix failure to unlock something that is already unlocked

This commit is contained in:
Tony Blyler 2016-05-14 16:12:24 -04:00
parent a08a102d15
commit ce5685573f
No known key found for this signature in database
GPG key ID: 25C9D3A655D1A65C

View file

@ -165,10 +165,6 @@ func (q *Queue) Close() []error {
} }
func (q *Queue) updateTorrentList() error { func (q *Queue) updateTorrentList() error {
// lock for torrentList
q.lock.Lock()
defer q.lock.Unlock()
torrents, err := q.rtClient.GetTorrents(rtorrent.ViewMain) torrents, err := q.rtClient.GetTorrents(rtorrent.ViewMain)
if err != nil { if err != nil {
return err return err
@ -206,15 +202,14 @@ func (q *Queue) addTorrentFilePath(path string) error {
} }
func (q *Queue) getFinishedTorrents() []rtorrent.Torrent { func (q *Queue) getFinishedTorrents() []rtorrent.Torrent {
// lock for torrentList
q.lock.RLock() q.lock.RLock()
defer q.lock.RUnlock()
torrents := []rtorrent.Torrent{} torrents := []rtorrent.Torrent{}
for hash, torrentPath := range q.downloadQueue { for hash, torrentPath := range q.downloadQueue {
torrent, exists := q.torrentList[hash] torrent, exists := q.torrentList[hash]
if !exists { if !exists {
q.lock.RUnlock()
err := q.addTorrentFilePath(torrentPath) err := q.addTorrentFilePath(torrentPath)
q.lock.RLock()
if err != nil { if err != nil {
q.logger.Printf("Unable to add torrent '%s' error '%s'", torrentPath, err) q.logger.Printf("Unable to add torrent '%s' error '%s'", torrentPath, err)
} }
@ -233,7 +228,6 @@ func (q *Queue) getFinishedTorrents() []rtorrent.Torrent {
} }
func (q *Queue) downloadTorrents(torrents []rtorrent.Torrent) { func (q *Queue) downloadTorrents(torrents []rtorrent.Torrent) {
// lock for downloadQueue
q.lock.RLock() q.lock.RLock()
defer q.lock.RUnlock() defer q.lock.RUnlock()
@ -338,6 +332,7 @@ func (q *Queue) downloadTorrents(torrents []rtorrent.Torrent) {
q.lock.Lock() q.lock.Lock()
delete(q.downloadQueue, finishedHash) delete(q.downloadQueue, finishedHash)
q.lock.Unlock() q.lock.Unlock()
q.lock.RLock()
} }
running-- running--
@ -370,30 +365,18 @@ func (q *Queue) Run(stop <-chan bool) {
} }
go func() { go func() {
err := q.updateTorrentList() lastUpdateTime := time.Time{}
if err != nil {
q.logger.Printf("Failed to update torrent list from rTorrent: '%s'", err)
}
if q.config.TorrentListUpdateInterval == 0 {
time.Sleep(time.Minute)
} else {
time.Sleep(q.config.TorrentListUpdateInterval)
}
}()
go func() {
for { for {
downloadTorrents := []rtorrent.Torrent{} if time.Now().Sub(lastUpdateTime) >= q.config.TorrentListUpdateInterval {
for torrentHash := range q.downloadQueue { err := q.updateTorrentList()
torrent, exists := q.torrentList[torrentHash] if err == nil {
if !exists && !torrent.Completed { lastUpdateTime = time.Now()
continue } else {
q.logger.Printf("Failed to update torrent list from rTorrent: '%s'", err)
} }
downloadTorrents = append(downloadTorrents, torrent)
} }
downloadTorrents := q.getFinishedTorrents()
if len(downloadTorrents) > 0 { if len(downloadTorrents) > 0 {
q.downloadTorrents(downloadTorrents) q.downloadTorrents(downloadTorrents)
} }