1
0
mirror of https://github.com/taigrr/wtf synced 2025-01-18 04:03:14 -08:00

Merge pull request #690 from wtfutil/20191011-transmission-race-conditions

Fix some of the transmission module race conditions
This commit is contained in:
Chris Cummer 2019-10-11 12:16:00 -07:00 committed by GitHub
commit e98b15ca2c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 14 deletions

View File

@ -9,15 +9,24 @@ import (
"github.com/wtfutil/wtf/utils"
)
func (widget *Widget) display() {
widget.ScrollableWidget.Redraw(widget.content)
}
func (widget *Widget) content() (string, string, bool) {
widget.mu.Lock()
defer widget.mu.Unlock()
title := widget.CommonSettings().Title
if widget.err != nil {
return title, widget.err.Error(), true
}
data := widget.torrents
if data == nil || len(data) == 0 {
return title, "No data", false
}
str := ""
for idx, torrent := range data {
@ -38,10 +47,6 @@ func (widget *Widget) content() (string, string, bool) {
return title, str, false
}
func (widget *Widget) display() {
widget.ScrollableWidget.Redraw(widget.content)
}
func (widget *Widget) prettyTorrentName(name string) string {
str := strings.Replace(name, "[", "(", -1)
str = strings.Replace(str, "]", ")", -1)

View File

@ -2,6 +2,7 @@ package transmission
import (
"errors"
"sync"
"github.com/hekmon/transmissionrpc"
"github.com/rivo/tview"
@ -15,6 +16,7 @@ type Widget struct {
client *transmissionrpc.Client
settings *Settings
mu sync.Mutex
torrents []*transmissionrpc.Torrent
err error
}
@ -43,6 +45,9 @@ func NewWidget(app *tview.Application, pages *tview.Pages, settings *Settings) *
// Fetch retrieves torrent data from the Transmission daemon
func (widget *Widget) Fetch() ([]*transmissionrpc.Torrent, error) {
widget.mu.Lock()
widget.mu.Unlock()
if widget.client == nil {
return nil, errors.New("client was not initialized")
}
@ -58,16 +63,18 @@ func (widget *Widget) Fetch() ([]*transmissionrpc.Torrent, error) {
// Refresh updates the data for this widget and displays it onscreen
func (widget *Widget) Refresh() {
torrents, err := widget.Fetch()
if err != nil {
widget.err = err
widget.torrents = nil
widget.SetItemCount(0)
} else {
widget.err = nil
widget.torrents = torrents
widget.SetItemCount(len(torrents))
count := 0
if err == nil {
count = len(torrents)
}
widget.mu.Lock()
widget.err = err
widget.torrents = torrents
widget.SetItemCount(count)
widget.mu.Unlock()
widget.display()
}
@ -96,6 +103,9 @@ func (widget *Widget) Unselect() {
// buildClient creates a persisten transmission client
func buildClient(widget *Widget) {
widget.mu.Lock()
defer widget.mu.Unlock()
client, err := transmissionrpc.New(widget.settings.host, widget.settings.username, widget.settings.password,
&transmissionrpc.AdvancedConfig{
Port: widget.settings.port,
@ -103,9 +113,8 @@ func buildClient(widget *Widget) {
if err != nil {
client = nil
}
widget.client = client
widget.Refresh()
widget.client = client
}
func (widget *Widget) currentTorrent() *transmissionrpc.Torrent {