Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions internal/health/library_sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -596,8 +596,18 @@ func (lsw *LibrarySyncWorker) SyncLibrary(ctx context.Context, dryRun bool) *Dry
dbPathSet := syncMaps.dbPathSet

// Find files to add (in filesystem but not in database)
// Collect results via channel to avoid large slice allocations and lock contention
filesToAddChan := make(chan database.AutomaticHealthCheckRecord, 100)
var filesToAdd []database.AutomaticHealthCheckRecord
var filesToAddMu sync.Mutex

// Start a goroutine to collect results from the channel
done := make(chan struct{})
go func() {
for record := range filesToAddChan {
filesToAdd = append(filesToAdd, record)
}
close(done)
}()

// Get concurrency setting (default to 10 if not set)
concurrency := cfg.Health.LibrarySyncConcurrency
Expand All @@ -611,6 +621,8 @@ func (lsw *LibrarySyncWorker) SyncLibrary(ctx context.Context, dryRun bool) *Dry
for mountRelativePath := range metaFileSet {
select {
case <-ctx.Done():
p.Wait()
close(filesToAddChan)
return nil
default:
}
Expand Down Expand Up @@ -691,9 +703,7 @@ func (lsw *LibrarySyncWorker) SyncLibrary(ctx context.Context, dryRun bool) *Dry
}

if record != nil {
filesToAddMu.Lock()
filesToAdd = append(filesToAdd, *record)
filesToAddMu.Unlock()
filesToAddChan <- *record
}
}

Expand All @@ -703,8 +713,10 @@ func (lsw *LibrarySyncWorker) SyncLibrary(ctx context.Context, dryRun bool) *Dry
})
}

// Wait for all workers to complete
// Wait for all workers to complete and close results channel
p.Wait()
close(filesToAddChan)
<-done

// Additional cleanup of orphaned database records if enabled
// We no longer delete metadata files here for safety.
Expand Down
Loading