defer unlock

This commit is contained in:
Nan Deng 2014-06-18 16:34:47 -07:00
parent 77dc470f54
commit 3f6fa477a4

View File

@ -169,24 +169,34 @@ type InMemoryStorage struct {
func (self *InMemoryStorage) AddStats(ref info.ContainerReference, stats *info.ContainerStats) error { func (self *InMemoryStorage) AddStats(ref info.ContainerReference, stats *info.ContainerStats) error {
var cstore *containerStorage var cstore *containerStorage
var ok bool var ok bool
self.lock.Lock()
if cstore, ok = self.containerStorageMap[ref.Name]; !ok { func() {
cstore = newContainerStore(ref, self.maxNumSamples, self.maxNumStats) self.lock.Lock()
self.containerStorageMap[ref.Name] = cstore defer self.lock.Unlock()
} if cstore, ok = self.containerStorageMap[ref.Name]; !ok {
self.lock.Unlock() cstore = newContainerStore(ref, self.maxNumSamples, self.maxNumStats)
self.containerStorageMap[ref.Name] = cstore
}
}()
return cstore.AddStats(stats) return cstore.AddStats(stats)
} }
func (self *InMemoryStorage) Samples(name string, numSamples int) ([]*info.ContainerStatsSample, error) { func (self *InMemoryStorage) Samples(name string, numSamples int) ([]*info.ContainerStatsSample, error) {
var cstore *containerStorage var cstore *containerStorage
var ok bool var ok bool
self.lock.RLock()
if cstore, ok = self.containerStorageMap[name]; !ok { err := func() error {
self.lock.RUnlock() self.lock.RLock()
return nil, fmt.Errorf("unable to find data for container %v", name) defer self.lock.RUnlock()
if cstore, ok = self.containerStorageMap[name]; !ok {
return fmt.Errorf("unable to find data for container %v", name)
}
return nil
}()
if err != nil {
return nil, err
} }
self.lock.RUnlock()
return cstore.Samples(numSamples) return cstore.Samples(numSamples)
} }
@ -194,12 +204,17 @@ func (self *InMemoryStorage) Samples(name string, numSamples int) ([]*info.Conta
func (self *InMemoryStorage) RecentStats(name string, numStats int) ([]*info.ContainerStats, error) { func (self *InMemoryStorage) RecentStats(name string, numStats int) ([]*info.ContainerStats, error) {
var cstore *containerStorage var cstore *containerStorage
var ok bool var ok bool
self.lock.RLock() err := func() error {
if cstore, ok = self.containerStorageMap[name]; !ok { self.lock.RLock()
self.lock.RUnlock() defer self.lock.RUnlock()
return nil, fmt.Errorf("unable to find data for container %v", name) if cstore, ok = self.containerStorageMap[name]; !ok {
return fmt.Errorf("unable to find data for container %v", name)
}
return nil
}()
if err != nil {
return nil, err
} }
self.lock.RUnlock()
return cstore.RecentStats(numStats) return cstore.RecentStats(numStats)
} }
@ -207,12 +222,17 @@ func (self *InMemoryStorage) RecentStats(name string, numStats int) ([]*info.Con
func (self *InMemoryStorage) Percentiles(name string, cpuPercentiles, memPercentiles []int) (*info.ContainerStatsPercentiles, error) { func (self *InMemoryStorage) Percentiles(name string, cpuPercentiles, memPercentiles []int) (*info.ContainerStatsPercentiles, error) {
var cstore *containerStorage var cstore *containerStorage
var ok bool var ok bool
self.lock.RLock() err := func() error {
if cstore, ok = self.containerStorageMap[name]; !ok { self.lock.RLock()
self.lock.RUnlock() defer self.lock.RUnlock()
return nil, fmt.Errorf("unable to find data for container %v", name) if cstore, ok = self.containerStorageMap[name]; !ok {
return fmt.Errorf("unable to find data for container %v", name)
}
return nil
}()
if err != nil {
return nil, err
} }
self.lock.RUnlock()
return cstore.Percentiles(cpuPercentiles, memPercentiles) return cstore.Percentiles(cpuPercentiles, memPercentiles)
} }