stop simulation run loop when shutting down server

master
Jordan Orelli 4 years ago
parent 0c812ce97b
commit 4496e97515

@ -135,35 +135,45 @@ func (s *Server) Shutdown() {
s.Info("starting shutdown procedure")
var wg sync.WaitGroup
wg.Add(2)
wg.Add(3)
go func() {
defer wg.Done()
s.Info("shutting down http server")
if err := s.world.Stop(); err != nil {
s.Error("error stopping the simulation: %v", err)
}
}()
go func() {
defer wg.Done()
log := s.Child("http")
log.Info("shutting down http server")
if err := s.http.Shutdown(context.Background()); err != nil {
s.Error("error shutting down http server: %v", err)
log.Error("error shutting down http server: %v", err)
} else {
s.Info("http server has shut down")
log.Info("http server has shut down")
}
}()
go func() {
defer wg.Done()
log := s.Child("sessions")
s.Lock()
if len(s.sessions) > 0 {
s.Info("broadcasting shutdown to %d active sessions", len(s.sessions))
log.Info("broadcasting shutdown to %d active sessions", len(s.sessions))
for id, sn := range s.sessions {
s.Info("sending done signal to session: %d", id)
log.Info("sending done signal to session: %d", id)
sn.done <- true
}
} else {
s.Info("no active sessions")
log.Info("no active sessions")
}
s.Unlock()
s.Info("waiting on connected sessions to shut down")
log.Info("waiting on connected sessions to shut down")
s.waitOnSessions.Wait()
s.Info("all sessions have shut down")
log.Info("all sessions have shut down")
}()
wg.Wait()
s.Info("shutdown procedure complete")

@ -10,6 +10,7 @@ import (
type World struct {
*blammo.Log
rooms []room
done chan bool
}
func NewWorld(log *blammo.Log) *World {
@ -22,10 +23,13 @@ func NewWorld(log *blammo.Log) *World {
height: 10,
},
},
done: make(chan bool),
}
}
func (w *World) Run(hz int) {
defer w.Info("simulation has exited run loop")
period := time.Second / time.Duration(hz)
w.Info("starting world with a tick rate of %dhz, frame duration of %v", hz, period)
ticker := time.NewTicker(period)
@ -35,8 +39,16 @@ func (w *World) Run(hz int) {
case <-ticker.C:
w.tick(time.Since(lastTick))
lastTick = time.Now()
case <-w.done:
return
}
}
}
func (w *World) Stop() error {
w.Info("stopping simulation")
w.done <- true
return nil
}
func (w *World) tick(d time.Duration) {

Loading…
Cancel
Save