feat: implement retry logic for processing tariff chunks in repository API

This commit is contained in:
2025-08-19 10:41:08 +03:00
parent e9f46d7b14
commit 818e3f3252

View File

@@ -216,18 +216,25 @@ func (r *apiRepository) CalculateProductTariffs(ctx context.Context, marketplace
} }
offerChunks := lo.Chunk(req.Offers, defaultChunkSize) offerChunks := lo.Chunk(req.Offers, defaultChunkSize)
maxRetries := 5
for chunkIndex, offerChunk := range offerChunks { for chunkIndex, offerChunk := range offerChunks {
fmt.Printf("Processing chunk %d/%d with %d offers\n", chunkIndex+1, len(offerChunks), len(offerChunk)) 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 response != nil {
if err != nil { resultChan <- []*pb.CalculateProductTariffsResponse{response}
errChan <- err break
return }
} }
if globalError != nil {
if response != nil { fmt.Printf("Error processing chunk %d: %s\n", chunkIndex+1, err)
resultChan <- []*pb.CalculateProductTariffsResponse{response} 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). CalculateTariffsRequest(*ymRequest).
Execute() Execute()
if httpResp != nil && httpResp.Body != nil {
_ = httpResp.Body.Close()
}
if err != nil { 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) return nil, fmt.Errorf("failed to call Yandex Market API for chunk %d: %w", chunkIndex+1, err)
} }
if response == nil || response.Result == nil { if response == nil || response.Result == nil {