Use the chunk manager properly

This commit is contained in:
Ben Sarmiento
2023-11-18 01:51:25 +01:00
parent 4da9416bec
commit b669f8d673
10 changed files with 30 additions and 31 deletions

View File

@@ -9,6 +9,7 @@ import (
"time"
"github.com/debridmediamanager.com/zurg/internal/torrent"
zurghttp "github.com/debridmediamanager.com/zurg/pkg/http"
"github.com/debridmediamanager.com/zurg/pkg/logutil"
"go.uber.org/zap"
"golang.org/x/sys/unix"
@@ -23,12 +24,13 @@ type Downloader struct {
storage *Storage
torMgr *torrent.TorrentManager
log *zap.SugaredLogger
client *zurghttp.HTTPClient
}
type DownloadCallback func(error, []byte)
// NewDownloader creates a new download manager
func NewDownloader(threads int, storage *Storage, bufferSize int64, torMgr *torrent.TorrentManager) (*Downloader, error) {
func NewDownloader(threads int, storage *Storage, bufferSize int64, torMgr *torrent.TorrentManager, client *zurghttp.HTTPClient) (*Downloader, error) {
rlog := logutil.NewLogger()
log := rlog.Named("downloader")
@@ -38,6 +40,7 @@ func NewDownloader(threads int, storage *Storage, bufferSize int64, torMgr *torr
callbacks: make(map[RequestID][]DownloadCallback, 100),
storage: storage,
torMgr: torMgr,
client: client,
log: log,
}
@@ -76,7 +79,6 @@ func (d *Downloader) thread(n int) {
}
func (d *Downloader) download(req *Request, buffer []byte) {
d.log.Debugf("Starting download %v (preload: %v)", req.id, req.preload)
err := d.downloadFromAPI(req, buffer, 0)
d.lock.Lock()
@@ -137,7 +139,7 @@ func (d *Downloader) downloadFromAPI(request *Request, buffer []byte, delay int6
d.log.Debugf("response read error: %v", err)
return fmt.Errorf("could not read objects %s %s API response", request.file.Path, request.file.Link)
}
d.log.Debugf("Downloaded %v bytes of %s %s", n, request.file.Path, request.file.Link)
d.log.Debugf("Downloaded %v bytes of %s", n, request.file.Path)
return nil
}

View File

@@ -6,8 +6,8 @@ import (
"hash/fnv"
"os"
"github.com/debridmediamanager.com/zurg/internal/config"
"github.com/debridmediamanager.com/zurg/internal/torrent"
zurghttp "github.com/debridmediamanager.com/zurg/pkg/http"
)
// Manager manages chunks on disk
@@ -59,7 +59,7 @@ func NewManager(
loadThreads,
maxChunks int,
torMgr *torrent.TorrentManager,
cfg config.ConfigInterface) (*Manager, error) {
client *zurghttp.HTTPClient) (*Manager, error) {
pageSize := int64(os.Getpagesize())
if chunkSize < pageSize {
@@ -82,7 +82,7 @@ func NewManager(
return nil, err
}
downloader, err := NewDownloader(loadThreads, storage, chunkSize, torMgr)
downloader, err := NewDownloader(loadThreads, storage, chunkSize, torMgr, client)
if nil != err {
return nil, err
}

View File

@@ -14,7 +14,7 @@ type HTTPClient struct {
Client *http.Client
MaxRetries int
Backoff func(attempt int) time.Duration
CheckRespStatus func(resp *http.Response, err error, log *zap.SugaredLogger) bool
CheckRespStatus func(resp *http.Response, err error) bool
BearerToken string
log *zap.SugaredLogger
config config.ConfigInterface
@@ -34,7 +34,7 @@ func (r *HTTPClient) Do(req *http.Request) (*http.Response, error) {
var err error
for attempt := 0; attempt < r.MaxRetries; attempt++ {
resp, err = r.Client.Do(req)
if !r.CheckRespStatus(resp, err, r.log) {
if !r.CheckRespStatus(resp, err) {
return resp, err
}
time.Sleep(r.Backoff(attempt))
@@ -50,7 +50,7 @@ func NewHTTPClient(token string, maxRetries int, cfg config.ConfigInterface) *HT
Backoff: func(attempt int) time.Duration {
return time.Duration(attempt) * time.Second
},
CheckRespStatus: func(resp *http.Response, err error, log *zap.SugaredLogger) bool {
CheckRespStatus: func(resp *http.Response, err error) bool {
if err != nil {
return true
}

View File

@@ -10,7 +10,6 @@ import (
"strconv"
"strings"
"github.com/debridmediamanager.com/zurg/internal/config"
zurghttp "github.com/debridmediamanager.com/zurg/pkg/http"
"go.uber.org/zap"
)
@@ -20,7 +19,7 @@ type RealDebrid struct {
client *zurghttp.HTTPClient
}
func NewRealDebrid(accessToken string, config config.ConfigInterface, log *zap.SugaredLogger) *RealDebrid {
func NewRealDebrid(accessToken string, log *zap.SugaredLogger) *RealDebrid {
maxRetries := 10
client := zurghttp.NewHTTPClient(accessToken, maxRetries, nil)
return &RealDebrid{