forked from lavina/lavina
1
0
Fork 0

fix graceful shutdown

This commit is contained in:
Nikita Vilunov 2023-10-02 23:35:23 +02:00
parent 9fca913430
commit 4621470bde
1 changed files with 3 additions and 2 deletions

View File

@ -155,6 +155,7 @@ enum PlayerCommand {
GetRooms(Promise<Vec<RoomInfo>>), GetRooms(Promise<Vec<RoomInfo>>),
/** Events from rooms */ /** Events from rooms */
Update(Updates), Update(Updates),
Stop,
} }
pub enum Cmd { pub enum Cmd {
@ -241,9 +242,8 @@ impl PlayerRegistry {
pub async fn shutdown_all(&mut self) -> Result<()> { pub async fn shutdown_all(&mut self) -> Result<()> {
let mut inner = self.0.write().unwrap(); let mut inner = self.0.write().unwrap();
let mut players = HashMap::new();
std::mem::swap(&mut players, &mut inner.players);
for (i, (k, j)) in inner.players.drain() { for (i, (k, j)) in inner.players.drain() {
k.send(PlayerCommand::Stop).await;
drop(k); drop(k);
j.await?; j.await?;
log::debug!("Player stopped #{i:?}") log::debug!("Player stopped #{i:?}")
@ -325,6 +325,7 @@ impl Player {
} }
} }
PlayerCommand::Cmd(cmd, connection_id) => self.handle_cmd(cmd, connection_id).await, PlayerCommand::Cmd(cmd, connection_id) => self.handle_cmd(cmd, connection_id).await,
PlayerCommand::Stop => break,
} }
} }
log::debug!("Shutting down player actor #{:?}", self.player_id); log::debug!("Shutting down player actor #{:?}", self.player_id);