Fix small race at shutdown, don't start prometheus if not prod

This commit is contained in:
zikaeroh 2020-05-27 00:58:08 -07:00
parent 577575a430
commit 9464868137
2 changed files with 13 additions and 5 deletions

View File

@ -239,15 +239,20 @@ func (r *Room) HandleConn(playerID uuid.UUID, nickname string, c *websocket.Conn
r.mu.Lock() r.mu.Lock()
r.players[playerID] = func(s protocol.ServerNote) { r.players[playerID] = func(s protocol.ServerNote) {
g.Go(func() error { if ctx.Err() != nil {
return
}
// It's not safe to start more group goroutines concurrently; just use a regular
// goroutine and hope that errors here will be reflected later via ping/receive failures.
go func() {
ctx, cancel := context.WithTimeout(ctx, time.Second) ctx, cancel := context.WithTimeout(ctx, time.Second)
defer cancel() defer cancel()
if err := wsjson.Write(ctx, c, &s); err != nil { if err := wsjson.Write(ctx, c, &s); err != nil {
return err return
} }
metricSent.Inc() metricSent.Inc()
return nil }()
})
} }
r.room.AddPlayer(playerID, nickname) r.room.AddPlayer(playerID, nickname)
r.sendAll() r.sendAll()

View File

@ -210,7 +210,10 @@ func main() {
}) })
runServer(ctx, g, args.Addr, r) runServer(ctx, g, args.Addr, r)
runServer(ctx, g, ":2112", prometheusHandler())
if args.Prod {
runServer(ctx, g, ":2112", prometheusHandler())
}
log.Fatal(g.Wait()) log.Fatal(g.Wait())
} }