diff --git a/modules/timer/parkour.go b/modules/timer/parkour.go index 9ae66158d..71d664530 100644 --- a/modules/timer/parkour.go +++ b/modules/timer/parkour.go @@ -7,6 +7,7 @@ import ( "go_dreamfactory/pb" "go_dreamfactory/sys/db" "sync" + "sync/atomic" "time" "go_dreamfactory/lego/base" @@ -24,14 +25,15 @@ import ( */ type ParkourComp struct { modules.MCompConfigure - service base.IRPCXService - module *Timer - refresh time.Time //上一次刷新时间 - ulock sync.RWMutex - users []*pb.DBRaceMember //推荐用户信息 - lock sync.RWMutex - teams map[string][]*pb.DBRaceMember - total int32 + service base.IRPCXService + module *Timer + refresh time.Time //上一次刷新时间 + ulock sync.RWMutex + users []*pb.DBRaceMember //推荐用户信息 + tlock sync.RWMutex + teams map[string][]*pb.DBRaceMember + timerlock int32 + total int32 } //组件初始化接口 @@ -39,6 +41,7 @@ func (this *ParkourComp) Init(service core.IService, module core.IModule, comp c this.MCompConfigure.Init(service, module, comp, options) this.service = service.(base.IRPCXService) this.module = module.(*Timer) + this.timerlock = 1 this.teams = make(map[string][]*pb.DBRaceMember) return } @@ -50,7 +53,7 @@ func (this *ParkourComp) Start() (err error) { this.refreshlist() this.service.RegisterFunctionName(string(comm.RPC_ParkourJoinMatch), this.join) this.service.RegisterFunctionName(string(comm.RPC_ParkourCancelMatch), this.cancel) - if _, err = cron.AddFunc("5 * * * * ?", this.timer); err != nil { + if _, err = cron.AddFunc("*/10 * * * * ?", this.match); err != nil { this.module.Errorf("cron.AddFunc err:%v", err) } if _, err = cron.AddFunc("1 1 * * * ?", this.refreshlist); err != nil { @@ -102,110 +105,38 @@ func (this *ParkourComp) refreshlist() { //加入匹配中 func (this *ParkourComp) join(ctx context.Context, req *pb.RPCParkourJoinMatchReq, resp *pb.RPCParkourJoinMatchResp) (err error) { - this.lock.Lock() - defer this.lock.Unlock() + this.tlock.Lock() this.teams[req.Captainid] = req.Member this.total += int32(len(req.Member)) - if this.total >= 6 { - var ( - teams1 []string = make([]string, 0) - teams2 []string = make([]string, 0) - teams3 []string = make([]string, 0) - red []string = make([]string, 0) - reduser []*pb.DBRaceMember = make([]*pb.DBRaceMember, 0) - bule []string = make([]string, 0) - buleuser []*pb.DBRaceMember = make([]*pb.DBRaceMember, 0) - ) - for k, v := range this.teams { - if len(v) == 1 { - teams1 = append(teams1, k) - } else if len(v) == 2 { - teams2 = append(teams2, k) - } else { - teams3 = append(teams3, k) - } - } - - //找红队 - if len(teams3) > 0 { - red = append(red, teams3[0]) - teams3 = teams3[1:] - } else if len(teams2) > 0 && len(teams1) > 0 { - red = append(red, teams2[0], teams1[0]) - teams2 = teams2[1:] - teams1 = teams1[1:] - } else if len(teams1) >= 3 { - red = append(red, teams1[0], teams1[1], teams1[2]) - teams1 = teams1[3:] - } - - if len(red) == 0 { - return - } - - //找蓝队 - if len(teams3) > 0 { - bule = append(bule, teams3[0]) - teams3 = teams3[1:] - } else if len(teams2) > 0 && len(teams1) > 0 { - bule = append(bule, teams2[0], teams1[0]) - teams2 = teams2[1:] - teams1 = teams1[1:] - } else if len(teams1) >= 3 { - bule = append(bule, teams1[0], teams1[1], teams1[2]) - teams1 = teams1[3:] - } - - if len(bule) == 0 { - return - } - for _, v := range red { - reduser = append(reduser, this.teams[v]...) - } - for _, v := range bule { - buleuser = append(reduser, this.teams[v]...) - } - - ctx, _ := context.WithTimeout(context.Background(), time.Second*2) - if err = this.service.RpcCall(ctx, - comm.Service_Worker, - string(comm.RPC_ParkourMatchSucc), - &pb.RPCParkourMatchSuccReq{Red: reduser, Bule: buleuser}, - &pb.RPCParkourMatchSuccResp{}, - ); err != nil { - this.module.Errorln(err) - return - } - - for _, v := range red { - delete(this.teams, v) - } - for _, v := range bule { - delete(this.teams, v) - } - this.total -= 6 + this.tlock.Unlock() + if this.total >= 6 && atomic.LoadInt32(&this.timerlock) == 1 { + this.match() } return } //加入匹配中 func (this *ParkourComp) cancel(ctx context.Context, req *pb.RPCParkourCancelMatchReq, resp *pb.RPCParkourCancelMatchResp) (err error) { - this.lock.Lock() + this.tlock.Lock() delete(this.teams, req.Captainid) - this.lock.Unlock() + this.tlock.Unlock() return } //定时匹配处理 -func (this *ParkourComp) timer() { - this.module.Errorf("捕羊大赛 定时匹配,%d", this.total) +func (this *ParkourComp) match() { + // this.module.Debug("执行一次匹配!") + if !atomic.CompareAndSwapInt32(&this.timerlock, 1, 2) { //正在执行,就不要在进来了 + return + } + + defer atomic.StoreInt32(&this.timerlock, 1) //执行完毕释放 + + // this.module.Errorf("捕羊大赛 定时匹配,%d", this.total) if this.total == 0 { return } - this.ulock.RLock() - this.ulock.RUnlock() - this.lock.Lock() - defer this.lock.Unlock() + var ( ok bool err error @@ -222,6 +153,7 @@ func (this *ParkourComp) timer() { buleuser []*pb.DBRaceMember = make([]*pb.DBRaceMember, 0) order bool = false ) + this.tlock.Lock() for k, v := range this.teams { if len(v) == 1 { teams1 = append(teams1, k) @@ -231,7 +163,9 @@ func (this *ParkourComp) timer() { teams3 = append(teams3, k) } } - this.ulock.Lock() + this.tlock.Unlock() + + this.ulock.RLock() users = make([]*pb.DBRaceMember, 0, len(this.users)) for _, v1 := range this.users { ok = false @@ -246,7 +180,7 @@ func (this *ParkourComp) timer() { users = append(users, v1) } } - this.ulock.Unlock() + this.ulock.RUnlock() if len(users)+int(this.total) < 6 { //人员不足 return @@ -336,12 +270,14 @@ func (this *ParkourComp) timer() { fn() //做多执行三次即可 } } + this.tlock.RLock() for _, v := range red { reduser = append(reduser, this.teams[v]...) } for _, v := range bule { buleuser = append(buleuser, this.teams[v]...) } + this.tlock.RLock() if len(users)+rednum+bulenum < 6 { return @@ -393,7 +329,7 @@ func (this *ParkourComp) timer() { this.module.Errorln(err) return } - + this.tlock.Lock() for _, v := range red { delete(this.teams, v) } @@ -402,6 +338,7 @@ func (this *ParkourComp) timer() { } this.total -= int32(rednum) this.total -= int32(bulenum) + this.tlock.Lock() } // 从远程库查询用户 diff --git a/modules/user/comp_configure.go b/modules/user/comp_configure.go index 0316c65ac..899255944 100644 --- a/modules/user/comp_configure.go +++ b/modules/user/comp_configure.go @@ -20,9 +20,9 @@ const ( ///配置管理基础组件 type configureComp struct { - hlock sync.RWMutex modules.MCompConfigure module *User + hlock sync.RWMutex _sign map[int32]*cfg.GameSignData _signExtra map[int32]*cfg.GameSignExtraData } @@ -30,7 +30,7 @@ type configureComp struct { //组件初始化接口 func (this *configureComp) Init(service core.IService, module core.IModule, comp core.IModuleComp, options core.IModuleOptions) (err error) { err = this.MCompConfigure.Init(service, module, comp, options) - module = module.(*User) + this.module = module.(*User) err = this.LoadConfigure(game_initial, cfg.NewGameInitial) this._sign = make(map[int32]*cfg.GameSignData, 0) configure.RegisterConfigure(game_sign, cfg.NewGameSign, this.LoadSignData)