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
|
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
|
var mem runtime.MemStats
|
||||||
runtime.ReadMemStats(&mem)
|
runtime.ReadMemStats(&mem)
|
||||||
|
|
||||||
@@ -83,7 +77,11 @@ func (zr *Handlers) generateResponse(resp http.ResponseWriter, req *http.Request
|
|||||||
sortedIDs := zr.torMgr.OnceDoneBin.ToSlice()
|
sortedIDs := zr.torMgr.OnceDoneBin.ToSlice()
|
||||||
sort.Strings(sortedIDs)
|
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
|
var trafficLogged int64
|
||||||
trafficLogged = 0
|
trafficLogged = 0
|
||||||
if _, ok := trafficDetails["real-debrid.com"]; ok {
|
if _, ok := trafficDetails["real-debrid.com"]; ok {
|
||||||
@@ -227,7 +225,7 @@ func (zr *Handlers) handleHome(resp http.ResponseWriter, req *http.Request) {
|
|||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
<td>Traffic Logged</td>
|
<td>Traffic Logged</td>
|
||||||
<td colspan="2">%d MB</td>
|
<td colspan="2">%d MB (%d MB added)</td>
|
||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
<td>Traffic Requested</td>
|
<td>Traffic Requested</td>
|
||||||
@@ -248,6 +246,7 @@ func (zr *Handlers) handleHome(resp http.ResponseWriter, req *http.Request) {
|
|||||||
response.NumGC,
|
response.NumGC,
|
||||||
response.PID,
|
response.PID,
|
||||||
response.TrafficLogged,
|
response.TrafficLogged,
|
||||||
|
response.TrafficLogged-bToMb(uint64(zr.initialTraffic)),
|
||||||
response.RequestedMB,
|
response.RequestedMB,
|
||||||
response.ServedMB,
|
response.ServedMB,
|
||||||
efficiency,
|
efficiency,
|
||||||
|
|||||||
@@ -20,13 +20,14 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type Handlers struct {
|
type Handlers struct {
|
||||||
downloader *universal.Downloader
|
downloader *universal.Downloader
|
||||||
torMgr *torrent.TorrentManager
|
torMgr *torrent.TorrentManager
|
||||||
cfg config.ConfigInterface
|
cfg config.ConfigInterface
|
||||||
api *realdebrid.RealDebrid
|
api *realdebrid.RealDebrid
|
||||||
workerPool *ants.Pool
|
workerPool *ants.Pool
|
||||||
hosts []string
|
hosts []string
|
||||||
log *logutil.Logger
|
initialTraffic int64
|
||||||
|
log *logutil.Logger
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
@@ -46,6 +47,16 @@ func AttachHandlers(router *chi.Mux, downloader *universal.Downloader, torMgr *t
|
|||||||
log: log,
|
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() != "" {
|
if cfg.GetUsername() != "" {
|
||||||
router.Use(hs.basicAuth)
|
router.Use(hs.basicAuth)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,13 +21,15 @@ import (
|
|||||||
|
|
||||||
type Downloader struct {
|
type Downloader struct {
|
||||||
client *zurghttp.HTTPClient
|
client *zurghttp.HTTPClient
|
||||||
|
workerPool *ants.Pool
|
||||||
RequestedBytes atomic.Uint64
|
RequestedBytes atomic.Uint64
|
||||||
TotalBytes atomic.Uint64
|
TotalBytes atomic.Uint64
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewDownloader(client *zurghttp.HTTPClient, workerPool *ants.Pool) *Downloader {
|
func NewDownloader(client *zurghttp.HTTPClient, workerPool *ants.Pool) *Downloader {
|
||||||
dl := &Downloader{
|
dl := &Downloader{
|
||||||
client: client,
|
client: client,
|
||||||
|
workerPool: workerPool,
|
||||||
}
|
}
|
||||||
|
|
||||||
// track bandwidth usage and reset at 12AM CET
|
// track bandwidth usage and reset at 12AM CET
|
||||||
@@ -213,16 +215,18 @@ func (dl *Downloader) streamFileToResponse(
|
|||||||
buf := make([]byte, cfg.GetNetworkBufferSize())
|
buf := make([]byte, cfg.GetNetworkBufferSize())
|
||||||
n, _ := io.CopyBuffer(resp, downloadResp.Body, buf)
|
n, _ := io.CopyBuffer(resp, downloadResp.Body, buf)
|
||||||
|
|
||||||
// Update the download statistics
|
dl.workerPool.Submit(func() {
|
||||||
reqBytes, _ := parseRangeHeader(req.Header.Get("Range"))
|
// Update the download statistics
|
||||||
if reqBytes == 0 && unrestrict != nil {
|
reqBytes, _ := parseRangeHeader(req.Header.Get("Range"))
|
||||||
reqBytes = uint64(unrestrict.Filesize)
|
if reqBytes == 0 && unrestrict != nil {
|
||||||
}
|
reqBytes = uint64(unrestrict.Filesize)
|
||||||
dl.RequestedBytes.Add(reqBytes)
|
}
|
||||||
dl.TotalBytes.Add(uint64(n))
|
dl.RequestedBytes.Add(reqBytes)
|
||||||
if cfg.ShouldLogRequests() {
|
dl.TotalBytes.Add(uint64(n))
|
||||||
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"))
|
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) {
|
func redirect(resp http.ResponseWriter, req *http.Request, url string) {
|
||||||
|
|||||||
Reference in New Issue
Block a user