feat: enhance context handling and add options for redis locker
This commit is contained in:
@@ -11,6 +11,7 @@ import (
|
||||
wbapi "sipro-mps/pkg/api/wb/client"
|
||||
"sipro-mps/pkg/utils"
|
||||
|
||||
"github.com/go-faster/errors"
|
||||
"github.com/samber/lo"
|
||||
)
|
||||
|
||||
@@ -61,6 +62,9 @@ func fetchProducts(
|
||||
for {
|
||||
response, err := client.ContentV2GetCardsListPost(ctx, &request, wbapi.ContentV2GetCardsListPostParams{Locale: wbapi.NewOptString("ru")})
|
||||
if err != nil {
|
||||
if errors.Is(err, context.Canceled) {
|
||||
return
|
||||
}
|
||||
currentRetry++
|
||||
if currentRetry >= maxRetries {
|
||||
errChan <- fmt.Errorf("fetching product IDs: %w", err)
|
||||
@@ -131,6 +135,9 @@ func (a apiRepository) StreamAllProductsCache(ctx context.Context, marketplaceId
|
||||
go a.StreamAllProducts(ctx, marketplaceId, innerResultChan, innerErrChan)
|
||||
var allProducts []pb.Product
|
||||
defer func() {
|
||||
if ctx.Err() != nil {
|
||||
return
|
||||
}
|
||||
if len(allProducts) == 0 {
|
||||
return
|
||||
}
|
||||
@@ -138,7 +145,6 @@ func (a apiRepository) StreamAllProductsCache(ctx context.Context, marketplaceId
|
||||
Products: utils.ToPtrs(allProducts),
|
||||
}
|
||||
_ = redis.WriteProtoMessage(ctx, key, &message)
|
||||
|
||||
}()
|
||||
for {
|
||||
select {
|
||||
@@ -157,6 +163,8 @@ func (a apiRepository) StreamAllProductsCache(ctx context.Context, marketplaceId
|
||||
})
|
||||
allProducts = append(allProducts, pbProducts...)
|
||||
resultChan <- pbProducts
|
||||
case <-ctx.Done():
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user