performance fix

This commit is contained in:
Shiroyasha 2024-06-16 10:54:59 +03:00
parent 703a2727e7
commit 393127570e
Signed by: shiroyashik
GPG key ID: E4953D3940D7860A
3 changed files with 36 additions and 19 deletions

View file

@ -50,9 +50,9 @@ authSystem = "elyby"
special = [0,0,0,1,0,0] # 6 special = [0,0,0,1,0,0] # 6
pride = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0] # 25 pride = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0] # 25
# Your nickname here # With advancedUsers you can set additional parameters
# [advancedUsers.your uuid here] # [advancedUsers.your uuid here]
# username = "Bot" # username = "Your_username_here"
# authSystem = "mojang" # can be: mojang, elyby, internal (cant be authenticated) # authSystem = "mojang" # can be: mojang, elyby, internal (cant be authenticated)
# special = [0,1,0,0,0,0] # and set badges what you want! :D # special = [0,1,0,0,0,0] # and set badges what you want! :D
# pride = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0] # pride = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]

View file

@ -3,7 +3,6 @@ use axum::{
extract::DefaultBodyLimit, middleware::from_extractor, routing::{delete, get, post, put}, Router extract::DefaultBodyLimit, middleware::from_extractor, routing::{delete, get, post, put}, Router
}; };
use dashmap::DashMap; use dashmap::DashMap;
use utils::collect_advanced_users;
use std::sync::Arc; use std::sync::Arc;
use tokio::sync::{broadcast, Mutex}; use tokio::sync::{broadcast, Mutex};
use tower_http::trace::TraceLayer; use tower_http::trace::TraceLayer;
@ -28,9 +27,11 @@ use profile as api_profile;
// Utils // Utils
mod utils; mod utils;
use utils::update_advanced_users;
// Config // Config
mod config; mod config;
use config::Config;
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct AppState { pub struct AppState {
@ -66,7 +67,7 @@ async fn main() -> Result<()> {
info!("The Sculptor v{}", SCULPTOR_VERSION); info!("The Sculptor v{}", SCULPTOR_VERSION);
// Config // Config
let config = Arc::new(Mutex::new(config::Config::parse(config_file.clone().into()))); let config = Arc::new(Mutex::new(Config::parse(config_file.clone().into())));
let listen = config.lock().await.listen.clone(); let listen = config.lock().await.listen.clone();
// State // State
@ -77,26 +78,20 @@ async fn main() -> Result<()> {
}; };
// Automatic update of configuration while the server is running // Automatic update of configuration while the server is running
let config_update = state.config.clone(); let config_update = Arc::clone(&state.config);
let user_manager = Arc::clone(&state.user_manager); let user_manager = Arc::clone(&state.user_manager);
update_advanced_users(&config_update.lock().await.advanced_users, &user_manager);
tokio::spawn(async move { tokio::spawn(async move {
loop { loop {
let new_config = config::Config::parse(config_file.clone().into()); tokio::time::sleep(std::time::Duration::from_secs(10)).await;
let new_config = Config::parse(config_file.clone().into());
let mut config = config_update.lock().await; let mut config = config_update.lock().await;
if new_config != *config { if new_config != *config {
info!("Server configuration modification detected!"); info!("Server configuration modification detected!");
*config = new_config; *config = new_config;
// let collected = collect_advanced_users(&config.advanced_users); update_advanced_users(&config.advanced_users, &user_manager);
// for (uuid, userinfo) in collected {
// user_manager.insert_user(uuid, userinfo);
// }
} }
let collected = collect_advanced_users(&config.advanced_users);
for (uuid, userinfo) in collected {
user_manager.insert_user(uuid, userinfo);
}
tokio::time::sleep(std::time::Duration::from_secs(10)).await;
} }
}); });

View file

@ -5,7 +5,7 @@ use rand::{distributions::Alphanumeric, thread_rng, Rng};
use ring::digest::{self, digest}; use ring::digest::{self, digest};
use uuid::Uuid; use uuid::Uuid;
use crate::auth::{AuthSystem, Userinfo}; use crate::auth::{AuthSystem, UManager, Userinfo};
// Core functions // Core functions
pub fn rand() -> [u8; 50] { pub fn rand() -> [u8; 50] {
@ -36,8 +36,25 @@ pub fn get_correct_array(value: &toml::Value) -> Vec<u8> {
.map(move |x| x.as_integer().unwrap() as u8) .map(move |x| x.as_integer().unwrap() as u8)
.collect() .collect()
} }
pub fn collect_advanced_users(value: &toml::Table) -> Vec<(Uuid, Userinfo)> { // pub fn collect_advanced_users(value: &toml::Table) -> Vec<(Uuid, Userinfo)> {
value // value
// .iter()
// .map( |(uuid, userdata)| {
// let auth_system = AuthSystem::from_str(userdata.as_table().unwrap().get("authSystem").expect("Can't find authSystem in advancedUser!").as_str().unwrap()).unwrap();
// let username = userdata.as_table().unwrap().get("username").expect("Can't find username in advancedUser!").as_str().unwrap().to_string();
// (
// Uuid::parse_str(uuid).unwrap(),
// Userinfo { username,
// uuid: Uuid::parse_str(uuid).unwrap(),
// auth_system,
// token: None
// }
// )})
// .collect()
// }
pub fn update_advanced_users(value: &toml::Table, umanager: &UManager) {
let users: Vec<(Uuid, Userinfo)> = value
.iter() .iter()
.map( |(uuid, userdata)| { .map( |(uuid, userdata)| {
let auth_system = AuthSystem::from_str(userdata.as_table().unwrap().get("authSystem").expect("Can't find authSystem in advancedUser!").as_str().unwrap()).unwrap(); let auth_system = AuthSystem::from_str(userdata.as_table().unwrap().get("authSystem").expect("Can't find authSystem in advancedUser!").as_str().unwrap()).unwrap();
@ -50,9 +67,14 @@ pub fn collect_advanced_users(value: &toml::Table) -> Vec<(Uuid, Userinfo)> {
token: None token: None
} }
)}) )})
.collect() .collect();
for (uuid, userinfo) in users {
umanager.insert_user(uuid, userinfo);
}
} }
pub fn format_uuid(uuid: &Uuid) -> String { pub fn format_uuid(uuid: &Uuid) -> String {
// let uuid = Uuid::parse_str(&uuid)?; TODO: Вероятно format_uuid стоит убрать // let uuid = Uuid::parse_str(&uuid)?; TODO: Вероятно format_uuid стоит убрать
// .map_err(|_| tide::Error::from_str(StatusCode::InternalServerError, "Failed to parse UUID"))?; // .map_err(|_| tide::Error::from_str(StatusCode::InternalServerError, "Failed to parse UUID"))?;