Fix failure to unlock something that is already unlocked
This commit is contained in:
parent
a08a102d15
commit
ce5685573f
1 changed files with 11 additions and 28 deletions
|
@ -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() {
|
||||||
|
lastUpdateTime := time.Time{}
|
||||||
|
for {
|
||||||
|
if time.Now().Sub(lastUpdateTime) >= q.config.TorrentListUpdateInterval {
|
||||||
err := q.updateTorrentList()
|
err := q.updateTorrentList()
|
||||||
if err != nil {
|
if err == nil {
|
||||||
|
lastUpdateTime = time.Now()
|
||||||
|
} else {
|
||||||
q.logger.Printf("Failed to update torrent list from rTorrent: '%s'", err)
|
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 {
|
|
||||||
downloadTorrents := []rtorrent.Torrent{}
|
|
||||||
for torrentHash := range q.downloadQueue {
|
|
||||||
torrent, exists := q.torrentList[torrentHash]
|
|
||||||
if !exists && !torrent.Completed {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
downloadTorrents = append(downloadTorrents, torrent)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
downloadTorrents := q.getFinishedTorrents()
|
||||||
if len(downloadTorrents) > 0 {
|
if len(downloadTorrents) > 0 {
|
||||||
q.downloadTorrents(downloadTorrents)
|
q.downloadTorrents(downloadTorrents)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue