mirror of
https://github.com/TeaOSLab/EdgeAPI.git
synced 2025-11-23 23:10:26 +08:00
优化自增锁算法
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/zero"
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
"github.com/iwind/TeaGo/Tea"
|
||||
"github.com/iwind/TeaGo/dbs"
|
||||
@@ -11,6 +12,10 @@ import (
|
||||
|
||||
type SysLockerDAO dbs.DAO
|
||||
|
||||
// concurrent transactions control
|
||||
// 考虑到存在多个API节点的可能性,容量不能太大,也不能使用mutex
|
||||
var sysLockerConcurrentLimiter = make(chan zero.Zero, 8)
|
||||
|
||||
func NewSysLockerDAO() *SysLockerDAO {
|
||||
return dbs.NewDAO(&SysLockerDAO{
|
||||
DAOObject: dbs.DAOObject{
|
||||
@@ -117,6 +122,12 @@ func (this *SysLockerDAO) Increase(tx *dbs.Tx, key string, defaultValue int64) (
|
||||
if tx == nil {
|
||||
var result int64
|
||||
var err error
|
||||
|
||||
sysLockerConcurrentLimiter <- zero.Zero{} // push
|
||||
defer func() {
|
||||
<-sysLockerConcurrentLimiter // pop
|
||||
}()
|
||||
|
||||
err = this.Instance.RunTx(func(tx *dbs.Tx) error {
|
||||
result, err = this.Increase(tx, key, defaultValue)
|
||||
if err != nil {
|
||||
@@ -142,11 +153,10 @@ func (this *SysLockerDAO) Increase(tx *dbs.Tx, key string, defaultValue int64) (
|
||||
FindInt64Col(0)
|
||||
}
|
||||
|
||||
|
||||
// 读取当前版本号
|
||||
func (this *SysLockerDAO) Read(tx *dbs.Tx, key string) (int64, error) {
|
||||
return this.Query(tx).
|
||||
Attr("key", key).
|
||||
Result("version").
|
||||
FindInt64Col(0)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user