feat: implement retry logic for processing tariff chunks in repository API
This commit is contained in:
@@ -216,18 +216,25 @@ func (r *apiRepository) CalculateProductTariffs(ctx context.Context, marketplace
|
||||
}
|
||||
|
||||
offerChunks := lo.Chunk(req.Offers, defaultChunkSize)
|
||||
|
||||
maxRetries := 5
|
||||
for chunkIndex, offerChunk := range offerChunks {
|
||||
fmt.Printf("Processing chunk %d/%d with %d offers\n", chunkIndex+1, len(offerChunks), len(offerChunk))
|
||||
var globalError error = nil
|
||||
for range maxRetries {
|
||||
response, err := r.processTariffChunk(ctx, client, ymParameters, offerChunk, chunkIndex)
|
||||
if err != nil {
|
||||
globalError = err
|
||||
continue
|
||||
}
|
||||
|
||||
response, err := r.processTariffChunk(ctx, client, ymParameters, offerChunk, chunkIndex)
|
||||
if err != nil {
|
||||
errChan <- err
|
||||
return
|
||||
if response != nil {
|
||||
resultChan <- []*pb.CalculateProductTariffsResponse{response}
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if response != nil {
|
||||
resultChan <- []*pb.CalculateProductTariffsResponse{response}
|
||||
if globalError != nil {
|
||||
fmt.Printf("Error processing chunk %d: %s\n", chunkIndex+1, err)
|
||||
errChan <- fmt.Errorf("failed to process chunk %d: %w", chunkIndex+1, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -258,12 +265,16 @@ func (r *apiRepository) processTariffChunk(ctx context.Context, client *ymclient
|
||||
CalculateTariffsRequest(*ymRequest).
|
||||
Execute()
|
||||
|
||||
if httpResp != nil && httpResp.Body != nil {
|
||||
_ = httpResp.Body.Close()
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
if httpResp != nil && httpResp.Body != nil {
|
||||
bodyData := make([]byte, 2048)
|
||||
_, httpErr := httpResp.Body.Read(bodyData)
|
||||
if httpErr == nil {
|
||||
fmt.Printf("Error response for chunk %d: %s\n", chunkIndex+1, string(bodyData))
|
||||
}
|
||||
}
|
||||
return nil, fmt.Errorf("failed to call Yandex Market API for chunk %d: %w", chunkIndex+1, err)
|
||||
|
||||
}
|
||||
|
||||
if response == nil || response.Result == nil {
|
||||
|
||||
Reference in New Issue
Block a user