From b018d7b634ea60392d9a15ef3b35b7aa0c59e892 Mon Sep 17 00:00:00 2001 From: Deluan Date: Wed, 25 Mar 2026 07:58:44 -0400 Subject: [PATCH] feat(stream): wire TranscodingThrottle into MediaStreamer --- cmd/wire_gen.go | 6 ++++-- core/stream/media_streamer.go | 5 +++-- core/stream/media_streamer_test.go | 3 ++- core/stream/throttle.go | 10 ++++++++++ core/wire_providers.go | 1 + 5 files changed, 20 insertions(+), 5 deletions(-) diff --git a/cmd/wire_gen.go b/cmd/wire_gen.go index 5b9fd648f..93ff02463 100644 --- a/cmd/wire_gen.go +++ b/cmd/wire_gen.go @@ -96,7 +96,8 @@ func CreateSubsonicAPIRouter(ctx context.Context) *subsonic.Router { provider := external.NewProvider(dataStore, agentsAgents) artworkArtwork := artwork.NewArtwork(dataStore, fileCache, fFmpeg, provider) transcodingCache := stream.GetTranscodingCache() - mediaStreamer := stream.NewMediaStreamer(dataStore, fFmpeg, transcodingCache) + transcodingThrottle := stream.GetTranscodingThrottle() + mediaStreamer := stream.NewMediaStreamer(dataStore, fFmpeg, transcodingCache, transcodingThrottle) share := core.NewShare(dataStore) archiver := core.NewArchiver(mediaStreamer, dataStore, share) players := core.NewPlayers(dataStore) @@ -124,7 +125,8 @@ func CreatePublicRouter() *public.Router { provider := external.NewProvider(dataStore, agentsAgents) artworkArtwork := artwork.NewArtwork(dataStore, fileCache, fFmpeg, provider) transcodingCache := stream.GetTranscodingCache() - mediaStreamer := stream.NewMediaStreamer(dataStore, fFmpeg, transcodingCache) + transcodingThrottle := stream.GetTranscodingThrottle() + mediaStreamer := stream.NewMediaStreamer(dataStore, fFmpeg, transcodingCache, transcodingThrottle) share := core.NewShare(dataStore) archiver := core.NewArchiver(mediaStreamer, dataStore, share) router := public.New(dataStore, artworkArtwork, mediaStreamer, share, archiver) diff --git a/core/stream/media_streamer.go b/core/stream/media_streamer.go index de03b4d2f..84fa84321 100644 --- a/core/stream/media_streamer.go +++ b/core/stream/media_streamer.go @@ -27,14 +27,15 @@ type MediaStreamer interface { type TranscodingCache cache.FileCache -func NewMediaStreamer(ds model.DataStore, t ffmpeg.FFmpeg, cache TranscodingCache) MediaStreamer { - return &mediaStreamer{ds: ds, transcoder: t, cache: cache} +func NewMediaStreamer(ds model.DataStore, t ffmpeg.FFmpeg, cache TranscodingCache, throttle *TranscodingThrottle) MediaStreamer { + return &mediaStreamer{ds: ds, transcoder: t, cache: cache, throttle: throttle} } type mediaStreamer struct { ds model.DataStore transcoder ffmpeg.FFmpeg cache cache.FileCache + throttle *TranscodingThrottle } type streamJob struct { diff --git a/core/stream/media_streamer_test.go b/core/stream/media_streamer_test.go index 1bc21e239..c7c7268fd 100644 --- a/core/stream/media_streamer_test.go +++ b/core/stream/media_streamer_test.go @@ -4,6 +4,7 @@ import ( "context" "io" "os" + "time" "github.com/navidrome/navidrome/conf" "github.com/navidrome/navidrome/conf/configtest" @@ -31,7 +32,7 @@ var _ = Describe("MediaStreamer", func() { }) testCache := stream.NewTranscodingCache() Eventually(func() bool { return testCache.Available(context.TODO()) }).Should(BeTrue()) - streamer = stream.NewMediaStreamer(ds, ffmpeg, testCache) + streamer = stream.NewMediaStreamer(ds, ffmpeg, testCache, stream.NewTranscodingThrottle(0, 100, time.Minute)) }) AfterEach(func() { _ = os.RemoveAll(conf.Server.CacheFolder) diff --git a/core/stream/throttle.go b/core/stream/throttle.go index acf219dc8..d9386e7b9 100644 --- a/core/stream/throttle.go +++ b/core/stream/throttle.go @@ -8,6 +8,7 @@ import ( "sync/atomic" "time" + "github.com/navidrome/navidrome/conf" "github.com/navidrome/navidrome/log" "golang.org/x/sync/semaphore" ) @@ -90,3 +91,12 @@ func (r *releaseOnClose) Close() error { r.once.Do(r.release) return err } + +// GetTranscodingThrottle creates a TranscodingThrottle from the current configuration. +func GetTranscodingThrottle() *TranscodingThrottle { + return NewTranscodingThrottle( + conf.Server.MaxConcurrentTranscodes, + conf.Server.DevTranscodeThrottleBacklogLimit, + conf.Server.DevTranscodeThrottleBacklogTimeout, + ) +} diff --git a/core/wire_providers.go b/core/wire_providers.go index 276d9556a..e53b789be 100644 --- a/core/wire_providers.go +++ b/core/wire_providers.go @@ -16,6 +16,7 @@ import ( var Set = wire.NewSet( stream.NewMediaStreamer, stream.GetTranscodingCache, + stream.GetTranscodingThrottle, NewArchiver, NewPlayers, NewShare,