viceroy-lib 0.20.0

Viceroy implementation details.
Documentation
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
use std::time::{Duration, Instant};

use crate::cache::CacheEntry;
use crate::downstream::DownstreamRequest;
use crate::execute::NextRequest;
use crate::http::PendingHeaders;
use crate::object_store::{KvStoreError, ObjectValue};
use crate::{body::Body, error::Error, streaming_body::StreamingBody};
use anyhow::anyhow;
use futures::Future;
use futures::FutureExt;
use http::Response;
use tokio::sync::oneshot;

#[derive(Debug)]
pub struct PendingKvLookupTask(PeekableTask<Result<Option<ObjectValue>, KvStoreError>>);
impl PendingKvLookupTask {
    pub fn new(t: PeekableTask<Result<Option<ObjectValue>, KvStoreError>>) -> PendingKvLookupTask {
        PendingKvLookupTask(t)
    }
    pub fn task(self) -> PeekableTask<Result<Option<ObjectValue>, KvStoreError>> {
        self.0
    }
}

#[derive(Debug)]
pub struct PendingKvInsertTask(PeekableTask<Result<(), KvStoreError>>);
impl PendingKvInsertTask {
    pub fn new(t: PeekableTask<Result<(), KvStoreError>>) -> PendingKvInsertTask {
        PendingKvInsertTask(t)
    }
    pub fn task(self) -> PeekableTask<Result<(), KvStoreError>> {
        self.0
    }
}

#[derive(Debug)]
pub struct PendingKvDeleteTask(PeekableTask<Result<bool, KvStoreError>>);
impl PendingKvDeleteTask {
    pub fn new(t: PeekableTask<Result<bool, KvStoreError>>) -> PendingKvDeleteTask {
        PendingKvDeleteTask(t)
    }
    pub fn task(self) -> PeekableTask<Result<bool, KvStoreError>> {
        self.0
    }
}

#[derive(Debug)]
pub struct PendingKvListTask(PeekableTask<Result<Vec<u8>, KvStoreError>>);
impl PendingKvListTask {
    pub fn new(t: PeekableTask<Result<Vec<u8>, KvStoreError>>) -> PendingKvListTask {
        PendingKvListTask(t)
    }
    pub fn task(self) -> PeekableTask<Result<Vec<u8>, KvStoreError>> {
        self.0
    }
}

/// This is very similar to a [PeekableTask] task, but is intentionally
/// set up so that it has no spawned tokio tasks separating us from the
/// [oneshot::Sender] and detecting when it's dropped.
///
/// The `try_recv()` method can be used to make sure that we safely recover
/// any pending requests that are sent to us without dropping them.
#[derive(Debug)]
pub enum PendingDownstreamReqTask {
    Complete(Result<Option<NextRequest>, Error>),
    Waiting(oneshot::Receiver<NextRequest>, Instant),
}

impl PendingDownstreamReqTask {
    pub fn new(
        rx: Option<oneshot::Receiver<NextRequest>>,
        timeout: Duration,
    ) -> PendingDownstreamReqTask {
        if let Some(rx) = rx {
            PendingDownstreamReqTask::Waiting(rx, Instant::now() + timeout)
        } else {
            PendingDownstreamReqTask::Complete(Ok(None))
        }
    }

    /// Receive a downstream request.
    ///
    /// This will block until the sender side of the channel is either dropped
    /// or sends us a value.
    pub async fn recv(mut self) -> Result<Option<DownstreamRequest>, Error> {
        self.await_ready().await;

        let Self::Complete(res) = self else {
            return Ok(None);
        };

        let Some(res) = res? else {
            return Ok(None);
        };

        Ok(res.into_request())
    }

