mirror of
https://github.com/TeaOSLab/EdgeNode.git
synced 2025-12-23 13:26:35 +08:00
优化计数器代码
This commit is contained in:
@@ -66,19 +66,16 @@ func (this *Counter) Increase(key uint64, lifeSeconds int) uint64 {
|
|||||||
var index = int(key % this.countMaps)
|
var index = int(key % this.countMaps)
|
||||||
this.locker.RLock(index)
|
this.locker.RLock(index)
|
||||||
var item = this.itemMaps[index][key]
|
var item = this.itemMaps[index][key]
|
||||||
|
var l = len(this.itemMaps[index])
|
||||||
this.locker.RUnlock(index)
|
this.locker.RUnlock(index)
|
||||||
if item == nil { // no need to care about duplication
|
if item == nil { // no need to care about duplication
|
||||||
item = NewItem(lifeSeconds)
|
if l > maxItemsPerGroup {
|
||||||
this.locker.Lock(index)
|
return 1
|
||||||
|
|
||||||
// check again
|
|
||||||
oldItem, ok := this.itemMaps[index][key]
|
|
||||||
if !ok {
|
|
||||||
this.itemMaps[index][key] = item
|
|
||||||
} else {
|
|
||||||
item = oldItem
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
item = NewItem(lifeSeconds)
|
||||||
|
this.locker.Lock(index)
|
||||||
|
this.itemMaps[index][key] = item
|
||||||
this.locker.Unlock(index)
|
this.locker.Unlock(index)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user