-
Notifications
You must be signed in to change notification settings - Fork 64
/
pipeline.rs
320 lines (292 loc) · 10.7 KB
/
pipeline.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
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
use crate::{
error::Error,
interfaces::{self, *},
modules::{inner::ClientInner, response::FromValue},
prelude::{FredResult, Value},
protocol::{
command::{Command, RouterCommand},
responders::ResponseKind,
utils as protocol_utils,
},
runtime::{oneshot_channel, Mutex, OneshotReceiver, RefCount},
utils,
};
use std::{collections::VecDeque, fmt, fmt::Formatter};
fn clone_buffered_commands(buffer: &Mutex<VecDeque<Command>>) -> VecDeque<Command> {
buffer.lock().iter().map(|c| c.duplicate(ResponseKind::Skip)).collect()
}
fn prepare_all_commands(
commands: VecDeque<Command>,
error_early: bool,
) -> (RouterCommand, OneshotReceiver<Result<Resp3Frame, Error>>) {
let (tx, rx) = oneshot_channel();
let expected_responses = commands
.iter()
.fold(0, |count, cmd| count + cmd.response.expected_response_frames());
let mut response = ResponseKind::new_buffer_with_size(expected_responses, tx);
response.set_error_early(error_early);
let commands: Vec<Command> = commands
.into_iter()
.enumerate()
.map(|(idx, mut cmd)| {
cmd.response = response.duplicate().unwrap_or(ResponseKind::Skip);
cmd.response.set_expected_index(idx);
cmd
})
.collect();
let command = RouterCommand::Pipeline { commands };
(command, rx)
}
/// Send a series of commands in a [pipeline](https://redis.io/docs/manual/pipelining/).
///
/// See the [all](Self::all), [last](Self::last), and [try_all](Self::try_all) functions for more information.
pub struct Pipeline<C: ClientLike> {
commands: RefCount<Mutex<VecDeque<Command>>>,
client: C,
}
#[doc(hidden)]
impl<C: ClientLike> Clone for Pipeline<C> {
fn clone(&self) -> Self {
Pipeline {
commands: self.commands.clone(),
client: self.client.clone(),
}
}
}
impl<C: ClientLike> fmt::Debug for Pipeline<C> {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
f.debug_struct("Pipeline")
.field("client", &self.client.inner().id)
.field("length", &self.commands.lock().len())
.finish()
}
}
#[doc(hidden)]
impl<C: ClientLike> From<C> for Pipeline<C> {
fn from(client: C) -> Self {
Pipeline {
client,
commands: RefCount::new(Mutex::new(VecDeque::new())),
}
}
}
impl<C: ClientLike> ClientLike for Pipeline<C> {
#[doc(hidden)]
fn inner(&self) -> &RefCount<ClientInner> {
self.client.inner()
}
#[doc(hidden)]
fn change_command(&self, command: &mut Command) {
self.client.change_command(command);
}
#[doc(hidden)]
#[allow(unused_mut)]
fn send_command<T>(&self, command: T) -> Result<(), Error>
where
T: Into<Command>,
{
let mut command: Command = command.into();
self.change_command(&mut command);
if let Some(mut tx) = command.take_responder() {
trace!(
"{}: Respond early to {} command in pipeline.",
&self.client.inner().id,
command.kind.to_str_debug()
);
let _ = tx.send(Ok(protocol_utils::queued_frame()));
}
self.commands.lock().push_back(command);
Ok(())
}
}
#[cfg(feature = "i-acl")]
#[cfg_attr(docsrs, doc(cfg(feature = "i-acl")))]
impl<C: AclInterface> AclInterface for Pipeline<C> {}
#[cfg(feature = "i-client")]
#[cfg_attr(docsrs, doc(cfg(feature = "i-client")))]
impl<C: ClientInterface> ClientInterface for Pipeline<C> {}
#[cfg(feature = "i-cluster")]
#[cfg_attr(docsrs, doc(cfg(feature = "i-cluster")))]
impl<C: ClusterInterface> ClusterInterface for Pipeline<C> {}
#[cfg(feature = "i-pubsub")]
#[cfg_attr(docsrs, doc(cfg(feature = "i-pubsub")))]
impl<C: PubsubInterface> PubsubInterface for Pipeline<C> {}
#[cfg(feature = "i-config")]
#[cfg_attr(docsrs, doc(cfg(feature = "i-config")))]
impl<C: ConfigInterface> ConfigInterface for Pipeline<C> {}
#[cfg(feature = "i-geo")]
#[cfg_attr(docsrs, doc(cfg(feature = "i-geo")))]
impl<C: GeoInterface> GeoInterface for Pipeline<C> {}
#[cfg(feature = "i-hashes")]
#[cfg_attr(docsrs, doc(cfg(feature = "i-hashes")))]
impl<C: HashesInterface> HashesInterface for Pipeline<C> {}
#[cfg(feature = "i-hyperloglog")]
#[cfg_attr(docsrs, doc(cfg(feature = "i-hyperloglog")))]
impl<C: HyperloglogInterface> HyperloglogInterface for Pipeline<C> {}
#[cfg(feature = "i-keys")]
#[cfg_attr(docsrs, doc(cfg(feature = "i-keys")))]
impl<C: KeysInterface> KeysInterface for Pipeline<C> {}
#[cfg(feature = "i-lists")]
#[cfg_attr(docsrs, doc(cfg(feature = "i-lists")))]
impl<C: ListInterface> ListInterface for Pipeline<C> {}
#[cfg(feature = "i-memory")]
#[cfg_attr(docsrs, doc(cfg(feature = "i-memory")))]
impl<C: MemoryInterface> MemoryInterface for Pipeline<C> {}
#[cfg(feature = "i-server")]
#[cfg_attr(docsrs, doc(cfg(feature = "i-server")))]
impl<C: AuthInterface> AuthInterface for Pipeline<C> {}
#[cfg(feature = "i-server")]
#[cfg_attr(docsrs, doc(cfg(feature = "i-server")))]
impl<C: ServerInterface> ServerInterface for Pipeline<C> {}
#[cfg(feature = "i-slowlog")]
#[cfg_attr(docsrs, doc(cfg(feature = "i-slowlog")))]
impl<C: SlowlogInterface> SlowlogInterface for Pipeline<C> {}
#[cfg(feature = "i-sets")]
#[cfg_attr(docsrs, doc(cfg(feature = "i-sets")))]
impl<C: SetsInterface> SetsInterface for Pipeline<C> {}
#[cfg(feature = "i-sorted-sets")]
#[cfg_attr(docsrs, doc(cfg(feature = "i-sorted-sets")))]
impl<C: SortedSetsInterface> SortedSetsInterface for Pipeline<C> {}
#[cfg(feature = "i-streams")]
#[cfg_attr(docsrs, doc(cfg(feature = "i-streams")))]
impl<C: StreamsInterface> StreamsInterface for Pipeline<C> {}
#[cfg(feature = "i-scripts")]
#[cfg_attr(docsrs, doc(cfg(feature = "i-scripts")))]
impl<C: FunctionInterface> FunctionInterface for Pipeline<C> {}
#[cfg(feature = "i-redis-json")]
#[cfg_attr(docsrs, doc(cfg(feature = "i-redis-json")))]
impl<C: RedisJsonInterface> RedisJsonInterface for Pipeline<C> {}
#[cfg(feature = "i-time-series")]
#[cfg_attr(docsrs, doc(cfg(feature = "i-time-series")))]
impl<C: TimeSeriesInterface> TimeSeriesInterface for Pipeline<C> {}
#[cfg(feature = "i-redisearch")]
#[cfg_attr(docsrs, doc(cfg(feature = "i-redisearch")))]
impl<C: RediSearchInterface> RediSearchInterface for Pipeline<C> {}
impl<C: ClientLike> Pipeline<C> {
/// Send the pipeline and respond with an array of all responses.
///
/// ```rust no_run
/// # use fred::prelude::*;
/// async fn example(client: &Client) -> Result<(), Error> {
/// let _ = client.mset(vec![("foo", 1), ("bar", 2)]).await?;
///
/// let pipeline = client.pipeline();
/// let _: () = pipeline.get("foo").await?; // returns when the command is queued in memory
/// let _: () = pipeline.get("bar").await?; // returns when the command is queued in memory
///
/// let results: Vec<i64> = pipeline.all().await?;
/// assert_eq!(results, vec![1, 2]);
/// Ok(())
/// }
/// ```
pub async fn all<R>(&self) -> Result<R, Error>
where
R: FromValue,
{
let commands = clone_buffered_commands(&self.commands);
send_all(self.client.inner(), commands).await?.convert()
}
/// Send the pipeline and respond with each individual result.
///
/// Note: use `Value` as the return type (and [convert](crate::types::Value::convert) as needed) to
/// support an array of different return types.
///
/// ```rust no_run
/// # use fred::prelude::*;
/// async fn example(client: &Client) -> Result<(), Error> {
/// let _ = client.mset(vec![("foo", 1), ("bar", 2)]).await?;
///
/// let pipeline = client.pipeline();
/// let _: () = pipeline.get("foo").await?;
/// let _: () = pipeline.hgetall("bar").await?; // this will error since `bar` is an integer
///
/// let results = pipeline.try_all::<Value>().await;
/// assert_eq!(results[0].clone()?.convert::<i64>()?, 1);
/// assert!(results[1].is_err());
///
/// Ok(())
/// }
/// ```
pub async fn try_all<R>(&self) -> Vec<FredResult<R>>
where
R: FromValue,
{
let commands = clone_buffered_commands(&self.commands);
try_send_all(self.client.inner(), commands)
.await
.into_iter()
.map(|v| v.and_then(|v| v.convert()))
.collect()
}
/// Send the pipeline and respond with only the result of the last command.
///
/// ```rust no_run
/// # use fred::prelude::*;
/// async fn example(client: &Client) -> Result<(), Error> {
/// let pipeline = client.pipeline();
/// let _: () = pipeline.incr("foo").await?; // returns when the command is queued in memory
/// let _: () = pipeline.incr("foo").await?; // returns when the command is queued in memory
///
/// assert_eq!(pipeline.last::<i64>().await?, 2);
/// // pipelines can also be reused
/// assert_eq!(pipeline.last::<i64>().await?, 4);
/// Ok(())
/// }
/// ```
pub async fn last<R>(&self) -> Result<R, Error>
where
R: FromValue,
{
let commands = clone_buffered_commands(&self.commands);
send_last(self.client.inner(), commands).await?.convert()
}
}
async fn try_send_all(inner: &RefCount<ClientInner>, commands: VecDeque<Command>) -> Vec<Result<Value, Error>> {
if commands.is_empty() {
return Vec::new();
}
let (mut command, rx) = prepare_all_commands(commands, false);
command.inherit_options(inner);
let timeout_dur = command.timeout_dur().unwrap_or_else(|| inner.default_command_timeout());
if let Err(e) = interfaces::send_to_router(inner, command) {
return vec![Err(e)];
};
let frame = match utils::timeout(rx, timeout_dur).await {
Ok(result) => match result {
Ok(f) => f,
Err(e) => return vec![Err(e)],
},
Err(e) => return vec![Err(e)],
};
if let Resp3Frame::Array { data, .. } = frame {
data.into_iter().map(protocol_utils::frame_to_results).collect()
} else {
vec![protocol_utils::frame_to_results(frame)]
}
}
async fn send_all(inner: &RefCount<ClientInner>, commands: VecDeque<Command>) -> Result<Value, Error> {
if commands.is_empty() {
return Ok(Value::Array(Vec::new()));
}
let (mut command, rx) = prepare_all_commands(commands, true);
command.inherit_options(inner);
let timeout_dur = command.timeout_dur().unwrap_or_else(|| inner.default_command_timeout());
interfaces::send_to_router(inner, command)?;
let frame = utils::timeout(rx, timeout_dur).await??;
protocol_utils::frame_to_results(frame)
}
async fn send_last(inner: &RefCount<ClientInner>, commands: VecDeque<Command>) -> Result<Value, Error> {
if commands.is_empty() {
return Ok(Value::Null);
}
let len = commands.len();
let (tx, rx) = oneshot_channel();
let mut commands: Vec<Command> = commands.into_iter().collect();
commands[len - 1].response = ResponseKind::Respond(Some(tx));
let mut command = RouterCommand::Pipeline { commands };
command.inherit_options(inner);
let timeout_dur = command.timeout_dur().unwrap_or_else(|| inner.default_command_timeout());
interfaces::send_to_router(inner, command)?;
let frame = utils::timeout(rx, timeout_dur).await??;
protocol_utils::frame_to_results(frame)
}