-
Notifications
You must be signed in to change notification settings - Fork 64
/
types.rs
176 lines (158 loc) · 4.62 KB
/
types.rs
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
use crate::{
error::{Error, ErrorKind},
modules::inner::ClientInner,
protocol::{connection::Connection, types::Server},
runtime::RefCount,
types::Resp3Frame,
};
use futures::stream::Stream;
use std::{
collections::HashMap,
future::Future,
pin::Pin,
task::{Context, Poll},
time::Instant,
};
/// Options describing how to change connections in a cluster.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct ClusterChange {
pub add: Vec<Server>,
pub remove: Vec<Server>,
}
impl Default for ClusterChange {
fn default() -> Self {
ClusterChange {
add: Vec::new(),
remove: Vec::new(),
}
}
}
// The following future types are used in the context of a select! loop, so they return Pending when there are no
// available connections to poll.
fn poll_connection(
inner: &RefCount<ClientInner>,
conn: &mut Connection,
cx: &mut Context<'_>,
buf: &mut Vec<(Server, Option<Result<Resp3Frame, Error>>)>,
now: &Instant,
) {
match Pin::new(&mut conn.transport).poll_next(cx) {
Poll::Ready(Some(frame)) => {
conn.last_write = None;
buf.push((conn.server.clone(), Some(frame.map(|f| f.into_resp3()))));
},
Poll::Ready(None) => {
conn.last_write = None;
buf.push((conn.server.clone(), None));
},
Poll::Pending => {
if let Some(duration) = inner.connection.unresponsive.max_timeout {
if let Some(last_write) = conn.last_write {
if now.saturating_duration_since(last_write) > duration {
buf.push((
conn.server.clone(),
Some(Err(Error::new(ErrorKind::IO, "Unresponsive connection."))),
));
}
}
}
},
};
}
/// A future that reads from all connections and performs unresponsive checks.
// `poll_next` on a Framed<TcpStream> is not cancel-safe
pub struct ReadAllFuture<'a, 'b> {
inner: &'a RefCount<ClientInner>,
connections: &'b mut HashMap<Server, Connection>,
#[cfg(feature = "replicas")]
replicas: &'b mut HashMap<Server, Connection>,
}
impl<'a, 'b> ReadAllFuture<'a, 'b> {
#[cfg(not(feature = "replicas"))]
pub fn new(inner: &'a RefCount<ClientInner>, connections: &'b mut HashMap<Server, Connection>) -> Self {
Self { connections, inner }
}
#[cfg(feature = "replicas")]
pub fn new(
inner: &'a RefCount<ClientInner>,
connections: &'b mut HashMap<Server, Connection>,
replicas: &'b mut HashMap<Server, Connection>,
) -> Self {
Self {
connections,
inner,
replicas,
}
}
}
impl Future for ReadAllFuture<'_, '_> {
type Output = Vec<(Server, Option<Result<Resp3Frame, Error>>)>;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
#[cfg(feature = "replicas")]
if self.connections.is_empty() && self.replicas.is_empty() {
return Poll::Pending;
}
#[cfg(not(feature = "replicas"))]
if self.connections.is_empty() {
return Poll::Pending;
}
let _self = self.get_mut();
let now = Instant::now();
let mut out = Vec::new();
for (_, conn) in _self.connections.iter_mut() {
poll_connection(_self.inner, conn, cx, &mut out, &now);
}
#[cfg(feature = "replicas")]
for (_, conn) in _self.replicas.iter_mut() {
poll_connection(_self.inner, conn, cx, &mut out, &now);
}
if out.is_empty() {
Poll::Pending
} else {
Poll::Ready(out)
}
}
}
/// A future that reads from the connection and performs unresponsive checks.
pub struct ReadFuture<'a, 'b> {
inner: &'a RefCount<ClientInner>,
connection: &'b mut Connection,
#[cfg(feature = "replicas")]
replicas: &'b mut HashMap<Server, Connection>,
}
impl<'a, 'b> ReadFuture<'a, 'b> {
#[cfg(not(feature = "replicas"))]
pub fn new(inner: &'a RefCount<ClientInner>, connection: &'b mut Connection) -> Self {
Self { connection, inner }
}
#[cfg(feature = "replicas")]
pub fn new(
inner: &'a RefCount<ClientInner>,
connection: &'b mut Connection,
replicas: &'b mut HashMap<Server, Connection>,
) -> Self {
Self {
inner,
connection,
replicas,
}
}
}
impl Future for ReadFuture<'_, '_> {
type Output = Vec<(Server, Option<Result<Resp3Frame, Error>>)>;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let mut out = Vec::new();
let now = Instant::now();
let _self = self.get_mut();
poll_connection(_self.inner, _self.connection, cx, &mut out, &now);
#[cfg(feature = "replicas")]
for (_, conn) in _self.replicas.iter_mut() {
poll_connection(_self.inner, conn, cx, &mut out, &now);
}
if out.is_empty() {
Poll::Pending
} else {
Poll::Ready(out)
}
}
}