Increment traffic monitors async
This commit is contained in:
@@ -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) {
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Traffic Logged</td>
|
||||
<td colspan="2">%d MB</td>
|
||||
<td colspan="2">%d MB (%d MB added)</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Traffic Requested</td>
|
||||
@@ -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,
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user