Increment traffic monitors async

This commit is contained in:
Ben Adrian Sarmiento
2024-06-25 02:19:54 +02:00
parent 7fc8a4e0c5
commit d095bf2dbf
3 changed files with 40 additions and 26 deletions

View File

@@ -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) {