Optimize repair

This commit is contained in:
Ben Sarmiento
2023-11-26 19:00:01 +01:00
parent 7a56bbcea0
commit a7623de410
2 changed files with 17 additions and 33 deletions

View File

@@ -4,7 +4,6 @@ import (
"bufio" "bufio"
"encoding/gob" "encoding/gob"
"fmt" "fmt"
"hash/fnv"
"math" "math"
"os" "os"
"path/filepath" "path/filepath"
@@ -371,11 +370,10 @@ func (t *TorrentManager) getMoreInfo(rdTorrent realdebrid.Torrent) *Torrent {
continue continue
} }
selectedFiles = append(selectedFiles, &File{ selectedFiles = append(selectedFiles, &File{
File: file, File: file,
Added: info.Added, Added: info.Added,
Ended: info.Ended, Ended: info.Ended,
Link: "", // no link yet Link: "", // no link yet
ZurgFS: hashStringToFh(file.Path + info.Hash),
}) })
} }
if len(selectedFiles) > len(info.Links) && info.Progress == 100 { if len(selectedFiles) > len(info.Links) && info.Progress == 100 {
@@ -404,12 +402,6 @@ func (t *TorrentManager) getMoreInfo(rdTorrent realdebrid.Torrent) *Torrent {
return &torrent return &torrent
} }
func hashStringToFh(s string) (fh uint64) {
hasher := fnv.New64a()
_, _ = hasher.Write([]byte(s)) // Write the string to the hasher; ignoring error as it never returns an error
return hasher.Sum64() // Returns a 64-bit hash value
}
func (t *TorrentManager) getName(name, originalName string) string { func (t *TorrentManager) getName(name, originalName string) string {
if t.cfg.EnableRetainRDTorrentName() { if t.cfg.EnableRetainRDTorrentName() {
return name return name
@@ -478,20 +470,15 @@ func (t *TorrentManager) organizeChaos(links []string, selectedFiles []*File) ([
wg.Add(1) wg.Add(1)
link := link // redeclare to avoid closure on loop variable link := link // redeclare to avoid closure on loop variable
// Use the existing worker pool to submit tasks // Use the existing worker pool to submit tasks
err := t.antsPool.Submit(func() { _ = t.antsPool.Submit(func() {
defer wg.Done() defer wg.Done()
resp := t.api.UnrestrictUntilOk(link) resp := t.api.UnrestrictUntilOk(link)
resultsChan <- Result{Response: resp} resultsChan <- Result{Response: resp}
}) })
if err != nil {
t.log.Errorf("Error submitting task to worker pool: %v", err)
}
} }
go func() { wg.Wait()
wg.Wait() close(resultsChan)
close(resultsChan)
}()
isChaotic := false isChaotic := false
for result := range resultsChan { for result := range resultsChan {
@@ -515,10 +502,9 @@ func (t *TorrentManager) organizeChaos(links []string, selectedFiles []*File) ([
Bytes: result.Response.Filesize, Bytes: result.Response.Filesize,
Selected: 1, Selected: 1,
}, },
Added: now, Added: now,
Ended: now, Ended: now,
Link: result.Response.Link, Link: result.Response.Link,
ZurgFS: hashStringToFh(result.Response.Filename),
}) })
} else { } else {
isChaotic = true isChaotic = true
@@ -605,11 +591,10 @@ func (t *TorrentManager) Repair(accessKey string) {
streamableCount++ streamableCount++
} }
fileCopy := &File{ fileCopy := &File{
File: file.File, File: file.File,
Added: file.Added, Added: file.Added,
Ended: file.Ended, Ended: file.Ended,
Link: file.Link, Link: file.Link,
ZurgFS: file.ZurgFS,
} }
selectedFiles = append(selectedFiles, fileCopy) selectedFiles = append(selectedFiles, fileCopy)
if strings.HasPrefix(fileCopy.Link, "http") { if strings.HasPrefix(fileCopy.Link, "http") {

View File

@@ -34,8 +34,7 @@ func (t *Torrent) AllInProgress() bool {
type File struct { type File struct {
realdebrid.File realdebrid.File
Added string Added string
Ended string Ended string
Link string Link string
ZurgFS uint64
} }