Fix traffic computation and optimize open ended requests
This commit is contained in:
@@ -2,6 +2,7 @@ package universal
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
@@ -18,16 +19,28 @@ import (
|
||||
)
|
||||
|
||||
type Downloader struct {
|
||||
rd *realdebrid.RealDebrid
|
||||
workerPool *ants.Pool
|
||||
TotalBytes atomic.Uint64
|
||||
rd *realdebrid.RealDebrid
|
||||
workerPool *ants.Pool
|
||||
TrafficServed atomic.Uint64
|
||||
TrafficOnStartup atomic.Uint64
|
||||
}
|
||||
|
||||
func NewDownloader(rd *realdebrid.RealDebrid, workerPool *ants.Pool) *Downloader {
|
||||
return &Downloader{
|
||||
dl := &Downloader{
|
||||
rd: rd,
|
||||
workerPool: workerPool,
|
||||
}
|
||||
|
||||
trafficDetails, err := dl.rd.GetTrafficDetails()
|
||||
if err != nil {
|
||||
trafficDetails = make(map[string]int64)
|
||||
}
|
||||
dl.TrafficOnStartup.Store(uint64(0))
|
||||
if _, ok := trafficDetails["real-debrid.com"]; ok {
|
||||
dl.TrafficOnStartup.Store(uint64(trafficDetails["real-debrid.com"]))
|
||||
}
|
||||
|
||||
return dl
|
||||
}
|
||||
|
||||
// StartResetBandwidthCountersJob is a permanent job that resets the bandwidth counters at 12AM CET
|
||||
@@ -48,7 +61,8 @@ func (dl *Downloader) StartResetBandwidthCountersJob() {
|
||||
ticker := time.NewTicker(24 * time.Hour)
|
||||
for {
|
||||
dl.rd.TokenManager.ResetAllTokens()
|
||||
dl.TotalBytes.Store(0)
|
||||
dl.TrafficServed.Store(0)
|
||||
dl.TrafficOnStartup.Store(0)
|
||||
<-ticker.C
|
||||
}
|
||||
})
|
||||
@@ -162,7 +176,12 @@ func (dl *Downloader) streamFileToResponse(
|
||||
|
||||
// Add the range header if it exists
|
||||
if req.Header.Get("Range") != "" {
|
||||
dlReq.Header.Add("Range", req.Header.Get("Range"))
|
||||
rangeVal := req.Header.Get("Range")
|
||||
// check if open-ended range request (e.g. "bytes=999-")
|
||||
if strings.HasSuffix(rangeVal, "-") {
|
||||
rangeVal += fmt.Sprintf("%d", unrestrict.Filesize-1)
|
||||
}
|
||||
dlReq.Header.Add("Range", rangeVal)
|
||||
}
|
||||
|
||||
downloadResp, err := dl.rd.DownloadFile(dlReq)
|
||||
@@ -227,13 +246,14 @@ func (dl *Downloader) streamFileToResponse(
|
||||
n, _ = io.Copy(resp, downloadResp.Body)
|
||||
}
|
||||
|
||||
if !strings.HasPrefix(unrestrict.Link, "https://real-debrid.com/d/") {
|
||||
return
|
||||
}
|
||||
|
||||
dl.workerPool.Submit(func() {
|
||||
if n == 0 {
|
||||
return
|
||||
}
|
||||
// Update the download statistics
|
||||
dl.TotalBytes.Add(uint64(n))
|
||||
if cfg.ShouldLogRequests() {
|
||||
dl.TrafficServed.Add(uint64(n))
|
||||
|
||||
if cfg.ShouldLogRequests() && bToMb(uint64(n)) > 0 {
|
||||
log.Debugf("Served %d MB of file %s (range=%s)", bToMb(uint64(n)), unrestrict.Filename, req.Header.Get("Range"))
|
||||
}
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user