lock player, so that only one match can be parsed at a time/player
This commit is contained in:
@@ -56,6 +56,8 @@ type DemoMatchLoader struct {
|
|||||||
parseDemo chan *Demo
|
parseDemo chan *Demo
|
||||||
parseMap map[string]bool
|
parseMap map[string]bool
|
||||||
parseMapL *sync.RWMutex
|
parseMapL *sync.RWMutex
|
||||||
|
parsePlayerMap map[uint32]*sync.RWMutex
|
||||||
|
parsePlayerMapL *sync.RWMutex
|
||||||
cache *cache.Cache
|
cache *cache.Cache
|
||||||
connectionWait uint64
|
connectionWait uint64
|
||||||
connectFeedback chan int
|
connectFeedback chan int
|
||||||
@@ -344,7 +346,7 @@ func (dml *DemoMatchLoader) greetGC() {
|
|||||||
log.Debugf("[DL] Sending GC greeting")
|
log.Debugf("[DL] Sending GC greeting")
|
||||||
msg := protobuf.CMsgClientHello{}
|
msg := protobuf.CMsgClientHello{}
|
||||||
dml.client.GC.Write(gamecoordinator.NewGCMsgProtobuf(APPID, uint32(protobuf.EGCBaseClientMsg_k_EMsgGCClientHello), &msg))
|
dml.client.GC.Write(gamecoordinator.NewGCMsgProtobuf(APPID, uint32(protobuf.EGCBaseClientMsg_k_EMsgGCClientHello), &msg))
|
||||||
time.Sleep(500 * time.Millisecond)
|
time.Sleep(1 * time.Second)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -368,34 +370,29 @@ func (dml *DemoMatchLoader) demoWorker() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (dml *DemoMatchLoader) gcWorker(apiKey string, rl ratelimit.Limiter) {
|
func (dml *DemoMatchLoader) handleDemo(demo *Demo, apiKey string, rl ratelimit.Limiter) error {
|
||||||
for demo := range dml.parseDemo {
|
|
||||||
if dml.IsLoading(demo) {
|
if dml.IsLoading(demo) {
|
||||||
log.Infof("[DL] Skipping %s: parsing in progress", demo.ShareCode)
|
log.Infof("[DL] Skipping %s: parsing in progress", demo.ShareCode)
|
||||||
continue
|
return nil
|
||||||
}
|
}
|
||||||
dml.lockDemo(demo)
|
dml.lockDemo(demo)
|
||||||
|
defer dml.unlockDemo(demo)
|
||||||
|
|
||||||
if !dml.GCReady {
|
if !dml.GCReady {
|
||||||
log.Infof("[DL] Postponing match %d (%s): GC not ready", demo.MatchId, demo.ShareCode)
|
log.Infof("[DL] Postponing match %d (%s): GC not ready", demo.MatchId, demo.ShareCode)
|
||||||
time.Sleep(5 * time.Second)
|
time.Sleep(5 * time.Second)
|
||||||
dml.unlockDemo(demo)
|
|
||||||
dml.parseDemo <- demo
|
dml.parseDemo <- demo
|
||||||
continue
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
matchId, _, _, err := DecodeSharecode(demo.ShareCode)
|
matchId, _, _, err := DecodeSharecode(demo.ShareCode)
|
||||||
if err != nil || matchId == 0 {
|
if err != nil || matchId == 0 {
|
||||||
log.Warningf("[DL] Can't parse match with sharecode %s: %v", demo.ShareCode, err)
|
return fmt.Errorf("error decoding sharecode %s: %w", demo.ShareCode, err)
|
||||||
dml.unlockDemo(demo)
|
|
||||||
continue
|
|
||||||
}
|
}
|
||||||
|
|
||||||
iMatch, err := dml.db.Match.Get(context.Background(), matchId)
|
iMatch, err := dml.db.Match.Get(context.Background(), matchId)
|
||||||
if err != nil && !ent.IsNotFound(err) {
|
if err != nil && !ent.IsNotFound(err) {
|
||||||
log.Errorf("[DL] Failure trying to lookup match %d in db: %v", matchId, err)
|
return fmt.Errorf("error looking up match: %w", err)
|
||||||
dml.unlockDemo(demo)
|
|
||||||
continue
|
|
||||||
} else if err == nil {
|
} else if err == nil {
|
||||||
if iMatch.DemoParsed == false && iMatch.Date.After(time.Now().UTC().AddDate(0, 0, -30)) {
|
if iMatch.DemoParsed == false && iMatch.Date.After(time.Now().UTC().AddDate(0, 0, -30)) {
|
||||||
log.Infof("[DL] Match %d is loaded, but not parsed. Try parsing.", iMatch.ID)
|
log.Infof("[DL] Match %d is loaded, but not parsed. Try parsing.", iMatch.ID)
|
||||||
@@ -404,15 +401,13 @@ func (dml *DemoMatchLoader) gcWorker(apiKey string, rl ratelimit.Limiter) {
|
|||||||
demo.DecryptionKey = iMatch.DecryptionKey
|
demo.DecryptionKey = iMatch.DecryptionKey
|
||||||
err := dml.dp.ParseDemo(demo)
|
err := dml.dp.ParseDemo(demo)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Warningf("[DL] Parsing demo from match %d failed: %v", demo.MatchId, err)
|
return fmt.Errorf("error parsing match %d: %w", demo.MatchId, err)
|
||||||
dml.unlockDemo(demo)
|
|
||||||
}
|
}
|
||||||
continue
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Infof("[DL] Skipped match %d: already loaded", matchId)
|
log.Infof("[DL] Skipped match %d: already loaded", matchId)
|
||||||
dml.unlockDemo(demo)
|
return nil
|
||||||
continue
|
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Infof("[DL] Requesting match %d from GC", matchId)
|
log.Infof("[DL] Requesting match %d from GC", matchId)
|
||||||
@@ -420,9 +415,7 @@ func (dml *DemoMatchLoader) gcWorker(apiKey string, rl ratelimit.Limiter) {
|
|||||||
|
|
||||||
matchDetails, err := dml.getMatchDetails(demo.ShareCode)
|
matchDetails, err := dml.getMatchDetails(demo.ShareCode)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Warningf("[DL] Failure to get match-details for %d from GC: %v", demo.MatchId, err)
|
return fmt.Errorf("error getting match-details for %d: %w", demo.MatchId, err)
|
||||||
dml.unlockDemo(demo)
|
|
||||||
continue
|
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Infof("[DL] Recieved matchdetails for match %d (%s)", matchId, time.Since(t))
|
log.Infof("[DL] Recieved matchdetails for match %d (%s)", matchId, time.Since(t))
|
||||||
@@ -431,14 +424,33 @@ func (dml *DemoMatchLoader) gcWorker(apiKey string, rl ratelimit.Limiter) {
|
|||||||
lastRound := matchZero.GetRoundstatsall()[len(matchZero.Roundstatsall)-1]
|
lastRound := matchZero.GetRoundstatsall()[len(matchZero.Roundstatsall)-1]
|
||||||
var players []*ent.Player
|
var players []*ent.Player
|
||||||
|
|
||||||
|
dml.parsePlayerMapL.Lock()
|
||||||
|
for _, accID := range lastRound.GetReservation().GetAccountIds() {
|
||||||
|
if dml.parsePlayerMap[accID] == nil {
|
||||||
|
dml.parsePlayerMap[accID] = new(sync.RWMutex)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
dml.parsePlayerMapL.Unlock()
|
||||||
|
|
||||||
|
dml.parsePlayerMapL.RLock()
|
||||||
for _, accountId := range lastRound.GetReservation().GetAccountIds() {
|
for _, accountId := range lastRound.GetReservation().GetAccountIds() {
|
||||||
|
dml.parsePlayerMap[accountId].Lock()
|
||||||
tPlayer, err := utils.Player(dml.db, AccountId2SteamId(accountId), apiKey, rl)
|
tPlayer, err := utils.Player(dml.db, AccountId2SteamId(accountId), apiKey, rl)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Warningf("[DL] Unable to get player for steamid %d: %v", AccountId2SteamId(accountId), err)
|
dml.parsePlayerMap[accountId].Unlock()
|
||||||
continue
|
dml.parsePlayerMapL.RUnlock()
|
||||||
|
return fmt.Errorf("error getting player for steamid %d: %w", AccountId2SteamId(accountId), err)
|
||||||
}
|
}
|
||||||
players = append(players, tPlayer)
|
players = append(players, tPlayer)
|
||||||
}
|
}
|
||||||
|
dml.parsePlayerMapL.RUnlock()
|
||||||
|
defer func() {
|
||||||
|
dml.parsePlayerMapL.RLock()
|
||||||
|
for _, accountId := range lastRound.GetReservation().GetAccountIds() {
|
||||||
|
dml.parsePlayerMap[accountId].Unlock()
|
||||||
|
}
|
||||||
|
dml.parsePlayerMapL.RUnlock()
|
||||||
|
}()
|
||||||
|
|
||||||
demo.Url = lastRound.GetMap()
|
demo.Url = lastRound.GetMap()
|
||||||
demo.MatchId = matchZero.GetMatchid()
|
demo.MatchId = matchZero.GetMatchid()
|
||||||
@@ -458,18 +470,13 @@ func (dml *DemoMatchLoader) gcWorker(apiKey string, rl ratelimit.Limiter) {
|
|||||||
SetDecryptionKey(demo.DecryptionKey).
|
SetDecryptionKey(demo.DecryptionKey).
|
||||||
Save(context.Background())
|
Save(context.Background())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Warningf("[DL] Unable to create match %d: %v", matchZero.GetMatchid(), err)
|
return fmt.Errorf("error creating match %d: %w", matchZero.GetMatchid(), err)
|
||||||
dml.unlockDemo(demo)
|
|
||||||
continue
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for i, mPlayer := range players {
|
for i, mPlayer := range players {
|
||||||
var (
|
var (
|
||||||
teamId int
|
teamId int
|
||||||
mk2 uint
|
mk2, mk3, mk4, mk5 uint
|
||||||
mk3 uint
|
|
||||||
mk4 uint
|
|
||||||
mk5 uint
|
|
||||||
)
|
)
|
||||||
|
|
||||||
if i > 4 {
|
if i > 4 {
|
||||||
@@ -514,7 +521,7 @@ func (dml *DemoMatchLoader) gcWorker(apiKey string, rl ratelimit.Limiter) {
|
|||||||
SetMk5(mk5).
|
SetMk5(mk5).
|
||||||
Exec(context.Background())
|
Exec(context.Background())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Warningf("[DL] Unable to create stats for player %d in match %d: %v", mPlayer.ID, tMatch.ID, err)
|
return fmt.Errorf("error creating stats for player %d in match %d: %w", mPlayer.ID, tMatch.ID, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -522,24 +529,31 @@ func (dml *DemoMatchLoader) gcWorker(apiKey string, rl ratelimit.Limiter) {
|
|||||||
for _, p := range players {
|
for _, p := range players {
|
||||||
err = dml.cache.Delete(context.Background(), fmt.Sprintf(utils.SideMetaCacheKey, p.ID))
|
err = dml.cache.Delete(context.Background(), fmt.Sprintf(utils.SideMetaCacheKey, p.ID))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Warningf("[DL] Unable to delete cache key %s: %v", fmt.Sprintf(utils.SideMetaCacheKey, p.ID), err)
|
return fmt.Errorf("error deleting cache key %s: %w", fmt.Sprintf(utils.SideMetaCacheKey, p.ID), err)
|
||||||
}
|
}
|
||||||
|
|
||||||
w, l, t, err := utils.GetWinLossTieForPlayer(p)
|
w, l, t, err := utils.GetWinLossTieForPlayer(p)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Warningf("[DL] Failure to calculate WinLossTie for player %d: %v", p.ID, err)
|
return fmt.Errorf("error calculating WinLossTie for player %d: %w", p.ID, err)
|
||||||
continue
|
|
||||||
}
|
}
|
||||||
err = p.Update().SetWins(w).SetTies(t).SetLooses(l).Exec(context.Background())
|
err = p.Update().SetWins(w).SetTies(t).SetLooses(l).Exec(context.Background())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Warningf("[DL] Failure to save WinLossTie for player %d: %v", p.ID, err)
|
return fmt.Errorf("error saving WinLossTie for player %d: %w", p.ID, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
err = dml.dp.ParseDemo(demo)
|
err = dml.dp.ParseDemo(demo)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Warningf("[DL] Can't queue demo %d for parsing: %v", demo.MatchId, err)
|
return fmt.Errorf("error queueing demo %d for parsing: %w", demo.MatchId, err)
|
||||||
dml.unlockDemo(demo)
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (dml *DemoMatchLoader) gcWorker(apiKey string, rl ratelimit.Limiter) {
|
||||||
|
for demo := range dml.parseDemo {
|
||||||
|
err := dml.handleDemo(demo, apiKey, rl)
|
||||||
|
if err != nil {
|
||||||
|
log.Warningf("[DL] Error handling demo: %v", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user