Skip to content

Commit

Permalink
cache init err
Browse files Browse the repository at this point in the history
  • Loading branch information
juliaqiuxy committed Jun 4, 2021
1 parent 3ee0083 commit 87dc58e
Showing 1 changed file with 7 additions and 8 deletions.
15 changes: 7 additions & 8 deletions cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ type FinishStorageOp func(interface{})
type Cache struct {
storages []Storage

makers []StorageMaker
once sync.Once
makers []StorageMaker
initOnce sync.Once
initErr error

startOperation StartStorageOp
finishOperation FinishStorageOp
Expand Down Expand Up @@ -72,14 +73,12 @@ func CreateWithHooks(sop StartStorageOp, fop FinishStorageOp, maker StorageMaker
}

func (c *Cache) ensureStorages() error {
var err error

c.once.Do(func() {
c.initOnce.Do(func() {
c.storages = make([]Storage, 0, len(c.makers))
for _, makeStorage := range c.makers {
storage, serr := makeStorage()
if serr != nil {
err = serr
c.initErr = serr
// TODO(juliaqiuxy) log error
break
}
Expand All @@ -88,8 +87,8 @@ func (c *Cache) ensureStorages() error {
}
})

if err != nil {
return err
if c.initErr != nil {
return c.initErr
}

// Due to how once.Do works, if the first go routine to acquire
Expand Down

0 comments on commit 87dc58e

Please sign in to comment.