GithubHelp home page GithubHelp logo

cocainecong / go-seckill Goto Github PK

View Code? Open in Web Editor NEW
26.0 2.0 6.0 58 KB

Gin+Gorm+Redis+ETCD的秒杀系统,列举出一系列并发情况下数据竞争问题的解决方案。

License: MIT License

Go 100.00%
gin go etcd gorm redis redission

go-seckill's Issues

关于“数据库悲观锁,查询加锁超卖”的问题

我在调用"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
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.