@@ -371,50 +371,63 @@ func (r *Reader) readRemoteNodeContent(ctx context.Context, node RemoteNode) ([]
371
371
timestamp := cache .ReadTimestamp ()
372
372
expiry := timestamp .Add (r .cacheExpiryDuration )
373
373
cacheValid := now .Before (expiry )
374
-
375
- // If we have not been been forced to download, check the cache
376
- if ! r .download {
377
- r .debugf ("checking cache for %q in %q\n " , node .Location (), cache .Location ())
378
- b , err := cache .Read ()
379
- switch {
380
- // If the cache doesn't exist, we need to download the file
381
- case errors .Is (err , os .ErrNotExist ):
382
- r .debugf ("no cache found\n " )
383
- // If we couldn't find a cached copy, and we are offline, we can't do anything
384
- if r .offline {
385
- return nil , & errors.TaskfileCacheNotFoundError {
386
- URI : node .Location (),
387
- }
374
+ var cacheFound bool
375
+
376
+ r .debugf ("checking cache for %q in %q\n " , node .Location (), cache .Location ())
377
+ cachedBytes , err := cache .Read ()
378
+ switch {
379
+ // If the cache doesn't exist, we need to download the file
380
+ case errors .Is (err , os .ErrNotExist ):
381
+ r .debugf ("no cache found\n " )
382
+ // If we couldn't find a cached copy, and we are offline, we can't do anything
383
+ if r .offline {
384
+ return nil , & errors.TaskfileCacheNotFoundError {
385
+ URI : node .Location (),
388
386
}
387
+ }
389
388
390
- // If the cache is expired
391
- case ! cacheValid :
392
- r .debugf ("cache expired at %s\n " , expiry .Format (time .RFC3339 ))
393
- // If we can't fetch a fresh copy, we should use the cache anyway
394
- if r .offline {
395
- r .debugf ("in offline mode, using expired cache\n " )
396
- return b , nil
397
- }
389
+ // If the cache is expired
390
+ case ! cacheValid :
391
+ r .debugf ("cache expired at %s\n " , expiry .Format (time .RFC3339 ))
392
+ cacheFound = true
393
+ // If we can't fetch a fresh copy, we should use the cache anyway
394
+ if r .offline {
395
+ r .debugf ("in offline mode, using expired cache\n " )
396
+ return cachedBytes , nil
397
+ }
398
398
399
- // Some other error
400
- case err != nil :
401
- return nil , err
399
+ // Some other error
400
+ case err != nil :
401
+ return nil , err
402
402
403
- // Found valid cache, return it
404
- default :
405
- r .debugf ("cache found\n " )
406
- return b , nil
403
+ // Found valid cache
404
+ default :
405
+ r .debugf ("cache found\n " )
406
+ // Not being forced to redownload, return cache
407
+ if ! r .download {
408
+ return cachedBytes , nil
407
409
}
410
+ cacheFound = true
408
411
}
409
412
410
413
// Try to read the remote file
411
- b , err := node .ReadContext (ctx )
414
+ r .debugf ("downloading remote file: %s\n " , node .Location ())
415
+ downloadedBytes , err := node .ReadContext (ctx )
412
416
if err != nil {
417
+ // If the context timed out or was cancelled, but we found a cached version, use that
418
+ if ctx .Err () != nil && cacheFound {
419
+ if cacheValid {
420
+ r .debugf ("failed to fetch remote file: %s: using cache\n " , ctx .Err ().Error ())
421
+ } else {
422
+ r .debugf ("failed to fetch remote file: %s: using expired cache\n " , ctx .Err ().Error ())
423
+ }
424
+ return cachedBytes , nil
425
+ }
413
426
return nil , err
414
427
}
415
428
416
429
r .debugf ("found remote file at %q\n " , node .Location ())
417
- checksum := checksum (b )
430
+ checksum := checksum (downloadedBytes )
418
431
prompt := cache .ChecksumPrompt (checksum )
419
432
420
433
// Prompt the user if required
@@ -440,9 +453,9 @@ func (r *Reader) readRemoteNodeContent(ctx context.Context, node RemoteNode) ([]
440
453
441
454
// Cache the file
442
455
r .debugf ("caching %q to %q\n " , node .Location (), cache .Location ())
443
- if err = cache .Write (b ); err != nil {
456
+ if err = cache .Write (downloadedBytes ); err != nil {
444
457
return nil , err
445
458
}
446
459
447
- return b , nil
460
+ return downloadedBytes , nil
448
461
}
0 commit comments