add rw-lock to mapblockaccessor, fixes #23
This commit is contained in:
parent
20addd599f
commit
f65aad778b
@ -4,15 +4,19 @@ import (
|
|||||||
"mapserver/coords"
|
"mapserver/coords"
|
||||||
"mapserver/eventbus"
|
"mapserver/eventbus"
|
||||||
"mapserver/mapblockparser"
|
"mapserver/mapblockparser"
|
||||||
|
"sync"
|
||||||
|
|
||||||
cache "github.com/patrickmn/go-cache"
|
cache "github.com/patrickmn/go-cache"
|
||||||
"github.com/prometheus/client_golang/prometheus"
|
"github.com/prometheus/client_golang/prometheus"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var lock = &sync.RWMutex{}
|
||||||
|
|
||||||
func (a *MapBlockAccessor) GetMapBlock(pos *coords.MapBlockCoords) (*mapblockparser.MapBlock, error) {
|
func (a *MapBlockAccessor) GetMapBlock(pos *coords.MapBlockCoords) (*mapblockparser.MapBlock, error) {
|
||||||
key := getKey(pos)
|
key := getKey(pos)
|
||||||
|
|
||||||
|
//maintenance
|
||||||
cacheBlocks.Set(float64(a.blockcache.ItemCount()))
|
cacheBlocks.Set(float64(a.blockcache.ItemCount()))
|
||||||
if a.blockcache.ItemCount() > a.maxcount {
|
if a.blockcache.ItemCount() > a.maxcount {
|
||||||
//flush cache
|
//flush cache
|
||||||
@ -25,8 +29,13 @@ func (a *MapBlockAccessor) GetMapBlock(pos *coords.MapBlockCoords) (*mapblockpar
|
|||||||
a.blockcache.Flush()
|
a.blockcache.Flush()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//read section
|
||||||
|
lock.RLock()
|
||||||
|
|
||||||
cachedblock, found := a.blockcache.Get(key)
|
cachedblock, found := a.blockcache.Get(key)
|
||||||
if found {
|
if found {
|
||||||
|
defer lock.RUnlock()
|
||||||
|
|
||||||
getCacheHitCount.Inc()
|
getCacheHitCount.Inc()
|
||||||
if cachedblock == nil {
|
if cachedblock == nil {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
@ -35,9 +44,27 @@ func (a *MapBlockAccessor) GetMapBlock(pos *coords.MapBlockCoords) (*mapblockpar
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//end read
|
||||||
|
lock.RUnlock()
|
||||||
|
|
||||||
timer := prometheus.NewTimer(dbGetDuration)
|
timer := prometheus.NewTimer(dbGetDuration)
|
||||||
defer timer.ObserveDuration()
|
defer timer.ObserveDuration()
|
||||||
|
|
||||||
|
//write section
|
||||||
|
lock.Lock()
|
||||||
|
defer lock.Unlock()
|
||||||
|
|
||||||
|
//try read
|
||||||
|
cachedblock, found = a.blockcache.Get(key)
|
||||||
|
if found {
|
||||||
|
getCacheHitCount.Inc()
|
||||||
|
if cachedblock == nil {
|
||||||
|
return nil, nil
|
||||||
|
} else {
|
||||||
|
return cachedblock.(*mapblockparser.MapBlock), nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
block, err := a.accessor.GetBlock(pos)
|
block, err := a.accessor.GetBlock(pos)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
Loading…
Reference in New Issue
Block a user