-
How can I reuse the client, i.e. reuse the underlying connection Trying to save the Client in AppState and use it so I don't have to create it again each time. It doesn't work in my function. Want without sqlx. How to do it correctly?
` |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 7 replies
-
You didn't provide any error messages or what specifically you have issues with so there isn't much help we can provide. |
Beta Was this translation helpful? Give feedback.
-
What is the issue if you save your client in #[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// POSTGRESQL - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
let (client, connection) = tokio_postgres::connect("host=localhost ***", NoTls).await?;
tokio::spawn(async move {
if let Err(e) = connection.await {
eprintln!("connection error: {}", e);
}
});
// creates the client
let created_client = &client.query("insert into test (inf, res) values ($1,$2)", &[&"txt", &100]).await?;
//
let state = AppState {
d: Arc::new(Mutex::new(vec![])),
client: client,
created_client: created_client, // or Arc::new(created_client)
};
//
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// AXUM - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// ...
let app = Router::new()
.route("/info", post(create_user))
.with_state(state);
let listener = tokio::net::TcpListener::bind("127.0.0.1:3000").await.unwrap();
axum::serve(listener, app).await.unwrap();
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// ...
Ok(())
}
async fn create_user(
State(state): State<AppState>,
Json(json): Json<Value>,
) -> Json<Value> {
let mut s: String = "".to_string();
// ...
let row = state.created_client;
// ...
// println!("s = {:?}", &s);
Json(serde_json::from_str::<serde_json::Value>(&s).expect("REASON"))
}
#[derive(Debug, Clone)]
struct AppState {
d: Arc<Mutex<Vec<String>>>, // вектор строк
// client: reqwest::Client,
client: tokio_postgres::Client,
created_client: <type of your created client>
} |
Beta Was this translation helpful? Give feedback.
-
What is the issue if you save your client in #[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// POSTGRESQL - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
let (client, connection) = tokio_postgres::connect("host=localhost ***", NoTls).await?;
tokio::spawn(async move {
if let Err(e) = connection.await {
eprintln!("connection error: {}", e);
}
});
// creates the client
let created_client = &client.query("insert into test (inf, res) values ($1,$2)", &[&"txt", &100]).await?;
//
let state = AppState {
d: Arc::new(Mutex::new(vec![])),
client: client,
created_client: created_client, // or Arc::new(created_client)
};
//
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// AXUM - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// ...
let app = Router::new()
.route("/info", post(create_user))
.with_state(state);
let listener = tokio::net::TcpListener::bind("127.0.0.1:3000").await.unwrap();
axum::serve(listener, app).await.unwrap();
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// ...
Ok(())
}
async fn create_user(
State(state): State<AppState>,
Json(json): Json<Value>,
) -> Json<Value> {
let mut s: String = "".to_string();
// ...
let row = state.created_client;
// ...
// println!("s = {:?}", &s);
Json(serde_json::from_str::<serde_json::Value>(&s).expect("REASON"))
}
#[derive(Debug, Clone)]
struct AppState {
d: Arc<Mutex<Vec<String>>>, // вектор строк
// client: reqwest::Client,
client: tokio_postgres::Client,
created_client: <type of your created client>
} |
Beta Was this translation helpful? Give feedback.
yes, you can try to put it into an
Arc
.