Skip to content

Commit

Permalink
refactor: remove extra game stopping logic
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobtread committed Oct 13, 2024
1 parent bcdd1bc commit 04c1632
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 45 deletions.
42 changes: 3 additions & 39 deletions src/services/game/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ use crate::{
},
};
use chrono::Utc;
use log::{debug, warn};
use log::debug;
use std::{
collections::VecDeque,
sync::{
atomic::{AtomicU32, Ordering},
Arc,
},
time::{Duration, SystemTime},
time::SystemTime,
};
use tokio::{
sync::{Mutex, RwLock},
Expand Down Expand Up @@ -61,9 +61,6 @@ struct MatchmakingEntry {
const DEFAULT_FIT: u16 = 21600;

impl GameManager {
/// Max number of times to poll a game for shutdown before erroring
const MAX_RELEASE_ATTEMPTS: u8 = 20;

/// Starts a new game manager service returning its link
pub fn new(
tunnel_service: Arc<TunnelService>,
Expand Down Expand Up @@ -269,40 +266,7 @@ impl GameManager {

pub async fn remove_game(&self, game_id: GameID) {
let games = &mut *self.games.write().await;
if let Some(mut game) = games.remove(&game_id) {
let mut attempt: u8 = 1;

// Attempt to obtain the owned game
let game = loop {
if attempt > Self::MAX_RELEASE_ATTEMPTS {
let references = Arc::strong_count(&game);
warn!(
"Failed to stop game {} there are still {} references to it",
game_id, references
);
return;
}

match Arc::try_unwrap(game) {
Ok(value) => break value,
Err(arc) => {
let wait = 5 * attempt as u64;
let references = Arc::strong_count(&arc);
debug!(
"Game {} still has {} references to it, waiting {}s",
game_id, references, wait
);
tokio::time::sleep(Duration::from_secs(wait)).await;
game = arc;
attempt += 1;
continue;
}
}
};

let game = game.into_inner();
game.stopped();
}
_ = games.remove(&game_id);
}

pub async fn process_queue(&self, link: GameRef, game_id: GameID) {
Expand Down
12 changes: 6 additions & 6 deletions src/services/game/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -395,12 +395,6 @@ impl Game {
}
}

/// Called by the game manager service once this game has been stopped and
/// removed from the game list
fn stopped(self) {
debug!("Game is stopped (GID: {})", self.id);
}

fn stop(&mut self) {
// Mark the game as stopping
self.state = GameState::Destructing;
Expand Down Expand Up @@ -627,3 +621,9 @@ impl Game {
debug!("Finished host migration (GID: {})", self.id);
}
}

impl Drop for Game {
fn drop(&mut self) {
debug!("Game is stopped (GID: {})", self.id);
}
}

0 comments on commit 04c1632

Please sign in to comment.