refactor: dedupe jikan refresh
This commit is contained in:
@@ -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)
|
||||
}()
|
||||
})
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}()
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user