]> git.lizzy.rs Git - go-anidb.git/blob - episodecache.go
anidb: Wait longer after each UDP API timeout
[go-anidb.git] / episodecache.go
1 package anidb
2
3 import (
4         "encoding/gob"
5         "strconv"
6         "strings"
7         "time"
8 )
9
10 func init() {
11         gob.RegisterName("*github.com/Kovensky/go-anidb.Episode", &Episode{})
12 }
13
14 func (e *Episode) Touch() {
15         e.Cached = time.Now()
16 }
17
18 func (e *Episode) IsStale() bool {
19         if e == nil {
20                 return true
21         }
22         return time.Now().Sub(e.Cached) > EpisodeCacheDuration
23 }
24
25 // Unique Episode IDentifier.
26 type EID int
27
28 // Retrieves the Episode corresponding to this EID from the cache.
29 func (eid EID) Episode() *Episode {
30         var e Episode
31         if cache.Get(&e, "eid", eid) == nil {
32                 return &e
33         }
34         return nil
35 }
36
37 func cacheEpisode(ep *Episode) {
38         cache.Set(ep.AID, "aid", "by-eid", ep.EID)
39         cache.Set(ep, "eid", ep.EID)
40 }
41
42 // Retrieves the Episode from the cache if possible.
43 //
44 // If the result is stale, then queries the UDP API to
45 // know which AID owns this EID, then gets the episode
46 // from the Anime.
47 func (adb *AniDB) EpisodeByID(eid EID) <-chan *Episode {
48         keys := []cacheKey{"eid", eid}
49         ch := make(chan *Episode, 1)
50
51         ic := make(chan Cacheable, 1)
52         go func() { ch <- (<-ic).(*Episode); close(ch) }()
53         if intentMap.Intent(ic, keys...) {
54                 return ch
55         }
56
57         if !cache.CheckValid(keys...) {
58                 intentMap.Notify((*Episode)(nil), keys...)
59                 return ch
60         }
61
62         if e := eid.Episode(); !e.IsStale() {
63                 intentMap.Notify(e, keys...)
64                 return ch
65         }
66
67         go func() {
68                 // The UDP API data is worse than the HTTP API anime data,
69                 // try and get from the corresponding Anime
70
71                 aid := AID(0)
72                 ok := cache.Get(&aid, "aid", "by-eid", eid) == nil
73
74                 udpDone := false
75
76                 var e *Episode
77                 for i := 0; i < 2; i++ {
78                         if !ok && udpDone {
79                                 // couldn't get anime and we already ran the EPISODE query
80                                 break
81                         }
82
83                         if !ok {
84                                 // We don't know what the AID is yet.
85                                 reply := <-adb.udp.SendRecv("EPISODE", paramMap{"eid": eid})
86
87                                 if reply.Error() == nil {
88                                         parts := strings.Split(reply.Lines()[1], "|")
89
90                                         if id, err := strconv.ParseInt(parts[1], 10, 32); err == nil {
91                                                 ok = true
92                                                 aid = AID(id)
93                                         }
94                                 } else if reply.Code() == 340 {
95                                         cache.MarkInvalid(keys...)
96                                 } else {
97                                         break
98                                 }
99                                 udpDone = true
100                         }
101                         <-adb.AnimeByID(AID(aid)) // this caches episodes...
102                         e = eid.Episode()         // ...so this is now a cache hit
103
104                         if e != nil {
105                                 break
106                         } else {
107                                 // if this is somehow still a miss, then the EID<->AID map broke
108                                 cache.Delete("aid", "by-eid", eid)
109                                 ok = false
110                         }
111                 }
112                 intentMap.Notify(e, keys...)
113         }()
114         return ch
115 }