- Add more limit to number of gorutines

- Add gorutine stats to logs
- Fix issues with repair
This commit is contained in:
Mukhtar Akere
2025-03-27 08:24:40 +01:00
parent 7bd38736b1
commit d49fbea60f
11 changed files with 163 additions and 139 deletions

View File

@@ -1,8 +1,10 @@
package arr
import (
"context"
"fmt"
"github.com/goccy/go-json"
"golang.org/x/sync/errgroup"
"net/http"
"strconv"
"strings"
@@ -155,20 +157,32 @@ func (a *Arr) searchSonarr(files []ContentFile) error {
id := fmt.Sprintf("%d-%d", f.Id, f.SeasonNumber)
ids[id] = nil
}
errs := make(chan error, len(ids))
g, ctx := errgroup.WithContext(context.Background())
// Limit concurrent goroutines
g.SetLimit(10)
for id := range ids {
go func() {
id := id
g.Go(func() error {
select {
case <-ctx.Done():
return ctx.Err()
default:
}
parts := strings.Split(id, "-")
if len(parts) != 2 {
return
return fmt.Errorf("invalid id: %s", id)
}
seriesId, err := strconv.Atoi(parts[0])
if err != nil {
return
return err
}
seasonNumber, err := strconv.Atoi(parts[1])
if err != nil {
return
return err
}
payload := sonarrSearch{
Name: "SeasonSearch",
@@ -177,20 +191,16 @@ func (a *Arr) searchSonarr(files []ContentFile) error {
}
resp, err := a.Request(http.MethodPost, "api/v3/command", payload)
if err != nil {
errs <- fmt.Errorf("failed to automatic search: %v", err)
return
return fmt.Errorf("failed to automatic search: %v", err)
}
if resp.StatusCode >= 300 || resp.StatusCode < 200 {
errs <- fmt.Errorf("failed to automatic search. Status Code: %s", resp.Status)
return
return fmt.Errorf("failed to automatic search. Status Code: %s", resp.Status)
}
}()
return nil
})
}
for range ids {
err := <-errs
if err != nil {
return err
}
if err := g.Wait(); err != nil {
return err
}
return nil
}