-
Notifications
You must be signed in to change notification settings - Fork 176
Expand file tree
/
Copy pathsetup.rs
More file actions
124 lines (108 loc) · 3 KB
/
setup.rs
File metadata and controls
124 lines (108 loc) · 3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
use sqlx::{Executor, Postgres, Row, pool::Pool, postgres::PgPoolOptions};
use tokio_postgres::*;
pub async fn connections_tokio() -> Vec<Client> {
let mut results = vec![];
for db in ["pgdog", "pgdog_sharded"] {
let (client, connection) = tokio_postgres::connect(
&format!(
"host=127.0.0.1 user=pgdog dbname={} password=pgdog port=6432 options=-c%20search_path%3D$user,public%20-capplication_name%3Dtokio",
db
),
NoTls,
)
.await
.unwrap();
tokio::spawn(async move {
if let Err(e) = connection.await {
eprintln!("connection error: {}", e);
}
});
results.push(client);
}
results
}
pub async fn connections_sqlx() -> Vec<Pool<Postgres>> {
let mut pools = vec![];
for db in ["pgdog", "pgdog_sharded"] {
let pool = PgPoolOptions::new()
.max_connections(1)
.connect(&format!(
"postgres://pgdog:pgdog@127.0.0.1:6432/{}?application_name=sqlx",
db
))
.await
.unwrap();
pools.push(pool);
}
pools
}
pub async fn connection_sqlx_direct() -> Pool<Postgres> {
connection_sqlx_direct_db("pgdog").await
}
pub async fn connection_sqlx_direct_db(name: &str) -> Pool<Postgres> {
PgPoolOptions::new()
.max_connections(1)
.connect(
format!(
"postgres://pgdog:pgdog@127.0.0.1:5432/{}?application_name=sqlx_direct",
name
)
.as_str(),
)
.await
.unwrap()
}
#[derive(Debug, PartialEq, Clone)]
pub struct Backend {
pub pid: i32,
pub backend_start: String,
}
pub async fn backends(application_name: &str, pool: &Pool<Postgres>) -> Vec<Backend> {
pool.fetch_all(
format!(
"SELECT pid::INTEGER,
backend_start::TEXT
FROM pg_stat_activity
WHERE application_name = '{}'
ORDER BY backend_start",
application_name
)
.as_str(),
)
.await
.unwrap()
.into_iter()
.map(|r| Backend {
pid: r.get(0),
backend_start: r.get(1),
})
.collect()
}
pub async fn connection_failover() -> Pool<Postgres> {
PgPoolOptions::new()
.max_connections(5)
.connect("postgres://pgdog:pgdog@127.0.0.1:6432/failover?application_name=sqlx")
.await
.unwrap()
}
pub async fn admin_tokio() -> Client {
let (client, connection) = tokio_postgres::connect(
"host=127.0.0.1 user=admin dbname=admin password=pgdog port=6432",
NoTls,
)
.await
.unwrap();
tokio::spawn(async move {
if let Err(e) = connection.await {
eprintln!("connection error: {}", e);
}
});
client
}
pub async fn admin_sqlx() -> Pool<Postgres> {
PgPoolOptions::new()
.max_connections(1)
.connect("postgres://admin:pgdog@127.0.0.1:6432/admin")
.await
.unwrap()
}