    /// Drive this task to completion.
    ///
    /// If we have passed the deadline for the task, we try to recover any request
    /// in the channel. If there is none, then the timed out task will resolve to
    /// [Error::NoDownstreamReqsAvailable].
    ///
    /// ## Cancel Safety
    ///
    /// Note that this method is used with [FutureExt::now_or_never] in [AsyncItem::is_ready], and
    /// should therefore be kept cancel safe.
    pub async fn await_ready(&mut self) {
        if let Self::Waiting(rx, deadline) = self {
            let v = if Instant::now() > *deadline {
                rx.close();
                rx.try_recv().ok()
            } else {
                tokio::time::timeout_at((*deadline).into(), rx)
                    .await
                    .ok()
                    .and_then(|r| r.ok())
            };

            *self = Self::Complete(Ok(v));
        }
    }
}

/// An async item, waiting for a cache lookup to complete.
#[derive(Debug)]
pub struct PendingCacheTask(PeekableTask<CacheEntry>);
impl PendingCacheTask {
    pub fn new(t: PeekableTask<CacheEntry>) -> PendingCacheTask {
        PendingCacheTask(t)
    }
    pub fn task(self) -> PeekableTask<CacheEntry> {
        self.0
    }

    /// Get a mutable reference to the CacheEntry, possibly blocking until it becomes available.
    pub async fn as_mut(&mut self) -> &mut Result<CacheEntry, Error> {
        self.0.await_ready().await;
        self.0
            .get_mut()
            .expect("internal error: PeekableTask was not ready after AwaitReady")
    }
}

/// Represents either a full body, or the write end of a streaming body.
///
/// This enum is needed because we reuse the handle for a body when it is transformed into a streaming
/// body (writeable only). It is used within the body handle map in `Sandbox`.
#[derive(Debug)]
pub enum AsyncItem {
    Body(Body),
    StreamingBody(StreamingBody),
    PendingReq(PendingResponse),
    PendingDownstream(PendingDownstreamReqTask),
    PendingKvLookup(PendingKvLookupTask),
    PendingKvInsert(PendingKvInsertTask),
    PendingKvDelete(PendingKvDeleteTask),
    PendingKvList(PendingKvListTask),
    PendingCache(PendingCacheTask),
    Ready,
}

impl AsyncItem {
    pub fn is_streaming(&self) -> bool {
        matches!(self, Self::StreamingBody(_))
    }

    pub fn as_body(&self) -> Option<&Body> {
        match self {
            Self::Body(body) => Some(body),
            _ => None,
        }
    }

    pub fn as_body_mut(&mut self) -> Option<&mut Body> {
        match self {
            Self::Body(body) => Some(body),
            _ => None,
        }
    }

    pub fn into_body(self) -> Option<Body> {
        match self {
            Self::Body(body) => Some(body),
            _ => None,
        }
    }

    pub fn as_streaming_mut(&mut self) -> Option<&mut StreamingBody> {
        match self {
            Self::StreamingBody(sender) => Some(sender),
            _ => None,
        }
    }

    pub fn into_streaming(self) -> Option<StreamingBody> {
        match self {
            Self::StreamingBody(streaming) => Some(streaming),
            _ => None,
        }
    }

    pub fn begin_streaming(&mut self) -> Option<Body> {
        if self.is_streaming() {
            return None;
        }

        let (streaming, receiver) = StreamingBody::new();
        match std::mem::replace(self, Self::StreamingBody(streaming)) {
            Self::Body(mut body) => {
                body.push_back(receiver);
                Some(body)
            }
            _ => {
                unreachable!("!self.is_streaming, but was actually streaming");
            }
        }
    }

    pub fn as_pending_kv_lookup(&self) -> Option<&PendingKvLookupTask> {
        match self {
            Self::PendingKvLookup(req) => Some(req),
            _ => None,
        }
    }

    pub fn into_pending_kv_lookup(self) -> Option<PendingKvLookupTask> {
        match self {
            Self::PendingKvLookup(req) => Some(req),
            _ => None,
        }
    }

    pub fn as_pending_kv_insert(&self) -> Option<&PendingKvInsertTask> {
        match self {
            Self::PendingKvInsert(req) => Some(req),
            _ => None,
        }
    }

