下单调整分布式锁

This commit is contained in:
momo 2023-11-27 17:13:03 +08:00
parent b91ea42ab6
commit 245b12d69d

View File

@ -46,20 +46,17 @@ func (l *CreateOrderLogic) CreateOrder(req *types.CreateOrderReq, userinfo *auth
return resp.SetStatus(basic.CodeUnAuth) return resp.SetStatus(basic.CodeUnAuth)
} }
var lockKey string
for _, v := range req.CartIds { for _, v := range req.CartIds {
var vStr = strconv.Itoa(int(v)) var lockKey = strconv.Itoa(int(v))
lockKey = lockKey + "|" + vStr
}
// 分布式锁--防止重复下单 // 分布式锁--防止重复下单
_, ok1 := lockMap.Load(lockKey) //获取锁 _, ok1 := lockMap.Load(lockKey) //获取锁
if ok1 { if ok1 {
return resp.SetStatusWithMessage(basic.CodeErrOrder, "create order failed, cannot place duplicate order") return resp.SetStatusWithMessage(basic.CodeErrOrder, "create order failed, cannot place duplicate order")
} else { } else {
lockMap.Store(lockKey, true) //加锁 lockMap.Store(lockKey, true) //加锁
defer lockMap.Delete(lockKey) //释放锁 defer lockMap.Delete(lockKey) //释放锁
} }
}
tPlus60Days := time.Now().AddDate(0, 0, 60).UTC() tPlus60Days := time.Now().AddDate(0, 0, 60).UTC()
res, err := l.svcCtx.Repositories.NewOrder.Create(l.ctx, &repositories.CreateReq{ res, err := l.svcCtx.Repositories.NewOrder.Create(l.ctx, &repositories.CreateReq{