fix nil crash

fix https://github.com/chrislusf/seaweedfs/issues/320
This commit is contained in:
Chris Lu 2016-06-13 14:34:05 -07:00
parent f6d75476b9
commit a61453d3ec

View file

@ -3,6 +3,7 @@ package operation
import ( import (
"errors" "errors"
"strconv" "strconv"
"sync"
"time" "time"
"github.com/chrislusf/seaweedfs/weed/glog" "github.com/chrislusf/seaweedfs/weed/glog"
@ -13,6 +14,7 @@ type VidInfo struct {
NextRefreshTime time.Time NextRefreshTime time.Time
} }
type VidCache struct { type VidCache struct {
sync.RWMutex
cache []VidInfo cache []VidInfo
} }
@ -22,6 +24,8 @@ func (vc *VidCache) Get(vid string) ([]Location, error) {
glog.V(1).Infof("Unknown volume id %s", vid) glog.V(1).Infof("Unknown volume id %s", vid)
return nil, err return nil, err
} }
vc.RLock()
defer vc.RUnlock()
if 0 < id && id <= len(vc.cache) { if 0 < id && id <= len(vc.cache) {
if vc.cache[id-1].Locations == nil { if vc.cache[id-1].Locations == nil {
return nil, errors.New("Not Set") return nil, errors.New("Not Set")
@ -39,6 +43,8 @@ func (vc *VidCache) Set(vid string, locations []Location, duration time.Duration
glog.V(1).Infof("Unknown volume id %s", vid) glog.V(1).Infof("Unknown volume id %s", vid)
return return
} }
vc.Lock()
defer vc.Unlock()
if id > len(vc.cache) { if id > len(vc.cache) {
for i := id - len(vc.cache); i > 0; i-- { for i := id - len(vc.cache); i > 0; i-- {
vc.cache = append(vc.cache, VidInfo{}) vc.cache = append(vc.cache, VidInfo{})