So in this repository , I have initialized my aws dynamo db Client using the lazy_static evaluation so that I don't need to reinitialize it everytime it is being call.
And this static value is being called at several locations in my code for DB operations
One problem I see here is if the connection is lost due to some reason .. therefore it is never reinitialized again
is there anything you guys see here which I can improve?
dpc
August 4, 2022, 1:44am
2
Get rid of global values and don't use lazy_static
.
1 Like
dpc
August 4, 2022, 1:45am
3
use crate::auth::{register_user, verification};
#[tokio::main]
async fn main() {
tracing_subscriber::fmt::init();
let app = Router::new()
.route("/register", get(register_user))
.route("/verify", get(verification));
#[cfg(debug_assertions)]
{
axum::Server::bind(&"0.0.0.0:3000".parse().unwrap())
.http2_enable_connect_protocol()
.serve(app.into_make_service())
.await
.unwrap();
}
// If we compile in release mode, use the Lambda Runtime
#[cfg(not(debug_assertions))]
Debug vs compile mode is not a replacement for command line arguments.
dpc
August 4, 2022, 1:48am
4
Another lazy static rust-TOTP-server/obj.rs at 13a357fc9dbd4f51bbab7d27e80f2c9d06c0bdcf · artech-git/rust-TOTP-server · GitHub
Generally, a proper program should take all the input from env/command line very early, parse it, validate it, put it in some struct Opts
or something. and then pass it around where it needed. axum - Rust
Using global variables for everything makes the whole thing completely uncomposable, hard to test and debug.
use std::collections::HashMap;
use config::Config;
use once_cell::sync::Lazy;
use regex::Regex;
use serde::{Deserialize, Serialize};
use crate::eval_constants::get_totp_size_value;
//==================================================================================================================
pub const FILE_PATH: &str = "./settings.toml";
pub static KEY_MAP: Lazy<HashMap<String, String>> = Lazy::new(|| {
let settings = match Config::builder()
// Add in `./Settings.toml`
.add_source(config::File::with_name(FILE_PATH))
// Add in settings from the environment (with a prefix of APP)
// Eg.. `APP_DEBUG=1 ./target/app` would set the `debug` key
.add_source(config::Environment::with_prefix("APP"))
These comment-lines are unnecessary and not idiomatic.
}
};
let hm = match settings.try_deserialize::<HashMap<String, String>>() {
Ok(v) => {
tracing::log::info!("deserialization succesfull");
v
}
Err(e) => {
tracing::log::error!("error deserialization of values: {}", e);
panic!();
}
};
return hm;
});
//==================================================================================================================
#[derive(Debug, Serialize, Deserialize)]
pub struct VerifyUser {
pub email: String,
Thanks to using global statics, this code can't handle external input properly, and needs to panic.
use crate::obj::FILE_PATH;
use crate::obj::KEY_MAP;
pub fn get_key_size_value() -> usize {
lazy_static! {
pub static ref VALUE: String = match KEY_MAP.get("KEY_SIZE") {
Some(v) => v.to_owned(),
None => {
tracing::log::error!("KEY_SIZE value not present in the : {}", FILE_PATH);
panic!();
}
};
pub static ref KEY_SIZE: usize = match VALUE.parse::<usize>() {
Ok(v) => v,
Err(e) => {
use crate::obj::FILE_PATH;
use crate::obj::KEY_MAP;
pub fn get_key_size_value() -> usize {
lazy_static! {
pub static ref VALUE: String = match KEY_MAP.get("KEY_SIZE") {
Some(v) => v.to_owned(),
None => {
tracing::log::error!("KEY_SIZE value not present in the : {}", FILE_PATH);
panic!();
}
};
pub static ref KEY_SIZE: usize = match VALUE.parse::<usize>() {
Ok(v) => v,
Err(e) => {
Oh dear. There's barely any code here that is not in a lazy_static.
Thanks for your valuable input
but one thing I was not confident in regarding what exactly is wrong if the program panics!
can you please elaborate more to it
}
};
let hm = match settings.try_deserialize::<HashMap<String, String>>() {
Ok(v) => {
tracing::log::info!("deserialization succesfull");
v
}
Err(e) => {
tracing::log::error!("error deserialization of values: {}", e);
panic!();
}
};
return hm;
});
//==================================================================================================================
#[derive(Debug, Serialize, Deserialize)]
pub struct VerifyUser {
pub email: String,
one thing I often become indecisive about is what should exactly be inserted in lazy_static evaluation
use crate::obj::FILE_PATH;
use crate::obj::KEY_MAP;
pub fn get_key_size_value() -> usize {
lazy_static! {
pub static ref VALUE: String = match KEY_MAP.get("KEY_SIZE") {
Some(v) => v.to_owned(),
None => {
tracing::log::error!("KEY_SIZE value not present in the : {}", FILE_PATH);
panic!();
}
};
pub static ref KEY_SIZE: usize = match VALUE.parse::<usize>() {
Ok(v) => v,
Err(e) => {
system
Closed
November 2, 2022, 5:50am
6
This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.