cocainecong / go-seckill Goto Github PK
View Code? Open in Web Editor NEWGin+Gorm+Redis+ETCD的秒杀系统,列举出一系列并发情况下数据竞争问题的解决方案。
License: MIT License
Gin+Gorm+Redis+ETCD的秒杀系统,列举出一系列并发情况下数据竞争问题的解决方案。
License: MIT License
我在调用"api/v1/with-pcc-read"的时候,发现确实是超卖(准确的说,应该是少卖)的,这让我有点疑惑:事务中查询加排他锁理应不会出现超卖。当我读完源码并打印sql后,发现gorm的加锁方式 Set(gorm:query_option, for update) 并没有生效!所以我准备修改代码采用执行原生sql的方式再尝试,结果就是正确的了(没有超卖或少买的问题)。具体原因还没看,先反馈一下
func WithPccReadSecKillGoods(gid, userID int) error {
tx := model.DB.Begin()
skGood := model.PromotionSecKill{}
tx.Debug().Raw("SELECT * FROM `promotion_sec_kill` WHERE goods_id= ? ORDER BY `promotion_sec_kill`.`ps_id` LIMIT 1 FOR UPDATE", gid).First(&skGood)
count := skGood.PsCount
if count > 0 {
// 1. 扣库存
tx.Debug().Exec("UPDATE `promotion_sec_kill` SET `ps_count`= ? WHERE goods_id= ?", count-1, gid)
// 2. 创建订单
}
tx.Commit()
return nil
}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.