Rename variables for easy reference

This commit is contained in:
Ben Adrian Sarmiento
2024-07-21 02:11:32 +02:00
parent 47751320f7
commit f8b9f8955b
8 changed files with 46 additions and 33 deletions

View File

@@ -56,7 +56,7 @@ func (zr *Handlers) generateResponse(req *http.Request) (*RootResponse, error) {
var mem runtime.MemStats var mem runtime.MemStats
runtime.ReadMemStats(&mem) runtime.ReadMemStats(&mem)
allTorrents, _ := zr.torMgr.DirectoryMap.Get(config.ALL_TORRENTS) torrents, _ := zr.torMgr.DirectoryMap.Get(config.ALL_TORRENTS)
repairQueueStr := "" repairQueueStr := ""
if zr.torMgr.RepairQueue == nil { if zr.torMgr.RepairQueue == nil {
@@ -108,7 +108,7 @@ func (zr *Handlers) generateResponse(req *http.Request) (*RootResponse, error) {
Logs: fmt.Sprintf("//%s/logs/", req.Host), Logs: fmt.Sprintf("//%s/logs/", req.Host),
UserInfo: userInfo, UserInfo: userInfo,
TrafficServedPerAPI: trafficFromAPI, TrafficServedPerAPI: trafficFromAPI,
LibrarySize: allTorrents.Count(), LibrarySize: torrents.Count(),
TorrentsToRepair: repairQueueStr, TorrentsToRepair: repairQueueStr,
MemAlloc: bToMb(mem.Alloc), MemAlloc: bToMb(mem.Alloc),
TotalAlloc: bToMb(mem.TotalAlloc), TotalAlloc: bToMb(mem.TotalAlloc),

View File

@@ -22,8 +22,8 @@ func (t *TorrentManager) CheckDeletedStatus(torrent *Torrent) bool {
} }
func (t *TorrentManager) Delete(accessKey string, deleteInRD bool) { func (t *TorrentManager) Delete(accessKey string, deleteInRD bool) {
allTorrents, _ := t.DirectoryMap.Get(INT_ALL) torrents, _ := t.DirectoryMap.Get(INT_ALL)
if torrent, ok := allTorrents.Get(accessKey); ok { if torrent, ok := torrents.Get(accessKey); ok {
if deleteInRD { if deleteInRD {
torrent.DownloadedIDs.Clone().Each(func(torrentID string) bool { torrent.DownloadedIDs.Clone().Each(func(torrentID string) bool {
t.DeleteByID(torrentID) t.DeleteByID(torrentID)
@@ -40,7 +40,7 @@ func (t *TorrentManager) Delete(accessKey string, deleteInRD bool) {
torrents.Remove(accessKey) torrents.Remove(accessKey)
}) })
allTorrents.Remove(accessKey) torrents.Remove(accessKey)
} }
func (t *TorrentManager) DeleteByID(torrentID string) { func (t *TorrentManager) DeleteByID(torrentID string) {

View File

@@ -105,12 +105,12 @@ func NewTorrentManager(cfg config.ConfigInterface, api *realdebrid.RealDebrid, w
defer wg.Done() defer wg.Done()
// initial load of existing *.zurgtorrent files // initial load of existing *.zurgtorrent files
allTorrents, _ := t.DirectoryMap.Get(INT_ALL) torrents, _ := t.DirectoryMap.Get(INT_ALL)
t.getTorrentFiles("data").Each(func(filePath string) bool { t.getTorrentFiles("data").Each(func(filePath string) bool {
torrent := t.readTorrentFromFile(filePath) torrent := t.readTorrentFromFile(filePath)
if torrent != nil { if torrent != nil {
accessKey := t.GetKey(torrent) accessKey := t.GetKey(torrent)
allTorrents.Set(accessKey, torrent) torrents.Set(accessKey, torrent)
t.assignDirectory(torrent, false, false) t.assignDirectory(torrent, false, false)
} }
return false return false
@@ -498,12 +498,12 @@ func (t *TorrentManager) StartDumpJob() {
} }
func (t *TorrentManager) analyzeAllTorrents() { func (t *TorrentManager) analyzeAllTorrents() {
allTorrents, _ := t.DirectoryMap.Get(INT_ALL) torrents, _ := t.DirectoryMap.Get(INT_ALL)
totalCount := allTorrents.Count() totalCount := torrents.Count()
t.log.Infof("Applying media info details to all %d torrents", totalCount) t.log.Infof("Applying media info details to all %d torrents", totalCount)
idx := 0 idx := 0
skipTheRest := false skipTheRest := false
allTorrents.IterCb(func(_ string, torrent *Torrent) { torrents.IterCb(func(_ string, torrent *Torrent) {
if skipTheRest { if skipTheRest {
return return
} }

View File

@@ -29,8 +29,8 @@ func (t *TorrentManager) refreshTorrents(initialRun bool) {
var wg sync.WaitGroup var wg sync.WaitGroup
var mergeChan = make(chan *Torrent, len(instances)) var mergeChan = make(chan *Torrent, len(instances))
allTorrents, _ := t.DirectoryMap.Get(INT_ALL) torrents, _ := t.DirectoryMap.Get(INT_ALL)
oldKeys := mapset.NewSet[string](allTorrents.Keys()...) oldKeys := mapset.NewSet[string](torrents.Keys()...)
freshIDs := mapset.NewSet[string]() freshIDs := mapset.NewSet[string]()
freshAccessKeys := mapset.NewSet[string]() freshAccessKeys := mapset.NewSet[string]()
@@ -60,9 +60,9 @@ func (t *TorrentManager) refreshTorrents(initialRun bool) {
freshAccessKeys.Add(accessKey) freshAccessKeys.Add(accessKey)
var forMerging *Torrent var forMerging *Torrent
mainTorrent, exists := allTorrents.Get(accessKey) mainTorrent, exists := torrents.Get(accessKey)
if !exists { if !exists {
allTorrents.Set(accessKey, torrent) torrents.Set(accessKey, torrent)
t.writeTorrentToFile(torrent) t.writeTorrentToFile(torrent)
t.assignDirectory(torrent, !initialRun, true) t.assignDirectory(torrent, !initialRun, true)
} else if !mainTorrent.DownloadedIDs.ContainsOne(tInfo.ID) { } else if !mainTorrent.DownloadedIDs.ContainsOne(tInfo.ID) {
@@ -83,13 +83,13 @@ func (t *TorrentManager) refreshTorrents(initialRun bool) {
continue continue
} }
accessKey := t.GetKey(torrent) accessKey := t.GetKey(torrent)
existing, ok := allTorrents.Get(accessKey) existing, ok := torrents.Get(accessKey)
if !ok { if !ok {
t.log.Warnf("Cannot merge %s", accessKey) t.log.Warnf("Cannot merge %s", accessKey)
continue continue
} }
mainTorrent := t.mergeTorrents(existing, torrent) mainTorrent := t.mergeTorrents(existing, torrent)
allTorrents.Set(accessKey, mainTorrent) torrents.Set(accessKey, mainTorrent)
t.writeTorrentToFile(mainTorrent) t.writeTorrentToFile(mainTorrent)
t.assignDirectory(mainTorrent, !initialRun, true) t.assignDirectory(mainTorrent, !initialRun, true)
} }
@@ -100,7 +100,7 @@ func (t *TorrentManager) refreshTorrents(initialRun bool) {
return return
} }
freshAccessKeys.Difference(oldKeys).Each(func(accessKey string) bool { freshAccessKeys.Difference(oldKeys).Each(func(accessKey string) bool {
torrent, _ := allTorrents.Get(accessKey) torrent, _ := torrents.Get(accessKey)
t.applyMediaInfoDetails(torrent) t.applyMediaInfoDetails(torrent)
return false return false
}) })
@@ -112,7 +112,7 @@ func (t *TorrentManager) refreshTorrents(initialRun bool) {
return false return false
}) })
t.log.Infof("Compiled into %d unique torrents", allTorrents.Count()) t.log.Infof("Compiled into %d unique torrents", torrents.Count())
t.workerPool.Submit(func() { t.workerPool.Submit(func() {
t.OnceDoneBin.Clone().Each(func(entry string) bool { t.OnceDoneBin.Clone().Each(func(entry string) bool {
@@ -140,7 +140,7 @@ func (t *TorrentManager) refreshTorrents(initialRun bool) {
// cleans up DownloadedIDs field of all torrents // cleans up DownloadedIDs field of all torrents
t.workerPool.Submit(func() { t.workerPool.Submit(func() {
allTorrents.IterCb(func(accessKey string, torrent *Torrent) { torrents.IterCb(func(accessKey string, torrent *Torrent) {
deletedIDs := torrent.DownloadedIDs.Difference(freshIDs) deletedIDs := torrent.DownloadedIDs.Difference(freshIDs)
if deletedIDs.Cardinality() > 0 { if deletedIDs.Cardinality() > 0 {
deletedIDs.Each(func(id string) bool { deletedIDs.Each(func(id string) bool {
@@ -397,8 +397,8 @@ func (t *TorrentManager) assignDirectory(tor *Torrent, triggerHook bool, outputL
for _, directories := range configV1.GetGroupMap() { for _, directories := range configV1.GetGroupMap() {
for _, directory := range directories { for _, directory := range directories {
if t.Config.MeetsConditions(directory, t.GetKey(tor), tor.ComputeTotalSize(), torrentIDs, filenames, fileSizes, mediaInfos) { if t.Config.MeetsConditions(directory, t.GetKey(tor), tor.ComputeTotalSize(), torrentIDs, filenames, fileSizes, mediaInfos) {
listing, _ := t.DirectoryMap.Get(directory) torrents, _ := t.DirectoryMap.Get(directory)
listing.Set(accessKey, tor) torrents.Set(accessKey, tor)
if directory != config.ALL_TORRENTS { if directory != config.ALL_TORRENTS {
dirs = append(dirs, directory) dirs = append(dirs, directory)

View File

@@ -92,19 +92,19 @@ func (t *TorrentManager) invokeRepair(torrent *Torrent) {
} }
func (t *TorrentManager) executeRepairJob(torrent *Torrent) { func (t *TorrentManager) executeRepairJob(torrent *Torrent) {
var haystack cmap.ConcurrentMap[string, *Torrent] var torrents cmap.ConcurrentMap[string, *Torrent]
if torrent == nil { if torrent == nil {
haystack, _ = t.DirectoryMap.Get(INT_ALL) torrents, _ = t.DirectoryMap.Get(INT_ALL)
} else { } else {
haystack = cmap.New[*Torrent]() torrents = cmap.New[*Torrent]()
haystack.Set("", torrent) torrents.Set("", torrent)
} }
// collect all torrents that need to be repaired asynchronously // collect all torrents that need to be repaired asynchronously
toRepair := mapset.NewSet[*Torrent]() toRepair := mapset.NewSet[*Torrent]()
var wg sync.WaitGroup var wg sync.WaitGroup
haystack.IterCb(func(_ string, torrent *Torrent) { torrents.IterCb(func(_ string, torrent *Torrent) {
wg.Add(1) wg.Add(1)
// temp worker for finding broken torrents // temp worker for finding broken torrents
t.workerPool.Submit(func() { t.workerPool.Submit(func() {
@@ -677,8 +677,8 @@ func (t *TorrentManager) ResetRepairState() {
if !t.Config.EnableRepair() { if !t.Config.EnableRepair() {
return return
} }
allTorrents, _ := t.DirectoryMap.Get(INT_ALL) torrents, _ := t.DirectoryMap.Get(INT_ALL)
allTorrents.IterCb(func(_ string, torrent *Torrent) { torrents.IterCb(func(_ string, torrent *Torrent) {
err := torrent.State.Event(context.Background(), "reset_repair") err := torrent.State.Event(context.Background(), "reset_repair")
if err == nil { if err == nil {
t.repairLog.Debugf("Repair state of torrent %s has been reset", t.GetKey(torrent)) t.repairLog.Debugf("Repair state of torrent %s has been reset", t.GetKey(torrent))

View File

@@ -8,7 +8,7 @@ import (
func (t *TorrentManager) GetUncachedTorrents() ([]*Torrent, error) { func (t *TorrentManager) GetUncachedTorrents() ([]*Torrent, error) {
t.log.Debug("Checking if torrents are still cached") t.log.Debug("Checking if torrents are still cached")
allTorrents, _ := t.DirectoryMap.Get(INT_ALL) torrents, _ := t.DirectoryMap.Get(INT_ALL)
var hashGroups []mapset.Set[string] var hashGroups []mapset.Set[string]
const maxGroupSize = 399 const maxGroupSize = 399
@@ -16,7 +16,7 @@ func (t *TorrentManager) GetUncachedTorrents() ([]*Torrent, error) {
currentGroup := mapset.NewSet[string]() currentGroup := mapset.NewSet[string]()
hashGroups = append(hashGroups, currentGroup) hashGroups = append(hashGroups, currentGroup)
allTorrents.IterCb(func(_ string, torrent *Torrent) { torrents.IterCb(func(_ string, torrent *Torrent) {
if torrent.UnrepairableReason != "" { if torrent.UnrepairableReason != "" {
return return
} }
@@ -45,7 +45,7 @@ func (t *TorrentManager) GetUncachedTorrents() ([]*Torrent, error) {
} }
var uncachedTorrents []*Torrent var uncachedTorrents []*Torrent
allTorrents.IterCb(func(_ string, torrent *Torrent) { torrents.IterCb(func(_ string, torrent *Torrent) {
if torrent.UnrepairableReason != "" { if torrent.UnrepairableReason != "" {
return return
} }

View File

@@ -201,6 +201,7 @@ func (r *HTTPClient) ensureReachableHost(req *http.Request) {
if !strings.Contains(req.Host, ".download.real-debrid.") { if !strings.Contains(req.Host, ".download.real-debrid.") {
return return
} }
// skip CDN servers
if req.Host[0] >= 'a' && req.Host[0] <= 'z' { if req.Host[0] >= 'a' && req.Host[0] <= 'z' {
return return
} }
@@ -216,16 +217,18 @@ func (r *HTTPClient) ensureReachableHost(req *http.Request) {
} else if strings.HasSuffix(req.Host, ".cloud") { } else if strings.HasSuffix(req.Host, ".cloud") {
newHost = strings.Replace(req.Host, ".cloud", ".com", 1) newHost = strings.Replace(req.Host, ".cloud", ".com", 1)
} }
// check if newHost is reachable
if r.CheckIfHostIsReachable(newHost) { if r.CheckIfHostIsReachable(newHost) {
req.Host = newHost req.Host = newHost
req.URL.Host = req.Host req.URL.Host = req.Host
return return
} }
// just pick a random host
req.Host = r.hosts[rand.Intn(len(r.hosts))] req.Host = r.hosts[rand.Intn(len(r.hosts))]
req.URL.Host = req.Host req.URL.Host = req.Host
} }
// CheckIfHostIsReachable checks if the given host is passed in the list of reachable hosts
func (r *HTTPClient) CheckIfHostIsReachable(reqHost string) bool { func (r *HTTPClient) CheckIfHostIsReachable(reqHost string) bool {
for _, host := range r.hosts { for _, host := range r.hosts {
if reqHost == host { if reqHost == host {

View File

@@ -84,9 +84,11 @@ func (rd *RealDebrid) UnrestrictAndVerify(link string) (*Download, error) {
if expiry, ok := rd.verifiedLinks.Get(download.ID); ok && expiry > time.Now().Unix() { if expiry, ok := rd.verifiedLinks.Get(download.ID); ok && expiry > time.Now().Unix() {
return download, nil return download, nil
} else if ok { } else if ok {
// if the link is expired, remove it from the verified links cache
rd.verifiedLinks.Remove(download.ID) rd.verifiedLinks.Remove(download.ID)
} }
// check if the link is still valid (not in the cache or expired)
err := rd.downloadClient.VerifyLink(download.Download) err := rd.downloadClient.VerifyLink(download.Download)
if utils.IsBytesLimitReached(err) { if utils.IsBytesLimitReached(err) {
rd.TokenManager.SetTokenAsExpired(token, "bandwidth limit exceeded") rd.TokenManager.SetTokenAsExpired(token, "bandwidth limit exceeded")
@@ -95,6 +97,7 @@ func (rd *RealDebrid) UnrestrictAndVerify(link string) (*Download, error) {
rd.verifiedLinks.Set(download.ID, time.Now().Unix()+DOWNLOAD_LINK_EXPIRY) rd.verifiedLinks.Set(download.ID, time.Now().Unix()+DOWNLOAD_LINK_EXPIRY)
return download, nil return download, nil
} }
// if verification failed, remove the link from the token map
tokenMap.Remove(link) tokenMap.Remove(link)
} }
@@ -419,8 +422,10 @@ func (rd *RealDebrid) DownloadFile(req *http.Request) (*http.Response, error) {
// MonitorExpiredTokens is a permanent job for monitoring expired tokens if they are still expired // MonitorExpiredTokens is a permanent job for monitoring expired tokens if they are still expired
func (rd *RealDebrid) MonitorExpiredTokens() { func (rd *RealDebrid) MonitorExpiredTokens() {
sleepPeriod := 1 * time.Minute sleepPeriod := 1 * time.Minute
i := 0
rd.workerPool.Submit(func() { rd.workerPool.Submit(func() {
for { for {
i++
expiredTokens := rd.TokenManager.GetExpiredTokens() expiredTokens := rd.TokenManager.GetExpiredTokens()
for _, token := range expiredTokens { for _, token := range expiredTokens {
tokenMap, _ := rd.UnrestrictMap.Get(token) tokenMap, _ := rd.UnrestrictMap.Get(token)
@@ -432,7 +437,12 @@ func (rd *RealDebrid) MonitorExpiredTokens() {
} }
err := rd.downloadClient.VerifyLink(download.Download) err := rd.downloadClient.VerifyLink(download.Download)
if err != nil { if err != nil {
skipAll = utils.IsBytesLimitReached(err) if utils.IsBytesLimitReached(err) {
if i%15 == 0 {
rd.log.Debugf("Token %s is still expired", utils.MaskToken(token))
}
skipAll = true
}
return return
} }
stillExpired = false stillExpired = false