Skip to content

Commit

Permalink
where's the boundary now
Browse files Browse the repository at this point in the history
  • Loading branch information
KyleSanderson authored Nov 24, 2024
1 parent 7d3589b commit bc7a3d8
Showing 1 changed file with 64 additions and 39 deletions.
103 changes: 64 additions & 39 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ type timeentry struct {
e map[string][]qbittorrent.Torrent
tc *timecache.Cache
err error
sync.Mutex
m sync.RWMutex
}

var db *bolt.DB
Expand All @@ -94,6 +94,13 @@ var titlemap = ttlcache.New[string, *rls.Release](
SetDefaultTTL(time.Minute * 15).
SetTimerResolution(time.Minute * 5))

var formattedmap = ttlcache.New[string, string](
ttlcache.Options[string, string]{}.
SetDefaultTTL(time.Minute * 15).
SetTimerResolution(time.Minute * 5))

var globalTime = timecache.New(timecache.Options{})

func main() {
initDatabase()

Expand Down Expand Up @@ -153,49 +160,42 @@ func (c *upgradereq) getAllTorrents() *timeentry {
Password: c.Password,
}

f := func() ttlcache.Item[*timeentry] {
te, ok := torrentmap.GetItem(set)
if ok {
return te
}

res := &timeentry{
tc: timecache.New(timecache.Options{}),
getOrInitialize := func() ttlcache.Item[*timeentry] {
if it, ok := torrentmap.GetItem(set); ok {
if c.CacheBypass == 0 || (c.CacheBypass == 1 && len(it.GetValue().e) == 0) {
return it
}
}

return torrentmap.SetItem(set, res, ttlcache.DefaultTTL)
return torrentmap.SetItem(set, &timeentry{tc: timecache.New(timecache.Options{})}, ttlcache.DefaultTTL)
}

res := f()
val := res.GetValue()
cur := val.tc.Now()
if c.CacheBypass == 0 && len(val.e) != 0 && res.GetTime().After(cur) {
te := getOrInitialize()
val := te.GetValue()
if val.e != nil {
return val
}

val.Lock()
defer val.Unlock()

res = f()
val = res.GetValue()
if c.CacheBypass == 0 && len(val.e) != 0 && res.GetTime().After(cur) {
return val
}
GetOrUpdate(&val.m, func() bool {
te = getOrInitialize()
val = te.GetValue()
return val.e != nil
}, func() {
torrents, err := c.Client.GetTorrents(qbittorrent.TorrentFilterOptions{})
if err != nil {
return
}

torrents, err := c.Client.GetTorrents(qbittorrent.TorrentFilterOptions{})
if err != nil {
return &timeentry{err: err, tc: timecache.New(timecache.Options{})}
}
val.e = make(map[string][]qbittorrent.Torrent)

nt := val.tc.Now()
val.e = make(map[string][]qbittorrent.Torrent)
for _, t := range torrents {
s := CacheFormatted(t.Name)
val.e[s] = append(val.e[s], t)
}

for _, t := range torrents {
s := getFormattedTitle(CacheTitle(t.Name))
val.e[s] = append(val.e[s], t)
}
torrentmap.Set(set, val, val.tc.Now().Sub(te.GetTime()))
})

torrentmap.Set(set, val, nt.Sub(cur))
return val
}

Expand Down Expand Up @@ -335,7 +335,7 @@ func handleUpgrade(w http.ResponseWriter, r *http.Request) {
var requestrls Entry
requestrls.r = CacheTitle(req.Name)

if v, ok := mp.e[getFormattedTitle(requestrls.r)]; ok {
if v, ok := mp.e[CacheFormatted(req.Name)]; ok {
code := 0
var parent Entry
for _, childtor := range v {
Expand Down Expand Up @@ -573,7 +573,7 @@ func handleCross(w http.ResponseWriter, r *http.Request) {
}

requestrls := Entry{r: CacheTitle(req.Name)}
v, ok := mp.e[getFormattedTitle(requestrls.r)]
v, ok := mp.e[CacheFormatted(req.Name)]
if !ok {
http.Error(w, fmt.Sprintf("Not a cross-submission: %q\n", req.Name), 420)
return
Expand Down Expand Up @@ -893,7 +893,11 @@ func handleUnregistered(w http.ResponseWriter, r *http.Request) {
http.Error(w, fmt.Sprintf("Unregistered torrents deleted: %d", count), 200)
}

func getFormattedTitle(r *rls.Release) string {
func getFormattedTitle(title string) string {
return getReleaseTitle(CacheTitle(title))
}

func getReleaseTitle(r *rls.Release) string {
s := fmt.Sprintf("%s%s%s%04d%02d%02d%02d%03d", rls.MustNormalize(r.Artist), rls.MustNormalize(r.Title), rls.MustNormalize(r.Subtitle), r.Year, r.Month, r.Day, r.Series, r.Episode)
for _, a := range r.Cut {
s += rls.MustNormalize(a)
Expand Down Expand Up @@ -1947,13 +1951,12 @@ func handleTorznabCrossSearch(w http.ResponseWriter, r *http.Request) {
tbc := torb.Bucket(k)

ibc.ForEach(func(kc, v []byte) error {
r := CacheTitle(string(kc))

ent, ok := mp.e[getFormattedTitle(r)]
ent, ok := mp.e[CacheFormatted(string(kc))]
if !ok {
return nil
}

r := CacheTitle(string(kc))
for _, e := range ent {
if rls.Compare(*r, *CacheTitle(e.Name)) != 0 {
continue
Expand Down Expand Up @@ -1990,6 +1993,16 @@ func handleTorznabCrossSearch(w http.ResponseWriter, r *http.Request) {
http.Error(w, fmt.Sprintf("Processed: %d\n", len(processlist)), 200)
}

func CacheFormatted(title string) string {
r, ok := formattedmap.Get(title)
if !ok {
r = getFormattedTitle(title)
formattedmap.Set(title, r, ttlcache.DefaultTTL)
}

return r
}

func CacheTitle(title string) *rls.Release {
r, ok := titlemap.Get(title)
if !ok {
Expand All @@ -2001,3 +2014,15 @@ func CacheTitle(title string) *rls.Release {

return r
}

func GetOrUpdate(m *sync.RWMutex, read func() bool, update func()) {
m.RLock()
if read() {
m.RUnlock()
return
}

m.Lock()
defer m.Unlock()
update()
}

0 comments on commit bc7a3d8

Please sign in to comment.