1、GetCommodityStocksByStoreID优化,加锁解决concurrent map writes 的问题;
This commit is contained in:
parent
8fe85abf71
commit
5418dab8c5
|
@ -2639,6 +2639,7 @@ func GetCommodityStocksByStoreID(commodityID uint32, stores []uint32) (map[uint3
|
||||||
wg.Add(len(stores))
|
wg.Add(len(stores))
|
||||||
|
|
||||||
result := make(map[uint32]uint32)
|
result := make(map[uint32]uint32)
|
||||||
|
var mu sync.Mutex // 用于保护 `result`
|
||||||
|
|
||||||
for _, store := range stores {
|
for _, store := range stores {
|
||||||
go func(storeID uint32) {
|
go func(storeID uint32) {
|
||||||
|
@ -2652,7 +2653,9 @@ func GetCommodityStocksByStoreID(commodityID uint32, stores []uint32) (map[uint3
|
||||||
// Handle error
|
// Handle error
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
mu.Lock() // 加锁
|
||||||
result[storeID] = uint32(stockCount)
|
result[storeID] = uint32(stockCount)
|
||||||
|
mu.Unlock() // 解锁
|
||||||
}(store)
|
}(store)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user