From ed36879cdaf9f8a9c8743b1c73d8cfb8c0a0a947 Mon Sep 17 00:00:00 2001 From: Drew Weymouth Date: Wed, 5 Mar 2025 07:47:34 -0800 Subject: [PATCH] Fix #571:make timeout configurable and bump default to 15 sec --- backend/app.go | 3 ++- backend/config.go | 3 ++- backend/lrclib.go | 7 ++++--- backend/servermanager.go | 9 +++++---- ui/controller/controller.go | 5 +++-- 5 files changed, 16 insertions(+), 11 deletions(-) diff --git a/backend/app.go b/backend/app.go index 84c27ff..d93309f 100644 --- a/backend/app.go +++ b/backend/app.go @@ -148,7 +148,8 @@ func StartupApp(appName, displayAppName, appVersion, appVersionTag, latestReleas _, _ = a.ImageManager.GetCoverThumbnail(coverID) }) if a.Config.Application.EnableLrcLib { - a.LrcLibFetcher = NewLrcLibFetcher(a.cacheDir, a.Config.Application.CustomLrcLibUrl) + timeout := time.Duration(a.Config.Application.RequestTimeoutSeconds) * time.Second + a.LrcLibFetcher = NewLrcLibFetcher(a.cacheDir, a.Config.Application.CustomLrcLibUrl, timeout) } a.PlaybackManager.OnPlaying(func() { diff --git a/backend/config.go b/backend/config.go index 604edf5..02d7f5b 100644 --- a/backend/config.go +++ b/backend/config.go @@ -54,8 +54,8 @@ type AppConfig struct { Language string DisableDPIDetection bool EnableAutoUpdateChecker bool + RequestTimeoutSeconds int - // Experimental - may be removed in future FontNormalTTF string FontBoldTTF string UIScaleSize string @@ -191,6 +191,7 @@ func DefaultConfig(appVersionTag string) *Config { EnqueueBatchSize: 100, Language: "auto", EnableAutoUpdateChecker: true, + RequestTimeoutSeconds: 15, }, AlbumPage: AlbumPageConfig{ TracklistColumns: []string{"Artist", "Time", "Plays", "Favorite", "Rating"}, diff --git a/backend/lrclib.go b/backend/lrclib.go index 685583d..feb5234 100644 --- a/backend/lrclib.go +++ b/backend/lrclib.go @@ -26,12 +26,13 @@ const lrclibCacheFolder = "lrclib" type LrcLibFetcher struct { cachePath string customLrcLibUrl string + timeout time.Duration } -func NewLrcLibFetcher(baseCacheDir string, customLrcLibUrl string) *LrcLibFetcher { +func NewLrcLibFetcher(baseCacheDir string, customLrcLibUrl string, timeout time.Duration) *LrcLibFetcher { cachePath := filepath.Join(baseCacheDir, lrclibCacheFolder) configdir.MakePath(cachePath) - return &LrcLibFetcher{cachePath: cachePath, customLrcLibUrl: customLrcLibUrl} + return &LrcLibFetcher{cachePath: cachePath, customLrcLibUrl: customLrcLibUrl, timeout: timeout} } func (l *LrcLibFetcher) FetchLrcLibLyrics(name, artist, album string, durationSecs int) (*mediaprovider.Lyrics, error) { @@ -67,7 +68,7 @@ func (l *LrcLibFetcher) FetchLrcLibLyrics(name, artist, album string, durationSe } func (l *LrcLibFetcher) fetchFromServer(name, artist, album string, durationSecs int) (*mediaprovider.Lyrics, error) { - ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) + ctx, cancel := context.WithTimeout(context.Background(), l.timeout) defer cancel() lrclibUrl := l.getLrclibUrl() diff --git a/backend/servermanager.go b/backend/servermanager.go index ef4c3c7..3bba6ef 100644 --- a/backend/servermanager.go +++ b/backend/servermanager.go @@ -177,9 +177,10 @@ func (s *ServerManager) SetServerPassword(server *ServerConfig, password string) func (s *ServerManager) connect(connection ServerConnection, password string) (mediaprovider.Server, error) { var cli, altCli mediaprovider.Server + timeout := time.Second * time.Duration(s.config.Application.RequestTimeoutSeconds) if connection.ServerType == ServerTypeJellyfin { - client, err := jellyfin.NewClient(connection.Hostname, res.AppName, res.AppVersion, jellyfin.WithTimeout(10*time.Second)) + client, err := jellyfin.NewClient(connection.Hostname, res.AppName, res.AppVersion, jellyfin.WithTimeout(timeout)) if err != nil { log.Printf("error creating Jellyfin client: %s", err.Error()) return nil, err @@ -190,7 +191,7 @@ func (s *ServerManager) connect(connection ServerConnection, password string) (m } if connection.AltHostname != "" { - altClient, err := jellyfin.NewClient(connection.AltHostname, res.AppName, res.AppVersion, jellyfin.WithTimeout(10*time.Second)) + altClient, err := jellyfin.NewClient(connection.AltHostname, res.AppName, res.AppVersion, jellyfin.WithTimeout(timeout)) if err != nil { log.Printf("error creating Jellyfin alternative client: %s", err.Error()) return nil, err @@ -205,7 +206,7 @@ func (s *ServerManager) connect(connection ServerConnection, password string) (m cli = &subsonicMP.SubsonicServer{ Client: subsonic.Client{ UserAgent: ua, - Client: &http.Client{Timeout: 10 * time.Second}, + Client: &http.Client{Timeout: timeout}, BaseUrl: connection.Hostname, User: connection.Username, PasswordAuth: connection.LegacyAuth, @@ -216,7 +217,7 @@ func (s *ServerManager) connect(connection ServerConnection, password string) (m altCli = &subsonicMP.SubsonicServer{ Client: subsonic.Client{ UserAgent: ua, - Client: &http.Client{Timeout: 10 * time.Second}, + Client: &http.Client{Timeout: timeout}, BaseUrl: connection.AltHostname, User: connection.Username, PasswordAuth: connection.LegacyAuth, diff --git a/ui/controller/controller.go b/ui/controller/controller.go index 3278c21..b6ab0b2 100644 --- a/ui/controller/controller.go +++ b/ui/controller/controller.go @@ -691,9 +691,10 @@ func (c *Controller) trySetPasswordAndConnectToServer(server *backend.ServerConf return c.tryConnectToServer(context.Background(), server, password) } -// try to connect to the given server, with a 10 second timeout added to the context +// try to connect to the given server, with the configured timeout added to the context func (c *Controller) tryConnectToServer(ctx context.Context, server *backend.ServerConfig, password string) error { - ctx, cancel := context.WithTimeout(ctx, 10*time.Second) + timeout := time.Duration(c.App.Config.Application.RequestTimeoutSeconds) * time.Second + ctx, cancel := context.WithTimeout(ctx, timeout) defer cancel() if err := c.App.ServerManager.TestConnectionAndAuth(ctx, server.ServerConnection, password); err != nil { return err