@@ -261,17 +261,24 @@ func (w *Location) fetchWeatherData(ctx context.Context, endpoint string, result
261
261
req .Header .Add ("Accept" , "application/json" )
262
262
263
263
var res * http.Response
264
- for attempts := range 3 {
264
+ for attempt := range 3 {
265
265
res , err = client .Do (req )
266
266
if err == nil {
267
267
break
268
268
}
269
- log .Error ("Request failed, retrying" , "attempt" , attempts , "url" , apiURLForLog .String (), "err" , err )
269
+ // Log attempts as 1-based for clarity
270
+ log .Error ("Request failed, retrying" , "attempt" , attempt + 1 , "url" , apiURLForLog .String (), "err" , err )
270
271
271
- time .Sleep (time .Duration (1 << attempts ) * time .Second )
272
+ backoff := time .Duration (1 << attempt ) * time .Second
273
+ select {
274
+ case <- ctx .Done ():
275
+ return ctx .Err ()
276
+ case <- time .After (backoff ):
277
+ }
272
278
}
279
+
273
280
if err != nil {
274
- log .Error ("Request failed after retries" , "err" , err )
281
+ log .Error ("Request failed after retries" , "url" , apiURLForLog . String (), " err" , err )
275
282
return err
276
283
}
277
284
@@ -288,16 +295,10 @@ func (w *Location) fetchWeatherData(ctx context.Context, endpoint string, result
288
295
return err
289
296
}
290
297
291
- responseBody , err := io .ReadAll (res .Body )
292
- if err != nil {
293
- log .Error ("reading response body" , "url" , apiURLForLog .String (), "err" , err )
294
- return err
295
- }
296
-
297
- unmarshalErr := json .Unmarshal (responseBody , result )
298
- if unmarshalErr != nil {
299
- log .Error ("fetchWeatherData" , "err" , unmarshalErr )
300
- return unmarshalErr
298
+ decErr := json .NewDecoder (res .Body ).Decode (result )
299
+ if decErr != nil {
300
+ log .Error ("fetchWeatherData" , "err" , decErr )
301
+ return decErr
301
302
}
302
303
303
304
return nil
0 commit comments