]> git.lizzy.rs Git - go-anidb.git/blobdiff - episodecache.go
anidb: Add missing loop 'break's to EpisodeByEID
[go-anidb.git] / episodecache.go
index 0ed142e262b4f2062bb0a78132f0a82a3d92d5c3..67d12b0fefaab471f407d2cbca53313987a2a447 100644 (file)
@@ -40,6 +40,10 @@ func cacheEpisode(ep *Episode) {
 }
 
 // Retrieves an Episode by its EID.
+//
+// If we know which AID owns this EID, then it's equivalent
+// to an Anime query. Otherwise, uses both the HTTP and UDP
+// APIs to retrieve it.
 func (adb *AniDB) EpisodeByID(eid EID) <-chan *Episode {
        keys := []cacheKey{"eid", eid}
        ch := make(chan *Episode, 1)
@@ -86,9 +90,13 @@ func (adb *AniDB) EpisodeByID(eid EID) <-chan *Episode {
                                        if id, err := strconv.ParseInt(parts[1], 10, 32); err == nil {
                                                ok = true
                                                aid = AID(id)
+                                       } else {
+                                               break
                                        }
                                } else if reply.Code() == 340 {
                                        cache.MarkInvalid(keys...)
+                                       cache.Delete(keys...) // deleted EID?
+                                       break
                                } else {
                                        break
                                }
@@ -101,9 +109,25 @@ func (adb *AniDB) EpisodeByID(eid EID) <-chan *Episode {
                                e = ep
                                break
                        } else {
-                               // if this is somehow still a miss, then the EID<->AID map broke
-                               cache.Delete("aid", "by-eid", eid)
-                               ok = false
+                               // check to see if we looked in the right AID
+                               found := false
+                               if a != nil {
+                                       for _, ep := range a.Episodes {
+                                               if eid == ep.EID {
+                                                       found = true
+                                                       break
+                                               }
+                                       }
+                               }
+
+                               // if found, then it's just that the anime is also stale (offline?)
+                               if found {
+                                       break
+                               } else {
+                                       // otherwise, the EID<->AID map broke
+                                       ok = false
+                                       cache.Delete("aid", "by-eid", eid)
+                               }
                        }
                }
                intentMap.Notify(e, keys...)