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) {
|
func (c *Client) refreshAnimeByIDAsync(id int) {
|
||||||
select {
|
c.runAsyncRefresh(func(ctx context.Context) {
|
||||||
case c.refreshSem <- struct{}{}:
|
|
||||||
default:
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
go func() {
|
|
||||||
defer func() { <-c.refreshSem }()
|
|
||||||
|
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
|
|
||||||
defer cancel()
|
|
||||||
|
|
||||||
_, _ = c.refreshAnimeByID(ctx, id)
|
_, _ = c.refreshAnimeByID(ctx, id)
|
||||||
}()
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -410,6 +410,12 @@ func (c *Client) refreshWithCacheAsync(cacheKey string, ttl time.Duration, url s
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
c.runAsyncRefresh(func(ctx context.Context) {
|
||||||
|
_ = c.refreshWithCache(ctx, cacheKey, ttl, url, target)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) runAsyncRefresh(refresh func(context.Context)) {
|
||||||
select {
|
select {
|
||||||
case c.refreshSem <- struct{}{}:
|
case c.refreshSem <- struct{}{}:
|
||||||
default:
|
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)
|
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
_ = c.refreshWithCache(ctx, cacheKey, ttl, url, target)
|
refresh(ctx)
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user