mirror of
https://github.com/timvisee/lazymc.git
synced 2025-05-19 04:40:22 -07:00
Grab exclusive lock for RCON invocations to server
This commit is contained in:
parent
5ffc6ee911
commit
96d7fc9dec
@ -6,6 +6,7 @@ use futures::FutureExt;
|
|||||||
use minecraft_protocol::data::server_status::ServerStatus;
|
use minecraft_protocol::data::server_status::ServerStatus;
|
||||||
use tokio::process::Command;
|
use tokio::process::Command;
|
||||||
use tokio::sync::watch;
|
use tokio::sync::watch;
|
||||||
|
use tokio::sync::Semaphore;
|
||||||
use tokio::time;
|
use tokio::time;
|
||||||
|
|
||||||
use crate::config::Config;
|
use crate::config::Config;
|
||||||
@ -99,6 +100,10 @@ pub struct Server {
|
|||||||
/// Used as starting/stopping timeout.
|
/// Used as starting/stopping timeout.
|
||||||
kill_at: RwLock<Option<Instant>>,
|
kill_at: RwLock<Option<Instant>>,
|
||||||
|
|
||||||
|
/// Lock for exclusive RCON operations.
|
||||||
|
#[cfg(feature = "rcon")]
|
||||||
|
rcon_lock: Semaphore,
|
||||||
|
|
||||||
/// Last time server was stopped over RCON.
|
/// Last time server was stopped over RCON.
|
||||||
#[cfg(feature = "rcon")]
|
#[cfg(feature = "rcon")]
|
||||||
rcon_last_stop: Mutex<Option<Instant>>,
|
rcon_last_stop: Mutex<Option<Instant>>,
|
||||||
@ -342,6 +347,8 @@ impl Default for Server {
|
|||||||
keep_online_until: Default::default(),
|
keep_online_until: Default::default(),
|
||||||
kill_at: Default::default(),
|
kill_at: Default::default(),
|
||||||
#[cfg(feature = "rcon")]
|
#[cfg(feature = "rcon")]
|
||||||
|
rcon_lock: Semaphore::new(1),
|
||||||
|
#[cfg(feature = "rcon")]
|
||||||
rcon_last_stop: Default::default(),
|
rcon_last_stop: Default::default(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -425,6 +432,9 @@ async fn stop_server_rcon(config: &Config, server: &Server) -> bool {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Grab RCON lock
|
||||||
|
let rcon_lock = server.rcon_lock.acquire().await.unwrap();
|
||||||
|
|
||||||
// Ensure RCON has cooled down
|
// Ensure RCON has cooled down
|
||||||
let rcon_cooled_down = server
|
let rcon_cooled_down = server
|
||||||
.rcon_last_stop
|
.rcon_last_stop
|
||||||
@ -465,6 +475,8 @@ async fn stop_server_rcon(config: &Config, server: &Server) -> bool {
|
|||||||
.replace(Instant::now());
|
.replace(Instant::now());
|
||||||
server.update_state(State::Stopping, config);
|
server.update_state(State::Stopping, config);
|
||||||
|
|
||||||
|
drop(rcon_lock);
|
||||||
|
|
||||||
// Gracefully close connection
|
// Gracefully close connection
|
||||||
rcon.close().await;
|
rcon.close().await;
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user