Skip to content

Commit 0b62182

Browse files
committed
feat: added pool for postgress, connection manager for redis
Signed-off-by: Martin <martin@hotmail.com.br>
1 parent 5dde6b1 commit 0b62182

File tree

2 files changed

+50
-19
lines changed

2 files changed

+50
-19
lines changed

src/application/mod.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
use redis::Client;
1+
pub mod adapters;
2+
3+
use deadpool_postgres::Pool;
24
use std::sync::Arc;
35

46
#[derive(Copy, Clone, Debug)]
@@ -12,7 +14,8 @@ impl AccountService {
1214
}
1315
}
1416

15-
#[derive(Debug, Clone)]
17+
#[derive(Clone)]
1618
pub struct ServerData {
17-
pub redis: Arc<Client>,
19+
pub re_conn: redis::aio::ConnectionManager,
20+
pub pg_pool: Pool,
1821
}

src/bin/main.rs

Lines changed: 44 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,54 @@
1+
use deadpool_postgres::{ManagerConfig, Pool, RecyclingMethod, Runtime};
2+
use listenfd::ListenFd;
13
use redis::Client;
24
use rinha_de_backend::application::ServerData;
35
use rinha_de_backend::infrastructure::server_impl::server::{match_routes, parse_http};
4-
use std::sync::Arc;
56
use tokio::io::{AsyncReadExt, AsyncWriteExt};
6-
use tokio::net::TcpListener;
7+
use tokio::net::{TcpListener, UnixListener};
8+
use tokio_postgres::NoTls;
79

810
#[tokio::main]
911
async fn main() {
1012
run().await
1113
}
1214

15+
async fn setup_pgsql() -> Pool {
16+
let mut cfg = deadpool_postgres::Config::new();
17+
cfg.dbname = Some("rinhabackend".to_string());
18+
cfg.host = Some("localhost".to_string());
19+
cfg.user = Some("postgres".to_string());
20+
// cfg.password = "inha";
21+
cfg.manager = Some(ManagerConfig {
22+
recycling_method: RecyclingMethod::Fast,
23+
});
24+
25+
cfg.create_pool(Some(Runtime::Tokio1), NoTls).unwrap()
26+
}
27+
1328
async fn run() {
14-
let socket = TcpListener::bind("0.0.0.0:1337").await.unwrap();
15-
let redis = Client::open("redis://0.0.0.0:1000").unwrap();
29+
let mut listenfd = ListenFd::from_env();
30+
let socket = if let Some(listener) = listenfd.take_tcp_listener(0).unwrap() {
31+
listener.set_nonblocking(true).unwrap();
32+
// UnixListener::from_std(listener).unwrap()
33+
TcpListener::from_std(listener).unwrap()
34+
} else {
35+
TcpListener::bind("localhost:1337").await.unwrap()
36+
};
37+
38+
let re_conn =
39+
redis::aio::ConnectionManager::new(Client::open("redis://localhost:6379").unwrap())
40+
.await
41+
.unwrap();
42+
43+
let pg_pool = setup_pgsql().await;
44+
1645
let data = ServerData {
17-
redis: Arc::new(redis),
46+
re_conn: re_conn,
47+
pg_pool: pg_pool,
1848
};
1949

50+
println!("Server is running!");
51+
2052
loop {
2153
let (mut socket, other) = socket.accept().await.unwrap();
2254

@@ -38,18 +70,14 @@ async fn run() {
3870
return;
3971
}
4072

41-
eprintln!("one request, read bytes: {}", read_bytes);
42-
4373
let request = parse_http(&buf).unwrap();
44-
let response = match_routes(&data, request);
45-
46-
socket
47-
.write_all(&response.left().unwrap().into_http())
48-
.await
49-
.unwrap();
50-
51-
// shutdown connections immediately since gatling doesn't have keep-alive connection by default
52-
// socket.shutdown().await.unwrap();
74+
let response = match_routes(&data, request).await;
75+
let response = if response.is_left() {
76+
response.unwrap_left()
77+
} else {
78+
response.unwrap_right()
79+
};
80+
socket.write_all(&response.into_http()).await.unwrap();
5381
}
5482
});
5583
}

0 commit comments

Comments
 (0)