refactor: dedupe jikan refresh

This commit is contained in:
2026-06-01 22:21:19 +02:00
committed by Milas Holsting
parent 085fe3e83d
commit be27625a3c
2 changed files with 9 additions and 14 deletions

View File

@@ -94,18 +94,7 @@ func (c *Client) refreshAnimeByID(ctx context.Context, id int) (Anime, error) {
}
func (c *Client) refreshAnimeByIDAsync(id int) {
select {
case c.refreshSem <- struct{}{}:
default:
return
}
go func() {
defer func() { <-c.refreshSem }()
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
defer cancel()
c.runAsyncRefresh(func(ctx context.Context) {
_, _ = c.refreshAnimeByID(ctx, id)
}()
})
}

View File

@@ -410,6 +410,12 @@ func (c *Client) refreshWithCacheAsync(cacheKey string, ttl time.Duration, url s
return
}
c.runAsyncRefresh(func(ctx context.Context) {
_ = c.refreshWithCache(ctx, cacheKey, ttl, url, target)
})
}
func (c *Client) runAsyncRefresh(refresh func(context.Context)) {
select {
case c.refreshSem <- struct{}{}:
default:
@@ -422,7 +428,7 @@ func (c *Client) refreshWithCacheAsync(cacheKey string, ttl time.Duration, url s
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
defer cancel()
_ = c.refreshWithCache(ctx, cacheKey, ttl, url, target)
refresh(ctx)
}()
}