Global State #407
-
Is there a way I can manage global state in salvo? I have different types of connection pools objects that I'm initializing at the start of the app in main.rs. Is there a place where I could save it as a global state which can then be referenced later by other parts of the code? Ideally, I would like to assign each of them a name (String) so that I can reference it later by name. |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 1 reply
-
You can use https://github.com/salvo-rs/salvo/blob/main/examples/affix/src/main.rs |
Beta Was this translation helpful? Give feedback.
-
I'm facing an issue when creating an affix list dynamically but I keep getting an error the trait bound`AffixList: Clone is not satisfied. This maybe a general rust issue on how I'm initializing and may not necessarily be an salvo issue. Here is what I'm doing to generate the list dynamically.
Anyway to get around this issue for AffixList clone? |
Beta Was this translation helpful? Give feedback.
-
I don't think put db connections in affix is a good idea. Affix dropped after the request process finished. If you put db connections in affix, if the request process spend much time, The connections will opening until request process finished.
You can db pool and just get a connection from a static fn when you need it.
From: ***@***.***>
Date: Thu, Sep 7, 2023, 04:55
Subject: Re: [salvo-rs/salvo] Global State (Discussion #407)
To: ***@***.***>
Cc: "Chrislearn ***@***.***>, ***@***.***>
I'm facing an issue when creating an affix list dynamically but I keep getting an error the trait bound`AffixList: Clone is not satisfied. This maybe a general rust issue on how I'm initializing and may not necessarily be an salvo issue.
Here is what I'm doing to generate the list dynamically.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Datasource {
name: String,
connector: ConnectorType,
options: HashMap<String, Value>
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum ConnectorType {
PostgresNoTls,
PostgresTls,
RedisTls,
RedisNoTls
}
pub async fn get_affix_list_for_datasources(datasources: Vec<Datasource>) -> AffixList {
return datasources.iter().fold(AffixList::new(), |acc, &ds| {
acc.insert(&ds.get_name(), get_affix_list_for_datasources(datasources).await) // <---- getting error here for Clone bound not satisfied
});
}
pub async fn get_affix_list_for_datasources(datasources: Vec<Datasource>) -> AffixList {
let mut affix_list = AffixList::new();
for ds in datasources {
match ds.get_connector_type() {
PostgresNoTls => {
affix_list = affix_list.insert(ds.get_name(), open_postgres_no_tls_pool(ds).await);
}
RedisTls => println!("redis tls"),
_ => println!("not found"),
};
}
return affix_list;
}
pub async fn open_postgres_no_tls_pool(
ds: Datasource,
) -> Pool<PostgresConnectionManager<tokio_postgres::NoTls>> {
// return pg pool
}
Anyway to get around this issue for AffixList clone?
… —
Reply to this email directly, view it on GitHub<#407 (comment)>, or unsubscribe<https://github.com/notifications/unsubscribe-auth/ABM2JMHRKCO6XRP5LQWVF4DXZDPJLANCNFSM6AAAAAA4MGTNPU>.
You are receiving this because you commented.[image: https://github.com/notifications/beacon/ABM2JMD7M2E3D474IZ635ZTXZDPJLA5CNFSM6AAAAAA4MGTNPWWGG33NNVSW45C7OR4XAZNRIRUXGY3VONZWS33OINXW23LFNZ2KUY3PNVWWK3TUL5UWJTQANG7XU.gif]Message ID: ***@***.***>
|
Beta Was this translation helpful? Give feedback.
-
The connections will be dynamic meaning it will be defined in a config file. I could have N number of connection pools of different types. from what I understand, if I use something like OnceCell, I have to predefine the module/functions. In that case, I would need to create static classes for each type and have some sort of hash map to manage |
Beta Was this translation helpful? Give feedback.
You can use
Affix
middleware.https://github.com/salvo-rs/salvo/blob/main/examples/affix/src/main.rs