    pub fn into_pending_kv_insert(self) -> Option<PendingKvInsertTask> {
        match self {
            Self::PendingKvInsert(req) => Some(req),
            _ => None,
        }
    }

    pub fn as_pending_kv_delete(&self) -> Option<&PendingKvDeleteTask> {
        match self {
            Self::PendingKvDelete(req) => Some(req),
            _ => None,
        }
    }

    pub fn into_pending_kv_delete(self) -> Option<PendingKvDeleteTask> {
        match self {
            Self::PendingKvDelete(req) => Some(req),
            _ => None,
        }
    }

    pub fn as_pending_kv_list(&self) -> Option<&PendingKvListTask> {
        match self {
            Self::PendingKvList(req) => Some(req),
            _ => None,
        }
    }

    pub fn into_pending_kv_list(self) -> Option<PendingKvListTask> {
        match self {
            Self::PendingKvList(req) => Some(req),
            _ => None,
        }
    }

    pub fn as_pending_req(&self) -> Option<&PendingResponse> {
        match self {
            Self::PendingReq(req) => Some(req),
            _ => None,
        }
    }

    pub fn as_pending_req_mut(&mut self) -> Option<&mut PendingResponse> {
        match self {
            Self::PendingReq(req) => Some(req),
            _ => None,
        }
    }

    pub fn as_pending_cache(&self) -> Option<&PendingCacheTask> {
        match self {
            Self::PendingCache(op) => Some(op),
            _ => None,
        }
    }

    pub fn as_pending_cache_mut(&mut self) -> Option<&mut PendingCacheTask> {
        match self {
            Self::PendingCache(op) => Some(op),
            _ => None,
        }
    }

    pub fn into_pending_cache(self) -> Option<PendingCacheTask> {
        match self {
            Self::PendingCache(op) => Some(op),
            _ => None,
        }
    }

    pub fn into_pending_req(self) -> Option<PendingResponse> {
        match self {
            Self::PendingReq(req) => Some(req),
            _ => None,
        }
    }

    pub fn as_pending_downstream_req_mut(&mut self) -> Option<&mut PendingDownstreamReqTask> {
        match self {
            Self::PendingDownstream(req) => Some(req),
            _ => None,
        }
    }

    pub fn into_pending_downstream_req(self) -> Option<PendingDownstreamReqTask> {
        match self {
            Self::PendingDownstream(req) => Some(req),
            _ => None,
        }
    }

    pub async fn await_ready(&mut self) {
        match self {
            Self::StreamingBody(body) => body.await_ready().await,
            Self::Body(body) => body.await_ready().await,
            Self::PendingReq(req) => req.await_ready().await,
            Self::PendingDownstream(req) => req.await_ready().await,
            Self::PendingKvLookup(req) => req.0.await_ready().await,
            Self::PendingKvInsert(req) => req.0.await_ready().await,
            Self::PendingKvDelete(req) => req.0.await_ready().await,
            Self::PendingKvList(req) => req.0.await_ready().await,
            Self::PendingCache(req) => req.0.await_ready().await,
            Self::Ready => (),
        }
    }

    pub fn is_ready(&mut self) -> bool {
        match self {
            Self::PendingReq(req) => req.is_ready(),
            other => other.await_ready().now_or_never().is_some(),
        }
    }
}

impl From<PeekableTask<Response<Body>>> for AsyncItem {
    fn from(req: PeekableTask<Response<Body>>) -> Self {
        Self::PendingReq(PendingResponse::new(req))
    }
}

impl From<PendingKvLookupTask> for AsyncItem {
    fn from(task: PendingKvLookupTask) -> Self {
        Self::PendingKvLookup(task)
    }
}

impl From<PendingKvInsertTask> for AsyncItem {
    fn from(task: PendingKvInsertTask) -> Self {
        Self::PendingKvInsert(task)
    }
}

impl From<PendingKvDeleteTask> for AsyncItem {
    fn from(task: PendingKvDeleteTask) -> Self {
        Self::PendingKvDelete(task)
    }
}

