diff --git a/internal/handlers/home.go b/internal/handlers/home.go index 1fe1fce..bf6035e 100644 --- a/internal/handlers/home.go +++ b/internal/handlers/home.go @@ -53,12 +53,6 @@ func (zr *Handlers) generateResponse(resp http.ResponseWriter, req *http.Request return nil, err } - trafficDetails, err := zr.api.GetTrafficDetails() - if err != nil { - http.Error(resp, err.Error(), http.StatusInternalServerError) - return nil, err - } - var mem runtime.MemStats runtime.ReadMemStats(&mem) @@ -83,7 +77,11 @@ func (zr *Handlers) generateResponse(resp http.ResponseWriter, req *http.Request sortedIDs := zr.torMgr.OnceDoneBin.ToSlice() sort.Strings(sortedIDs) - // check if real-debrid.com is in the traffic details + trafficDetails, err := zr.api.GetTrafficDetails() + if err != nil { + http.Error(resp, err.Error(), http.StatusInternalServerError) + return nil, err + } var trafficLogged int64 trafficLogged = 0 if _, ok := trafficDetails["real-debrid.com"]; ok { @@ -227,7 +225,7 @@ func (zr *Handlers) handleHome(resp http.ResponseWriter, req *http.Request) { Traffic Logged - %d MB + %d MB (%d MB added) Traffic Requested @@ -248,6 +246,7 @@ func (zr *Handlers) handleHome(resp http.ResponseWriter, req *http.Request) { response.NumGC, response.PID, response.TrafficLogged, + response.TrafficLogged-bToMb(uint64(zr.initialTraffic)), response.RequestedMB, response.ServedMB, efficiency, diff --git a/internal/handlers/router.go b/internal/handlers/router.go index 38503f4..8dd5c5e 100644 --- a/internal/handlers/router.go +++ b/internal/handlers/router.go @@ -20,13 +20,14 @@ import ( ) type Handlers struct { - downloader *universal.Downloader - torMgr *torrent.TorrentManager - cfg config.ConfigInterface - api *realdebrid.RealDebrid - workerPool *ants.Pool - hosts []string - log *logutil.Logger + downloader *universal.Downloader + torMgr *torrent.TorrentManager + cfg config.ConfigInterface + api *realdebrid.RealDebrid + workerPool *ants.Pool + hosts []string + initialTraffic int64 + log *logutil.Logger } func init() { @@ -46,6 +47,16 @@ func AttachHandlers(router *chi.Mux, downloader *universal.Downloader, torMgr *t log: log, } + trafficDetails, err := api.GetTrafficDetails() + if err != nil { + log.Errorf("Failed to get traffic details: %v", err) + trafficDetails = make(map[string]int64) + } + hs.initialTraffic = 0 + if _, ok := trafficDetails["real-debrid.com"]; ok { + hs.initialTraffic = trafficDetails["real-debrid.com"] + } + if cfg.GetUsername() != "" { router.Use(hs.basicAuth) } diff --git a/internal/universal/downloader.go b/internal/universal/downloader.go index fad2306..4af324a 100644 --- a/internal/universal/downloader.go +++ b/internal/universal/downloader.go @@ -21,13 +21,15 @@ import ( type Downloader struct { client *zurghttp.HTTPClient + workerPool *ants.Pool RequestedBytes atomic.Uint64 TotalBytes atomic.Uint64 } func NewDownloader(client *zurghttp.HTTPClient, workerPool *ants.Pool) *Downloader { dl := &Downloader{ - client: client, + client: client, + workerPool: workerPool, } // track bandwidth usage and reset at 12AM CET @@ -213,16 +215,18 @@ func (dl *Downloader) streamFileToResponse( buf := make([]byte, cfg.GetNetworkBufferSize()) n, _ := io.CopyBuffer(resp, downloadResp.Body, buf) - // Update the download statistics - reqBytes, _ := parseRangeHeader(req.Header.Get("Range")) - if reqBytes == 0 && unrestrict != nil { - reqBytes = uint64(unrestrict.Filesize) - } - dl.RequestedBytes.Add(reqBytes) - dl.TotalBytes.Add(uint64(n)) - if cfg.ShouldLogRequests() { - log.Debugf("Served %d MB of the requested %d MB of file %s (range=%s)", bToMb(uint64(n)), bToMb(reqBytes), unrestrict.Filename, req.Header.Get("Range")) - } + dl.workerPool.Submit(func() { + // Update the download statistics + reqBytes, _ := parseRangeHeader(req.Header.Get("Range")) + if reqBytes == 0 && unrestrict != nil { + reqBytes = uint64(unrestrict.Filesize) + } + dl.RequestedBytes.Add(reqBytes) + dl.TotalBytes.Add(uint64(n)) + if cfg.ShouldLogRequests() { + log.Debugf("Served %d MB of the requested %d MB of file %s (range=%s)", bToMb(uint64(n)), bToMb(reqBytes), unrestrict.Filename, req.Header.Get("Range")) + } + }) } func redirect(resp http.ResponseWriter, req *http.Request, url string) {