Add job for monitoring bw limit status of tokens
This commit is contained in:
@@ -27,11 +27,14 @@ type Downloader struct {
|
||||
}
|
||||
|
||||
func NewDownloader(rd *realdebrid.RealDebrid, workerPool *ants.Pool) *Downloader {
|
||||
dl := &Downloader{
|
||||
return &Downloader{
|
||||
rd: rd,
|
||||
workerPool: workerPool,
|
||||
}
|
||||
}
|
||||
|
||||
// StartResetBandwidthCountersJob is a permanent job that resets the bandwidth counters at 12AM CET
|
||||
func (dl *Downloader) StartResetBandwidthCountersJob() {
|
||||
// track bandwidth usage and reset at 12AM CET
|
||||
now := time.Now()
|
||||
tomorrow := now.AddDate(0, 0, 1)
|
||||
@@ -42,19 +45,17 @@ func NewDownloader(rd *realdebrid.RealDebrid, workerPool *ants.Pool) *Downloader
|
||||
nextMidnightInCET := time.Date(tomorrow.Year(), tomorrow.Month(), tomorrow.Day(), 0, 0, 0, 0, cetTZ)
|
||||
duration := nextMidnightInCET.Sub(now)
|
||||
timer := time.NewTimer(duration)
|
||||
// permanent job for bandwidth reset
|
||||
workerPool.Submit(func() {
|
||||
|
||||
dl.workerPool.Submit(func() {
|
||||
<-timer.C
|
||||
ticker := time.NewTicker(24 * time.Hour)
|
||||
for {
|
||||
rd.TokenManager.ResetAllTokens()
|
||||
dl.rd.TokenManager.ResetAllTokens()
|
||||
dl.RequestedBytes.Store(0)
|
||||
dl.TotalBytes.Store(0)
|
||||
<-ticker.C
|
||||
}
|
||||
})
|
||||
|
||||
return dl
|
||||
}
|
||||
|
||||
// DownloadFile handles a GET request for files in torrents
|
||||
@@ -90,7 +91,7 @@ func (dl *Downloader) DownloadFile(
|
||||
}
|
||||
|
||||
unrestrict, err := torMgr.UnrestrictFile(file)
|
||||
if utils.IsBWLimitExceeded(err) {
|
||||
if utils.AreAllTokensExpired(err) {
|
||||
// log.Errorf("Your account has reached the bandwidth limit, please try again after 12AM CET")
|
||||
http.Error(resp, "File is not available (bandwidth limit reached)", http.StatusBadRequest)
|
||||
return
|
||||
@@ -164,7 +165,8 @@ func (dl *Downloader) streamFileToResponse(
|
||||
}
|
||||
|
||||
downloadResp, err := dl.rd.DownloadFile(dlReq)
|
||||
if utils.IsBWLimitExceeded(err) {
|
||||
if utils.IsBytesLimitReached(err) {
|
||||
dl.rd.TokenManager.SetTokenAsExpired(unrestrict.Token, "bandwidth limit exceeded")
|
||||
// log.Errorf("Your account has reached the bandwidth limit, please try again after 12AM CET")
|
||||
http.Error(resp, "File is not available (bandwidth limit reached)", http.StatusBadRequest)
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user