Introduced Backend.Close method

This allows Backend implementations to free used resources.
Currently used for musicbrainzws2.Client
This commit is contained in:
Philipp Wolfer 2025-06-10 08:30:26 +02:00
parent c1a480a1a6
commit 499786cab9
No known key found for this signature in database
GPG key ID: 8FDF744D4919943B
14 changed files with 45 additions and 8 deletions

View file

@ -38,6 +38,8 @@ type DeezerApiBackend struct {
func (b *DeezerApiBackend) Name() string { return "deezer" } func (b *DeezerApiBackend) Name() string { return "deezer" }
func (b *DeezerApiBackend) Close() {}
func (b *DeezerApiBackend) Options() []models.BackendOption { func (b *DeezerApiBackend) Options() []models.BackendOption {
return []models.BackendOption{{ return []models.BackendOption{{
Name: "client-id", Name: "client-id",

View file

@ -43,6 +43,8 @@ type DeezerHistoryBackend struct {
func (b *DeezerHistoryBackend) Name() string { return "deezer-history" } func (b *DeezerHistoryBackend) Name() string { return "deezer-history" }
func (b *DeezerHistoryBackend) Close() {}
func (b *DeezerHistoryBackend) Options() []models.BackendOption { func (b *DeezerHistoryBackend) Options() []models.BackendOption {
return []models.BackendOption{{ return []models.BackendOption{{
Name: "file-path", Name: "file-path",

View file

@ -36,6 +36,8 @@ type DumpBackend struct {
func (b *DumpBackend) Name() string { return "dump" } func (b *DumpBackend) Name() string { return "dump" }
func (b *DumpBackend) Close() {}
func (b *DumpBackend) Options() []models.BackendOption { func (b *DumpBackend) Options() []models.BackendOption {
return []models.BackendOption{{ return []models.BackendOption{{
Name: "file-path", Name: "file-path",

View file

@ -36,6 +36,8 @@ type FunkwhaleApiBackend struct {
func (b *FunkwhaleApiBackend) Name() string { return "funkwhale" } func (b *FunkwhaleApiBackend) Name() string { return "funkwhale" }
func (b *FunkwhaleApiBackend) Close() {}
func (b *FunkwhaleApiBackend) Options() []models.BackendOption { func (b *FunkwhaleApiBackend) Options() []models.BackendOption {
return []models.BackendOption{{ return []models.BackendOption{{
Name: "server-url", Name: "server-url",

View file

@ -46,6 +46,8 @@ type JSPFBackend struct {
func (b *JSPFBackend) Name() string { return "jspf" } func (b *JSPFBackend) Name() string { return "jspf" }
func (b *JSPFBackend) Close() {}
func (b *JSPFBackend) Options() []models.BackendOption { func (b *JSPFBackend) Options() []models.BackendOption {
return []models.BackendOption{{ return []models.BackendOption{{
Name: "file-path", Name: "file-path",

View file

@ -46,6 +46,8 @@ type LastfmApiBackend struct {
func (b *LastfmApiBackend) Name() string { return "lastfm" } func (b *LastfmApiBackend) Name() string { return "lastfm" }
func (b *LastfmApiBackend) Close() {}
func (b *LastfmApiBackend) Options() []models.BackendOption { func (b *LastfmApiBackend) Options() []models.BackendOption {
return []models.BackendOption{{ return []models.BackendOption{{
Name: "username", Name: "username",

View file

@ -42,11 +42,17 @@ const (
type ListenBrainzArchiveBackend struct { type ListenBrainzArchiveBackend struct {
filePath string filePath string
lbClient listenbrainz.Client lbClient listenbrainz.Client
mbClient musicbrainzws2.Client mbClient *musicbrainzws2.Client
} }
func (b *ListenBrainzArchiveBackend) Name() string { return "listenbrainz-archive" } func (b *ListenBrainzArchiveBackend) Name() string { return "listenbrainz-archive" }
func (b *ListenBrainzArchiveBackend) Close() {
if b.mbClient != nil {
b.mbClient.Close()
}
}
func (b *ListenBrainzArchiveBackend) Options() []models.BackendOption { func (b *ListenBrainzArchiveBackend) Options() []models.BackendOption {
return []models.BackendOption{{ return []models.BackendOption{{
Name: "archive-path", Name: "archive-path",
@ -58,7 +64,7 @@ func (b *ListenBrainzArchiveBackend) Options() []models.BackendOption {
func (b *ListenBrainzArchiveBackend) InitConfig(config *config.ServiceConfig) error { func (b *ListenBrainzArchiveBackend) InitConfig(config *config.ServiceConfig) error {
b.filePath = config.GetString("archive-path") b.filePath = config.GetString("archive-path")
b.lbClient = listenbrainz.NewClient("", version.UserAgent()) b.lbClient = listenbrainz.NewClient("", version.UserAgent())
b.mbClient = *musicbrainzws2.NewClient(musicbrainzws2.AppInfo{ b.mbClient = musicbrainzws2.NewClient(musicbrainzws2.AppInfo{
Name: version.AppName, Name: version.AppName,
Version: version.AppVersion, Version: version.AppVersion,
URL: version.AppURL, URL: version.AppURL,
@ -191,7 +197,7 @@ func (b *ListenBrainzArchiveBackend) ExportLoves(
if len(batch) >= lovesBatchSize { if len(batch) >= lovesBatchSize {
// The dump does not contain track metadata. Extend it with additional // The dump does not contain track metadata. Extend it with additional
// lookups // lookups
loves, err := lbapi.ExtendTrackMetadata(ctx, &b.lbClient, &b.mbClient, &batch) loves, err := lbapi.ExtendTrackMetadata(ctx, &b.lbClient, b.mbClient, &batch)
if err != nil { if err != nil {
p.Export.Abort() p.Export.Abort()
progress <- p progress <- p
@ -205,7 +211,7 @@ func (b *ListenBrainzArchiveBackend) ExportLoves(
} }
} }
loves, err := lbapi.ExtendTrackMetadata(ctx, &b.lbClient, &b.mbClient, &batch) loves, err := lbapi.ExtendTrackMetadata(ctx, &b.lbClient, b.mbClient, &batch)
if err != nil { if err != nil {
p.Export.Abort() p.Export.Abort()
progress <- p progress <- p

View file

@ -36,12 +36,18 @@ const lovesBatchSize = listenbrainz.MaxItemsPerGet
type ListenBrainzApiBackend struct { type ListenBrainzApiBackend struct {
client listenbrainz.Client client listenbrainz.Client
mbClient musicbrainzws2.Client mbClient *musicbrainzws2.Client
username string username string
checkDuplicates bool checkDuplicates bool
existingMBIDs map[mbtypes.MBID]bool existingMBIDs map[mbtypes.MBID]bool
} }
func (b *ListenBrainzApiBackend) Close() {
if b.mbClient != nil {
b.mbClient.Close()
}
}
func (b *ListenBrainzApiBackend) Name() string { return "listenbrainz" } func (b *ListenBrainzApiBackend) Name() string { return "listenbrainz" }
func (b *ListenBrainzApiBackend) Options() []models.BackendOption { func (b *ListenBrainzApiBackend) Options() []models.BackendOption {
@ -62,7 +68,7 @@ func (b *ListenBrainzApiBackend) Options() []models.BackendOption {
func (b *ListenBrainzApiBackend) InitConfig(config *config.ServiceConfig) error { func (b *ListenBrainzApiBackend) InitConfig(config *config.ServiceConfig) error {
b.client = listenbrainz.NewClient(config.GetString("token"), version.UserAgent()) b.client = listenbrainz.NewClient(config.GetString("token"), version.UserAgent())
b.mbClient = *musicbrainzws2.NewClient(musicbrainzws2.AppInfo{ b.mbClient = musicbrainzws2.NewClient(musicbrainzws2.AppInfo{
Name: version.AppName, Name: version.AppName,
Version: version.AppVersion, Version: version.AppVersion,
URL: version.AppURL, URL: version.AppURL,
@ -261,7 +267,7 @@ out:
// Missing track metadata indicates that the recording MBID is no // Missing track metadata indicates that the recording MBID is no
// longer available and might have been merged. Try fetching details // longer available and might have been merged. Try fetching details
// from MusicBrainz. // from MusicBrainz.
lovesBatch, err := ExtendTrackMetadata(ctx, &b.client, &b.mbClient, &batch) lovesBatch, err := ExtendTrackMetadata(ctx, &b.client, b.mbClient, &batch)
if err != nil { if err != nil {
results <- models.LovesResult{Error: err} results <- models.LovesResult{Error: err}
return return
@ -276,7 +282,7 @@ out:
offset += listenbrainz.MaxItemsPerGet offset += listenbrainz.MaxItemsPerGet
} }
lovesBatch, err := ExtendTrackMetadata(ctx, &b.client, &b.mbClient, &batch) lovesBatch, err := ExtendTrackMetadata(ctx, &b.client, b.mbClient, &batch)
if err != nil { if err != nil {
results <- models.LovesResult{Error: err} results <- models.LovesResult{Error: err}
return return

View file

@ -35,6 +35,8 @@ type MalojaApiBackend struct {
func (b *MalojaApiBackend) Name() string { return "maloja" } func (b *MalojaApiBackend) Name() string { return "maloja" }
func (b *MalojaApiBackend) Close() {}
func (b *MalojaApiBackend) Options() []models.BackendOption { func (b *MalojaApiBackend) Options() []models.BackendOption {
return []models.BackendOption{{ return []models.BackendOption{{
Name: "server-url", Name: "server-url",

View file

@ -41,6 +41,8 @@ type ScrobblerLogBackend struct {
func (b *ScrobblerLogBackend) Name() string { return "scrobbler-log" } func (b *ScrobblerLogBackend) Name() string { return "scrobbler-log" }
func (b *ScrobblerLogBackend) Close() {}
func (b *ScrobblerLogBackend) Options() []models.BackendOption { func (b *ScrobblerLogBackend) Options() []models.BackendOption {
return []models.BackendOption{{ return []models.BackendOption{{
Name: "file-path", Name: "file-path",

View file

@ -41,6 +41,8 @@ type SpotifyApiBackend struct {
func (b *SpotifyApiBackend) Name() string { return "spotify" } func (b *SpotifyApiBackend) Name() string { return "spotify" }
func (b *SpotifyApiBackend) Close() {}
func (b *SpotifyApiBackend) Options() []models.BackendOption { func (b *SpotifyApiBackend) Options() []models.BackendOption {
return []models.BackendOption{{ return []models.BackendOption{{
Name: "client-id", Name: "client-id",

View file

@ -36,6 +36,8 @@ type SpotifyHistoryBackend struct {
func (b *SpotifyHistoryBackend) Name() string { return "spotify-history" } func (b *SpotifyHistoryBackend) Name() string { return "spotify-history" }
func (b *SpotifyHistoryBackend) Close() {}
func (b *SpotifyHistoryBackend) Options() []models.BackendOption { func (b *SpotifyHistoryBackend) Options() []models.BackendOption {
return []models.BackendOption{{ return []models.BackendOption{{
Name: "archive-path", Name: "archive-path",

View file

@ -37,6 +37,8 @@ type SubsonicApiBackend struct {
func (b *SubsonicApiBackend) Name() string { return "subsonic" } func (b *SubsonicApiBackend) Name() string { return "subsonic" }
func (b *SubsonicApiBackend) Close() {}
func (b *SubsonicApiBackend) Options() []models.BackendOption { func (b *SubsonicApiBackend) Options() []models.BackendOption {
return []models.BackendOption{{ return []models.BackendOption{{
Name: "server-url", Name: "server-url",

View file

@ -35,6 +35,9 @@ type Backend interface {
// Return configuration options // Return configuration options
Options() []BackendOption Options() []BackendOption
// Free all resources of the backend
Close()
} }
type ImportBackend interface { type ImportBackend interface {