From d70949362e23abc5e07f542f606c9441bf08a211 Mon Sep 17 00:00:00 2001 From: mkelvers Date: Wed, 8 Apr 2026 16:29:10 +0200 Subject: [PATCH] fix: increase jikan rate limit backoff and retries to handle 60/min bucket --- internal/jikan/client.go | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/internal/jikan/client.go b/internal/jikan/client.go index 4e95f47..77fdf94 100644 --- a/internal/jikan/client.go +++ b/internal/jikan/client.go @@ -32,7 +32,9 @@ func (c *Client) waitRateLimit() { defer c.mu.Unlock() now := time.Now() - nextAllowed := c.lastReqTime.Add(340 * time.Millisecond) + // Jikan has a 3 req/sec limit AND a 60 req/min limit. + // 400ms base delay keeps us safely under the 3/sec limit. + nextAllowed := c.lastReqTime.Add(400 * time.Millisecond) if now.Before(nextAllowed) { time.Sleep(nextAllowed.Sub(now)) c.lastReqTime = time.Now() @@ -72,7 +74,7 @@ func (c *Client) setCache(key string, data interface{}, ttl time.Duration) { // fetchWithRetry provides robust fetching respecting Jikan's strict 3 req/sec rate limit func (c *Client) fetchWithRetry(urlStr string, out interface{}) error { - maxRetries := 3 + maxRetries := 5 for i := 0; i < maxRetries; i++ { c.waitRateLimit() @@ -83,7 +85,9 @@ func (c *Client) fetchWithRetry(urlStr string, out interface{}) error { if resp.StatusCode == 429 { resp.Body.Close() - time.Sleep(800 * time.Millisecond) // Double delay on rate limit + // Jikan rate limit is hit (usually the 60 requests/minute limit) + // Wait for 2 seconds before retrying to let the bucket refill slightly + time.Sleep(2 * time.Second) continue }