impl From<PendingKvListTask> for AsyncItem {
    fn from(task: PendingKvListTask) -> Self {
        Self::PendingKvList(task)
    }
}

impl From<PendingCacheTask> for AsyncItem {
    fn from(task: PendingCacheTask) -> Self {
        Self::PendingCache(task)
    }
}

impl From<PendingDownstreamReqTask> for AsyncItem {
    fn from(task: PendingDownstreamReqTask) -> Self {
        Self::PendingDownstream(task)
    }
}

#[derive(Debug)]
pub enum PeekableTask<T> {
    Waiting(oneshot::Receiver<Result<T, Error>>),
    Complete(Result<T, Error>),
}

impl<T: Send + 'static> PeekableTask<T> {
    pub async fn spawn(fut: impl Future<Output = Result<T, Error>> + 'static + Send) -> Self {
        let (sender, receiver) = oneshot::channel();
        tokio::task::spawn(async move { sender.send(fut.await) });
        Self::Waiting(receiver)
    }

    pub fn complete(t: T) -> Self {
        PeekableTask::Complete(Ok(t))
    }

    /// Block until a response is ready.
    pub async fn await_ready(&mut self) {
        if let PeekableTask::Waiting(rx) = self {
            match rx.await {
                Ok(v) => *self = PeekableTask::Complete(v),
                _ => {
                    // todo, not the correct error type
                    *self = PeekableTask::Complete(Err(anyhow!(
                        "peekable task sender unexpectedly dropped"
                    )
                    .into()));
                }
            }
        }
    }

    pub async fn recv(self) -> Result<T, Error> {
        match self {
            PeekableTask::Waiting(rx) => rx
                .await
                .map_err(|_| anyhow!("peekable task sender unexpectedly dropped"))?,
            PeekableTask::Complete(res) => res,
        }
    }

    pub fn get_mut(&mut self) -> Option<&mut Result<T, Error>> {
        if let PeekableTask::Complete(res) = self {
            Some(res)
        } else {
            None
        }
    }
}

/// An asynchronously-waited-for response, along with header modifications to apply when the
/// [Response] arrives.
#[derive(Debug)]
pub struct PendingResponse {
    task: PeekableTask<Response<Body>>,

    /// Headers to apply to the response to the request.
    headers_resp: Box<PendingHeaders>,

    /// Headers to apply to the synthetic response made by the `recv_or_else` callback.
    headers_err: Box<PendingHeaders>,
}

impl PendingResponse {
    pub fn new(task: PeekableTask<Response<Body>>) -> Self {
        Self {
            task,
            headers_resp: Default::default(),
            headers_err: Default::default(),
        }
    }

    pub async fn await_ready(&mut self) {
        self.task.await_ready().await
    }

    pub fn headers_resp_mut(&mut self) -> &mut PendingHeaders {
        &mut self.headers_resp
    }

    pub fn headers_err_mut(&mut self) -> &mut PendingHeaders {
        &mut self.headers_err
    }

    pub fn is_ready(&mut self) -> bool {
        self.await_ready().now_or_never().is_some()
    }

    /// Wait until the request completes and we have the response or request error.
    ///
    /// The pending headers will be applied to the returned [Response].
    pub async fn recv(self) -> Result<Response<Body>, Error> {
        let mut resp = self.task.recv().await?;
        self.headers_resp.apply(resp.headers_mut());
        Ok(resp)
    }

    /// Similar to [PendingResponse::recv], but supports converting any request errors
    /// into an appropriate [Response].
    ///
    /// The pending headers will be applied after conversion so that the returned [Response]
    /// always contains them.
    pub async fn recv_or_else<F>(self, f: F) -> Response<Body>
    where
        F: Fn(Error) -> Response<Body>,
    {
        let resp = self.task.recv().await.map_err(f);

        match resp {
            Ok(mut resp) => {
                self.headers_resp.apply(resp.headers_mut());
                resp
            }
            Err(mut resp) => {
                self.headers_err.apply(resp.headers_mut());
                resp
            }
        }
    }
}