From f5eac37b06a542f7ef359241c0ed1e28dd31a642 Mon Sep 17 00:00:00 2001 From: Steve Date: Mon, 27 Sep 2021 16:51:40 -0400 Subject: [PATCH] clean out stale matches --- internal/coordinator/match.go | 7 +++++++ internal/coordinator/session.go | 6 ++++++ 2 files changed, 13 insertions(+) diff --git a/internal/coordinator/match.go b/internal/coordinator/match.go index f0cbf6e..5bc49f7 100644 --- a/internal/coordinator/match.go +++ b/internal/coordinator/match.go @@ -60,6 +60,13 @@ func MatchCleaner(c *Coordinator) { delete(c.Matches, v.ID) v.Watcher <- true } + if time.Now().After(v.LastMove.Add(time.Minute * 10)) { + log.Println("clearing stale match") + v.Game = nil + v.Active = false + delete(c.Matches, v.ID) + v.Watcher <- true + } } c.MatchLock.Unlock() } diff --git a/internal/coordinator/session.go b/internal/coordinator/session.go index 01c6977..3d34372 100644 --- a/internal/coordinator/session.go +++ b/internal/coordinator/session.go @@ -2,6 +2,7 @@ package coordinator import ( "fmt" + "time" "git.saintnet.tech/stryan/snengame/internal/game" "github.com/google/uuid" @@ -49,6 +50,7 @@ type Session struct { Game *game.Game Watcher chan bool Broadcasts map[uuid.UUID]chan SessionResp + LastMove time.Time } func NewSession() *Session { @@ -61,6 +63,7 @@ func NewSession() *Session { Game: nil, Watcher: make(chan bool), Broadcasts: make(map[uuid.UUID]chan SessionResp), + LastMove: time.Now(), } } @@ -102,6 +105,9 @@ func (s *Session) Play(id uuid.UUID, cmd *game.Command) *game.CommandResult { if s.pMap[id] != cmd.PlayerID { return nil } + if cmd.Type != SessionCmdPoll { + s.LastMove = time.Now() + } res := s.Game.Parse(cmd) return res }