diff --git a/broker/sse.go b/broker/sse.go index 468d100..6a49de9 100644 --- a/broker/sse.go +++ b/broker/sse.go @@ -63,8 +63,8 @@ func (broker *Broker) ServeHTTP(w http.ResponseWriter, r *http.Request) { broker.newClients <- messageChan defer func() { broker.closingClients <- messageChan }() ctx := r.Context() - // browser can close sse on its own - heartbeat := time.NewTicker(15 * time.Second) + // browser can close sse on its own; ping every 2s to prevent + heartbeat := time.NewTicker(2 * time.Second) defer heartbeat.Stop() for { select { diff --git a/crons/main.go b/crons/main.go index 2a601bf..abfcda6 100644 --- a/crons/main.go +++ b/crons/main.go @@ -57,6 +57,7 @@ func (cm *CronManager) CleanupRooms() { } return } + roomListChange := false for _, room := range rooms { players, err := cm.repo.PlayerListByRoom(ctx, room.ID) if err != nil { @@ -71,6 +72,7 @@ func (cm *CronManager) CleanupRooms() { if err := cm.repo.SettingsDeleteByRoomID(ctx, room.ID); err != nil { cm.log.Error("failed to delete settings for empty room", "room_id", room.ID, "err", err) } + roomListChange = true continue } creatorInRoom := false @@ -120,6 +122,7 @@ func (cm *CronManager) CleanupRooms() { if err := cm.repo.SettingsDeleteByRoomID(ctx, room.ID); err != nil { cm.log.Error("failed to delete settings for room", "room_id", room.ID, "reason", reason, "err", err) } + roomListChange = true // Move to the next room continue } @@ -127,9 +130,11 @@ func (cm *CronManager) CleanupRooms() { if err := tx.Commit(); err != nil { cm.log.Error("failed to commit transaction", "err", err) } - broker.Notifier.Notifier <- broker.NotificationEvent{ - EventName: models.NotifyRoomListUpdate, - Payload: "", + if roomListChange { + broker.Notifier.Notifier <- broker.NotificationEvent{ + EventName: models.NotifyRoomListUpdate, + Payload: "", + } } }