fix: guard against division by zero when fetchBytes is 0
processAndStore() computed sizePercent as outputSize/fetchBytes*100 without checking for zero, producing Inf/NaN in logs and metrics. Also treat empty cached source data the same as missing (re-fetch from upstream) since zero-byte images can't be processed. Fixes #5
This commit is contained in:
@@ -153,8 +153,8 @@ func (s *Service) processFromSourceOrFetch(
|
||||
}
|
||||
}
|
||||
|
||||
// Fetch from upstream if we don't have source data
|
||||
if sourceData == nil {
|
||||
// Fetch from upstream if we don't have source data or it's empty
|
||||
if len(sourceData) == 0 {
|
||||
resp, err := s.fetchAndProcess(ctx, req, cacheKey)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -264,7 +264,11 @@ func (s *Service) processAndStore(
|
||||
|
||||
// Log conversion details
|
||||
outputSize := int64(len(processedData))
|
||||
sizePercent := float64(outputSize) / float64(fetchBytes) * 100.0 //nolint:mnd // percentage calculation
|
||||
|
||||
var sizePercent float64
|
||||
if fetchBytes > 0 {
|
||||
sizePercent = float64(outputSize) / float64(fetchBytes) * 100.0 //nolint:mnd // percentage calculation
|
||||
}
|
||||
|
||||
s.log.Info("image converted",
|
||||
"host", req.SourceHost,
|
||||
|
||||
Reference in New Issue
Block a user