perf: fetch intermediate pages in reverse order

This commit is contained in:
2026-05-03 00:02:51 +02:00
parent a83ab2e33f
commit 31b294c979

View File

@@ -185,22 +185,20 @@ func (c *Client) GetAllEpisodes(ctx context.Context, animeID int) ([]Episode, er
} }
} }
// Background fetching for intermediate pages // Background fetching for intermediate pages (Working BACKWARDS)
if lastPage > 2 { if lastPage > 2 {
go func() { go func() {
// Create a fresh context for background work bgCtx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
bgCtx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
defer cancel() defer cancel()
for p := 2; p < lastPage; p++ { // Count backwards from the second-to-last page to page 2
// We don't need to store the result here if the client has an internal cache, for p := lastPage - 1; p >= 2; p-- {
// but calling it ensures the data is ready for the next request.
_, _ = c.GetEpisodes(bgCtx, animeID, p) _, _ = c.GetEpisodes(bgCtx, animeID, p)
select { select {
case <-bgCtx.Done(): case <-bgCtx.Done():
return return
case <-time.After(500 * time.Millisecond): // Rate limit buffer case <-time.After(600 * time.Millisecond): // Slightly slower to be safe
} }
} }
}() }()