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
use std::{future::Future, hash::Hash, io, net::ToSocketAddrs};
use monoio::{
io::{AsyncReadRent, AsyncWriteRent},
net::TcpStream,
};
use monoio_http::{h1::codec::ClientCodec, Param};
use super::pool::{ConnectionPool, PooledConnection};
pub type DefaultTcpConnector<T> = PooledConnector<TcpConnector, T, TcpStream>;
#[cfg(feature = "tls")]
pub type DefaultTlsConnector<T> =
PooledConnector<TlsConnector, T, monoio_rustls::ClientTlsStream<TcpStream>>;
pub trait Connector<K> {
type Connection;
type Error;
type ConnectionFuture<'a>: Future<Output = Result<Self::Connection, Self::Error>>
where
Self: 'a;
fn connect(&self, key: K) -> Self::ConnectionFuture<'_>;
}
#[derive(Default, Clone, Debug)]
pub struct TcpConnector;
impl<T> Connector<T> for TcpConnector
where
T: ToSocketAddrs,
{
type Connection = TcpStream;
type Error = io::Error;
type ConnectionFuture<'a> = impl Future<Output = Result<Self::Connection, Self::Error>>;
fn connect(&self, key: T) -> Self::ConnectionFuture<'_> {
async move { TcpStream::connect(key).await }
}
}
#[cfg(feature = "tls")]
#[derive(Clone)]
pub struct TlsConnector<C = TcpConnector> {
tcp_connector: C,
tls_connector: monoio_rustls::TlsConnector,
}
#[cfg(feature = "tls")]
impl Default for TlsConnector {
fn default() -> Self {
let mut root_store = rustls::RootCertStore::empty();
root_store.add_server_trust_anchors(webpki_roots::TLS_SERVER_ROOTS.0.iter().map(|ta| {
rustls::OwnedTrustAnchor::from_subject_spki_name_constraints(
ta.subject,
ta.spki,
ta.name_constraints,
)
}));
let cfg = rustls::ClientConfig::builder()
.with_safe_defaults()
.with_root_certificates(root_store)
.with_no_client_auth();
Self {
tcp_connector: TcpConnector,
tls_connector: cfg.into(),
}
}
}
#[cfg(feature = "tls")]
impl<T, C> Connector<T> for TlsConnector<C>
where
T: ToSocketAddrs + Param<rustls::ServerName>,
C: Connector<T, Error = io::Error>,
C::Connection: AsyncReadRent + AsyncWriteRent,
{
type Connection = monoio_rustls::ClientTlsStream<C::Connection>;
type Error = monoio_rustls::TlsError;
type ConnectionFuture<'a> = impl Future<Output = Result<Self::Connection, Self::Error>> where C: 'a;
fn connect(&self, key: T) -> Self::ConnectionFuture<'_> {
async move {
let server_name = key.param();
let stream = self.tcp_connector.connect(key).await?;
let tls_stream = self.tls_connector.connect(server_name, stream).await?;
Ok(tls_stream)
}
}
}
pub struct PooledConnector<C, K: Hash + Eq, IO> {
inner: C,
pool: ConnectionPool<K, IO>,
}
impl<C, K: Hash + Eq, IO> Clone for PooledConnector<C, K, IO>
where
C: Clone,
{
fn clone(&self) -> Self {
Self {
inner: self.inner.clone(),
pool: self.pool.clone(),
}
}
}
impl<C, K: Hash + Eq + 'static, IO: 'static> Default for PooledConnector<C, K, IO>
where
C: Default,
{
fn default() -> Self {
Self {
inner: Default::default(),
pool: Default::default(),
}
}
}
impl<C, T, IO> Connector<T> for PooledConnector<C, T, IO>
where
T: ToSocketAddrs + Hash + Eq + ToOwned<Owned = T>,
C: Connector<T, Connection = IO>,
{
type Connection = PooledConnection<T, IO>;
type Error = C::Error;
type ConnectionFuture<'a> = impl Future<Output = Result<Self::Connection, Self::Error>> where Self: 'a;
fn connect(&self, key: T) -> Self::ConnectionFuture<'_> {
async move {
if let Some(conn) = self.pool.get(&key) {
return Ok(conn);
}
let key_owned = key.to_owned();
let io = self.inner.connect(key).await?;
let codec = ClientCodec::new(io);
Ok(self.pool.link(key_owned, codec))
}
}
}
#[cfg(test)]
mod tests {
use std::time::Instant;
use super::*;
#[monoio::test_all]
async fn connect_tcp() {
let connector = DefaultTcpConnector::<&'static str>::default();
let begin = Instant::now();
let conn = connector
.connect("captive.apple.com:80")
.await
.expect("unable to get connection");
println!("First connection cost {}ms", begin.elapsed().as_millis());
drop(conn);
let begin = Instant::now();
let _ = connector
.connect("captive.apple.com:80")
.await
.expect("unable to get connection");
let spent = begin.elapsed().as_millis();
println!("Second connection cost {}ms", spent);
assert!(
spent <= 2,
"second connect spend too much time, maybe not with pool?"
);
}
}