@@ -392,59 +392,60 @@ func (c *FullCachingSource) removeFile(m libkb.MetaContext, ent *lru.DiskLRUEntr
392392
393393func (c * FullCachingSource ) populateCacheWorker (m libkb.MetaContext ) {
394394 for arg := range c .populateCacheCh {
395- c .debug (m , "populateCacheWorker: fetching: name: %s format: %s url: %s" , arg .name ,
396- arg .format , arg .url )
397- // Grab image data first
398- url := arg .url .String ()
399- resp , err := libkb .ProxyHTTPGet (m .G (), m .G ().GetEnv (), url , "FullCachingSource: Avatar" )
395+ err := c .populateCacheJob (m , arg )
400396 if err != nil {
401- c .debug (m , "populateCacheWorker: failed to download avatar: %s" , err )
402- continue
403- }
404- // Find any previous path we stored this image at on the disk
405- var previousEntry lruEntry
406- var previousPath string
407- key := c .avatarKey (arg .name , arg .format )
408- found , ent , err := c .diskLRU .Get (m .Ctx (), m .G (), key )
409- if err != nil {
410- c .debug (m , "populateCacheWorker: failed to read previous entry in LRU: %s" , err )
411- err = libkb .DiscardAndCloseBody (resp )
412- if err != nil {
413- c .debug (m , "populateCacheWorker: error closing body: %+v" , err )
414- }
415- continue
416- }
417- if found {
418- previousEntry = c .processLRUHit (ent )
419- previousPath = c .normalizeFilenameFromCache (m , previousEntry .Path )
397+ c .debug (m , "populateCacheWorker: %s" , err )
420398 }
399+ }
400+ }
421401
422- // Save to disk
423- path , err := c .commitAvatarToDisk (m , resp .Body , previousPath )
424- discardErr := libkb .DiscardAndCloseBody (resp )
425- if discardErr != nil {
426- c .debug (m , "populateCacheWorker: error closing body: %+v" , discardErr )
427- }
428- if err != nil {
429- c .debug (m , "populateCacheWorker: failed to write to disk: %s" , err )
430- continue
431- }
432- v := lruEntry {
433- Path : path ,
434- URL : & url ,
435- }
436- evicted , err := c .diskLRU .Put (m .Ctx (), m .G (), key , v )
437- if err != nil {
438- c .debug (m , "populateCacheWorker: failed to put into LRU: %s" , err )
439- continue
440- }
441- // Remove any evicted file (if there is one)
442- c .removeFile (m , evicted )
402+ func (c * FullCachingSource ) populateCacheJob (m libkb.MetaContext , arg populateArg ) (err error ) {
403+ c .debug (m , "populateCacheWorker: fetching: name: %s format: %s url: %s" , arg .name ,
404+ arg .format , arg .url )
405+ // Grab image data first
406+ url := arg .url .String ()
407+ resp , err := libkb .ProxyHTTPGet (m .G (), m .G ().GetEnv (), url , "FullCachingSource: Avatar" )
408+ defer func () { _ = libkb .DiscardAndCloseBody (resp ) }()
409+ if err != nil {
410+ c .debug (m , "populateCacheWorker: failed to download avatar: %s" , err )
411+ return err
412+ }
413+ // Find any previous path we stored this image at on the disk
414+ var previousEntry lruEntry
415+ var previousPath string
416+ key := c .avatarKey (arg .name , arg .format )
417+ found , ent , err := c .diskLRU .Get (m .Ctx (), m .G (), key )
418+ if err != nil {
419+ c .debug (m , "populateCacheWorker: failed to read previous entry in LRU: %s" , err )
420+ return err
421+ }
422+ if found {
423+ previousEntry = c .processLRUHit (ent )
424+ previousPath = c .normalizeFilenameFromCache (m , previousEntry .Path )
425+ }
443426
444- if c .populateSuccessCh != nil {
445- c .populateSuccessCh <- struct {}{}
446- }
427+ // Save to disk
428+ path , err := c .commitAvatarToDisk (m , resp .Body , previousPath )
429+ if err != nil {
430+ c .debug (m , "populateCacheWorker: failed to write to disk: %s" , err )
431+ return err
447432 }
433+ v := lruEntry {
434+ Path : path ,
435+ URL : & url ,
436+ }
437+ evicted , err := c .diskLRU .Put (m .Ctx (), m .G (), key , v )
438+ if err != nil {
439+ c .debug (m , "populateCacheWorker: failed to put into LRU: %s" , err )
440+ return err
441+ }
442+ // Remove any evicted file (if there is one)
443+ c .removeFile (m , evicted )
444+
445+ if c .populateSuccessCh != nil {
446+ c .populateSuccessCh <- struct {}{}
447+ }
448+ return nil
448449}
449450
450451func (c * FullCachingSource ) dispatchPopulateFromRes (m libkb.MetaContext , res keybase1.LoadAvatarsRes ,
0 commit comments