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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#![warn(missing_docs)]
use std::collections::HashMap;
use std::ops::Deref;
use async_trait::async_trait;
use futures::FutureExt;
use log::{info, warn};
use tokio::spawn;
use tokio_postgres::{
tls::MakeTlsConnect, tls::TlsConnect, Client as PgClient, Config as PgConfig, Error, Socket,
Statement, Transaction as PgTransaction,
};
pub type Pool = deadpool::Pool<Client, tokio_postgres::Error>;
pub struct Manager<T: MakeTlsConnect<Socket>> {
config: PgConfig,
tls: T,
}
impl<T: MakeTlsConnect<Socket>> Manager<T> {
pub fn new(config: PgConfig, tls: T) -> Manager<T> {
Manager {
config: config,
tls: tls,
}
}
}
#[async_trait]
impl<T> deadpool::Manager<Client, Error> for Manager<T>
where
T: MakeTlsConnect<Socket> + Clone + Sync + Send + 'static,
T::Stream: Sync + Send,
T::TlsConnect: Sync + Send,
<T::TlsConnect as TlsConnect<Socket>>::Future: Send,
{
async fn create(&self) -> Result<Client, Error> {
let (client, connection) = self.config.connect(self.tls.clone()).await?;
let connection = connection.map(|r| {
if let Err(e) = r {
warn!(target: "deadpool.postgres", "Connection error: {}", e);
}
});
spawn(connection);
Ok(Client::new(client))
}
async fn recycle(&self, client: &mut Client) -> Result<(), Error> {
match client.simple_query("").await {
Ok(_) => Ok(()),
Err(e) => {
info!(target: "deadpool.postgres", "Connection could not be recycled: {}", e);
Err(e)
}
}
}
}
pub struct StatementCache {
map: HashMap<String, Statement>,
}
impl StatementCache {
fn new() -> StatementCache {
StatementCache {
map: HashMap::new()
}
}
pub fn size(&self) -> usize {
self.map.len()
}
pub fn clear(&mut self) {
self.map.clear()
}
}
pub struct Client {
client: PgClient,
pub statement_cache: StatementCache,
}
impl Client {
pub fn new(client: PgClient) -> Client {
Client {
client: client,
statement_cache: StatementCache::new(),
}
}
pub async fn prepare(&mut self, query: &str) -> Result<Statement, Error> {
let query_owned = query.to_owned();
match self.statement_cache.map.get(&query_owned) {
Some(statement) => Ok(statement.clone()),
None => {
let stmt = self.client.prepare(query).await?;
self.statement_cache.map
.insert(query_owned.clone(), stmt.clone());
Ok(stmt)
}
}
}
pub async fn transaction<'a>(&'a mut self) -> Result<Transaction<'a>, Error> {
Ok(Transaction {
txn: PgClient::transaction(&mut self.client).await?,
statement_cache: &mut self.statement_cache,
})
}
}
impl Deref for Client {
type Target = PgClient;
fn deref(&self) -> &PgClient {
&self.client
}
}
pub struct Transaction<'a> {
txn: PgTransaction<'a>,
pub statement_cache: &'a mut StatementCache,
}
impl<'a> Transaction<'a> {
pub async fn prepare(&mut self, query: &str) -> Result<Statement, Error> {
let query_owned = query.to_owned();
match self.statement_cache.map.get(&query_owned) {
Some(statement) => Ok(statement.clone()),
None => {
let stmt = self.txn.prepare(query).await?;
self.statement_cache.map
.insert(query_owned.clone(), stmt.clone());
Ok(stmt)
}
}
}
pub async fn commit(self) -> Result<(), Error> {
self.txn.commit().await
}
pub async fn rollback(self) -> Result<(), Error> {
self.txn.rollback().await
}
}
impl<'a> Deref for Transaction<'a> {
type Target = PgTransaction<'a>;
fn deref(&self) -> &PgTransaction<'a> {
&self.txn
}
}