Increment traffic monitors async
This commit is contained in:
@@ -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