Use atomic value for initial traffic

This commit is contained in:
Ben Adrian Sarmiento
2024-06-26 23:33:01 +02:00
parent f4910b8e87
commit bd1a163002
2 changed files with 7 additions and 6 deletions

View File

@@ -198,8 +198,8 @@ func (zr *Handlers) handleHome(resp http.ResponseWriter, req *http.Request) {
}
efficiency := response.ServedMB * 100 / denominator
if zr.initialTraffic > response.TrafficLogged {
zr.initialTraffic = response.TrafficLogged
if zr.initialTraffic.Load() > response.TrafficLogged {
zr.initialTraffic.Store(response.TrafficLogged)
}
out += fmt.Sprintf(`
@@ -250,7 +250,7 @@ func (zr *Handlers) handleHome(resp http.ResponseWriter, req *http.Request) {
response.NumGC,
response.PID,
response.TrafficLogged,
response.TrafficLogged-bToMb(uint64(zr.initialTraffic)),
response.TrafficLogged-bToMb(zr.initialTraffic.Load()),
response.RequestedMB,
response.ServedMB,
efficiency,

View File

@@ -6,6 +6,7 @@ import (
"net/url"
"path/filepath"
"strings"
"sync/atomic"
"github.com/debridmediamanager/zurg/internal/config"
"github.com/debridmediamanager/zurg/internal/dav"
@@ -26,7 +27,7 @@ type Handlers struct {
api *realdebrid.RealDebrid
workerPool *ants.Pool
hosts []string
initialTraffic uint64
initialTraffic atomic.Uint64
log *logutil.Logger
}
@@ -52,9 +53,9 @@ func AttachHandlers(router *chi.Mux, downloader *universal.Downloader, torMgr *t
log.Errorf("Failed to get traffic details: %v", err)
trafficDetails = make(map[string]int64)
}
hs.initialTraffic = 0
hs.initialTraffic.Store(uint64(0))
if _, ok := trafficDetails["real-debrid.com"]; ok {
hs.initialTraffic = uint64(trafficDetails["real-debrid.com"])
hs.initialTraffic.Store(uint64(trafficDetails["real-debrid.com"]))
}
if cfg.GetUsername() != "" {