Skip to content

Commit

Permalink
add recover function that handles panic
Browse files Browse the repository at this point in the history
  • Loading branch information
bluele committed Sep 2, 2017
1 parent 65d5d4f commit e2d3d44
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package gcache

import (
"errors"
"fmt"
"sync"
"time"
)
Expand Down Expand Up @@ -178,7 +179,12 @@ func buildCache(c *baseCache, cb *CacheBuilder) {

// load a new value using by specified key.
func (c *baseCache) load(key interface{}, cb func(interface{}, *time.Duration, error) (interface{}, error), isWait bool) (interface{}, bool, error) {
v, called, err := c.loadGroup.Do(key, func() (interface{}, error) {
v, called, err := c.loadGroup.Do(key, func() (v interface{}, e error) {
defer func() {
if r := recover(); r != nil {
e = fmt.Errorf("Loader panics: %v", r)
}
}()
return cb(c.loaderExpireFunc(key))
}, isWait)
if err != nil {
Expand Down

0 comments on commit e2d3d44

Please sign in to comment.