diff --git a/modules/comp_model.go b/modules/comp_model.go index b2de5dc30..f84593734 100644 --- a/modules/comp_model.go +++ b/modules/comp_model.go @@ -55,6 +55,11 @@ func (this *MCompModel) UpdateModelLogs(table string, uID string, where bson.M, return this.DBModel.UpdateModelLogs(table, uID, where, target) } +//创建分布式锁 +func (this *MCompModel) NewRedisMutex(key string, outtime int) (result *redis.RedisMutex, err error) { + return this.DBModel.NewRedisMutex(key, outtime) +} + //添加新的数据 func (this *MCompModel) Add(uid string, data interface{}, opt ...db.DBOption) (err error) { return this.DBModel.Add(uid, data) diff --git a/modules/moonfantasy/api_ask.go b/modules/moonfantasy/api_ask.go index 141c0beb9..1d72945c6 100644 --- a/modules/moonfantasy/api_ask.go +++ b/modules/moonfantasy/api_ask.go @@ -2,6 +2,7 @@ package moonfantasy import ( "go_dreamfactory/comm" + "go_dreamfactory/lego/sys/redis" "go_dreamfactory/pb" cfg "go_dreamfactory/sys/configure/structs" "time" @@ -19,6 +20,7 @@ func (this *apiComp) Ask(session comm.IUserSession, req *pb.MoonfantasyAskReq) ( var ( globalconf *cfg.GameGlobalData mdata *pb.DBMoonFantasy + lock *redis.RedisMutex umfantasy *pb.DBUserMFantasy user *pb.DBUser cd pb.ErrorCode @@ -30,7 +32,17 @@ func (this *apiComp) Ask(session comm.IUserSession, req *pb.MoonfantasyAskReq) ( if cd = this.AskCheck(session, req); cd != pb.ErrorCode_Success { return } - + //月之秘境需要加分布式锁 防止多人同时操作 + if lock, err = this.module.modelDream.newDreamLock(req.Mid); err != nil { + cd = pb.ErrorCode_DBError + return + } + defer lock.Unlock() + if err = lock.Lock(); err != nil { + cd = pb.ErrorCode_DBError + this.module.Errorln(err) + return + } if mdata, err = this.module.modelDream.querymfantasy(req.Mid); err != nil { cd = pb.ErrorCode_DBError return diff --git a/modules/moonfantasy/modelDream.go b/modules/moonfantasy/modelDream.go index d430c2bac..f20ff4854 100644 --- a/modules/moonfantasy/modelDream.go +++ b/modules/moonfantasy/modelDream.go @@ -1,9 +1,11 @@ package moonfantasy import ( + "fmt" "go_dreamfactory/comm" "go_dreamfactory/lego/core" "go_dreamfactory/lego/sys/mgo" + "go_dreamfactory/lego/sys/redis" "go_dreamfactory/lego/sys/timewheel" "go_dreamfactory/modules" "go_dreamfactory/pb" @@ -33,6 +35,9 @@ func (this *modelDreamComp) Init(service core.IService, module core.IModule, com }) return } +func (this *modelDreamComp) newDreamLock(mid string) (result *redis.RedisMutex, err error) { + return this.module.modelDream.NewRedisMutex(fmt.Sprintf("%s-%s_lock", this.TableName, mid), 5) +} ///添加月之秘境记录 func (this *modelDreamComp) querymfantasy(mid string) (result *pb.DBMoonFantasy, err error) { diff --git a/sys/db/dbconn.go b/sys/db/dbconn.go index 736dde47d..6a3bfd493 100644 --- a/sys/db/dbconn.go +++ b/sys/db/dbconn.go @@ -149,6 +149,12 @@ func (this *DBModel) UpdateModelLogs(table string, uID string, where bson.M, tar return err } +///创建锁对象 +func (this *DBModel) NewRedisMutex(key string, outtime int) (result *lgredis.RedisMutex, err error) { + result, err = this.Redis.NewRedisMutex(key, lgredis.SetExpiry(outtime)) + return +} + //添加新的数据 func (this *DBModel) Add(uid string, data interface{}, opt ...DBOption) (err error) { //defer log.Debug("DBModel Add", log.Field{Key: "TableName", Value: this.TableName}, log.Field{Key: "uid", Value: uid}, log.Field{Key: "data", Value: data})