|
1 | 1 | #[macro_use] |
2 | 2 | extern crate log; |
3 | 3 |
|
4 | | -use postgres::NoTls; |
5 | | -use r2d2_postgres::{ |
6 | | - r2d2::{self, Pool}, |
7 | | - PostgresConnectionManager, |
8 | | -}; |
9 | 4 | use rsocket_rust::{error::RSocketError, prelude::*}; |
10 | 5 | use rsocket_rust_transport_tcp::TcpServerTransport; |
11 | 6 | use std::error::Error; |
| 7 | +use std::sync::Arc; |
| 8 | +use tokio_postgres::{Client as PgClient, NoTls}; |
12 | 9 |
|
13 | 10 | #[tokio::main] |
14 | 11 | async fn main() -> Result<(), Box<dyn Error + Send + Sync>> { |
15 | 12 | env_logger::builder().format_timestamp_millis().init(); |
16 | | - let dao = Dao::try_new().expect("Connect failed!"); |
| 13 | + let dao = Dao::try_new().await?; |
17 | 14 | RSocketFactory::receive() |
18 | | - .acceptor(Box::new(move |_, _| { |
19 | | - info!("accept new socket!"); |
20 | | - Ok(Box::new(dao.clone())) |
21 | | - })) |
| 15 | + .acceptor(Box::new(move |_, _| Ok(Box::new(dao.clone())))) |
22 | 16 | .on_start(Box::new(|| info!("server start success!!!"))) |
23 | 17 | .transport(TcpServerTransport::from("127.0.0.1:7878")) |
24 | 18 | .serve() |
25 | 19 | .await |
26 | 20 | } |
27 | 21 |
|
28 | | -#[derive(Clone, Debug)] |
| 22 | +#[derive(Clone)] |
29 | 23 | struct Dao { |
30 | | - pool: Pool<PostgresConnectionManager<NoTls>>, |
| 24 | + client: Arc<PgClient>, |
31 | 25 | } |
32 | 26 |
|
33 | 27 | impl RSocket for Dao { |
34 | 28 | fn request_response(&self, _: Payload) -> Mono<Result<Payload, RSocketError>> { |
35 | | - let pool = self.pool.clone(); |
| 29 | + let client = self.client.clone(); |
36 | 30 | Box::pin(async move { |
37 | | - // TODO: something wrong here!!! |
38 | | - let mut client = pool.get().expect("Get client from pool failed!"); |
39 | 31 | let row = client |
40 | 32 | .query_one("SELECT 'world' AS hello", &[]) |
| 33 | + .await |
41 | 34 | .expect("Execute SQL failed!"); |
42 | 35 | let result: String = row.get("hello"); |
43 | 36 | Ok(Payload::builder().set_data_utf8(&result).build()) |
@@ -65,16 +58,22 @@ impl RSocket for Dao { |
65 | 58 | } |
66 | 59 |
|
67 | 60 | impl Dao { |
68 | | - fn try_new() -> Result<Dao, Box<dyn Error + Sync + Send>> { |
69 | | - let manager: PostgresConnectionManager<NoTls> = PostgresConnectionManager::new( |
70 | | - "host=localhost user=postgres password=postgres" |
71 | | - .parse() |
72 | | - .unwrap(), |
73 | | - NoTls, |
74 | | - ); |
75 | | - let pool: Pool<PostgresConnectionManager<NoTls>> = |
76 | | - r2d2::Pool::new(manager).expect("Create pool failed!"); |
| 61 | + async fn try_new() -> Result<Dao, Box<dyn Error + Sync + Send>> { |
| 62 | + let (client, connection) = |
| 63 | + tokio_postgres::connect("host=localhost user=postgres password=postgres", NoTls) |
| 64 | + .await?; |
| 65 | + |
| 66 | + // The connection object performs the actual communication with the database, |
| 67 | + // so spawn it off to run on its own. |
| 68 | + tokio::spawn(async move { |
| 69 | + if let Err(e) = connection.await { |
| 70 | + eprintln!("connection error: {}", e); |
| 71 | + } |
| 72 | + }); |
| 73 | + |
77 | 74 | info!("==> create postgres pool success!"); |
78 | | - Ok(Dao { pool }) |
| 75 | + Ok(Dao { |
| 76 | + client: Arc::new(client), |
| 77 | + }) |
79 | 78 | } |
80 | 79 | } |
0 commit comments