refactor: split jikan client into transport/cache/rate subpackages

This commit is contained in:
2026-06-16 00:50:12 +02:00
committed by Milas Holsting
parent 4f73b0ca97
commit 9e25745804
6 changed files with 477 additions and 373 deletions

View File

@@ -0,0 +1,42 @@
package rate
import (
"context"
"fmt"
"sync"
"time"
)
type Limiter struct {
mu sync.Mutex
lastReqTime time.Time
interval time.Duration
}
func NewLimiter(interval time.Duration) *Limiter {
return &Limiter{interval: interval}
}
// Wait enforces minimum spacing between upstream Jikan requests.
func (l *Limiter) Wait(ctx context.Context) error {
l.mu.Lock()
defer l.mu.Unlock()
now := time.Now()
nextAllowed := l.lastReqTime.Add(l.interval)
if now.Before(nextAllowed) {
timer := time.NewTimer(nextAllowed.Sub(now))
defer timer.Stop()
select {
case <-timer.C:
case <-ctx.Done():
return fmt.Errorf("request canceled while waiting for rate limit: %w", ctx.Err())
}
l.lastReqTime = time.Now()
return nil
}
l.lastReqTime = now
return nil
}