corro-client 0.2.0-alpha.0

client to interact with corrosion
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
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
//! Streaming response types for the Corrosion HTTP API.
//!
//! Each long-running endpoint exposes a dedicated [`Stream`] implementation:
//!
//! * [`QueryStream`] — one-shot query results
//!   (`POST /v1/queries`).
//! * [`SubscriptionStream`] — resumable live subscription
//!   (`POST /v1/subscriptions`); transparently reconnects with the last
//!   observed [`ChangeId`] on transient I/O errors.
//! * [`UpdatesStream`] — table-level update feed (`POST /v1/updates/{table}`).
//!
//! All three decode JSON-lines responses through the [`LinesBytesCodec`]
//! defined here, which is a port of `tokio-util`'s `LinesCodec` adapted to
//! emit [`bytes::BytesMut`] frames.

use std::{
    error::Error,
    io,
    net::SocketAddr,
    pin::Pin,
    task::{Context, Poll},
    time::Duration,
};

use bytes::{Buf, Bytes, BytesMut};
use corro_api_types::{ChangeId, QueryEvent, TypedNotifyEvent, TypedQueryEvent};
use futures::{ready, Future, Stream};
use pin_project_lite::pin_project;
use serde::de::DeserializeOwned;
use tokio::time::{sleep, Sleep};
use tokio_util::{
    codec::{Decoder, FramedRead, LinesCodecError},
    io::StreamReader,
};
use tracing::error;
use uuid::Uuid;

pin_project! {
    /// Adapter that exposes a [`reqwest::Body`] as a [`Stream`] of
    /// [`io::Result<Bytes>`], mapping framing errors into [`io::Error`] so
    /// the underlying [`StreamReader`] can consume them.
    pub struct IoBodyStream {
        #[pin]
        body: reqwest::Body
    }
}

impl Stream for IoBodyStream {
    type Item = io::Result<Bytes>;

    fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
        use http_body::Body;
        let this = self.project();
        let res = ready!(this.body.poll_frame(cx));
        match res {
            Some(Ok(b)) => Poll::Ready(Some(
                b.into_data()
                    .map_err(|_| io::Error::other("not a data frame")),
            )),
            Some(Err(e)) => {
                let io_err = match e
                    .source()
                    .and_then(|source| source.downcast_ref::<io::Error>())
                {
                    Some(io_err) => io::Error::from(io_err.kind()),
                    None => io::Error::other(e),
                };
                Poll::Ready(Some(Err(io_err)))
            }
            None => Poll::Ready(None),
        }
    }
}

type IoBodyStreamReader = StreamReader<IoBodyStream, Bytes>;
type FramedBody = FramedRead<IoBodyStreamReader, LinesBytesCodec>;
type ResponseFuture =
    Box<dyn Future<Output = Result<reqwest::Response, reqwest::Error>> + Unpin + Send + Sync>;

/// Live subscription stream returned by
/// [`crate::CorrosionApiClient::subscribe_typed`].
///
/// Yields [`TypedQueryEvent<T>`] frames produced by the agent. Once the
/// initial result set is fully consumed (after the first
/// [`TypedQueryEvent::EndOfQuery`]) the stream automatically reconnects
/// when the underlying HTTP body terminates, using the last observed
/// [`ChangeId`] to resume without gaps. Reconnects use a fixed-size
/// linear backoff and give up after 10 consecutive failures with
/// [`SubscriptionError::MaxRetryAttempts`].
pub struct SubscriptionStream<T> {
    id: Uuid,
    hash: Option<String>,
    client: reqwest::Client,
    api_addr: SocketAddr,
    observed_eoq: bool,
    last_change_id: Option<ChangeId>,
    stream: Option<FramedBody>,
    backoff: Option<Pin<Box<Sleep>>>,
    backoff_count: u32,
    response: Option<ResponseFuture>,
    _deser: std::marker::PhantomData<T>,
}

/// Errors yielded by [`SubscriptionStream`].
#[derive(Debug, thiserror::Error)]
pub enum SubscriptionError {
    /// Underlying I/O error on the HTTP body.
    #[error(transparent)]
    Io(#[from] io::Error),
    /// Generic HTTP error encountered while reconnecting.
    #[error(transparent)]
    Http(#[from] http::Error),
    /// A frame could not be decoded as a [`TypedQueryEvent`].
    #[error(transparent)]
    Deserialize(#[from] serde_json::Error),
    /// The agent skipped a [`ChangeId`], indicating the local view is no
    /// longer consistent with the server.
    #[error("missed a change (expected: {expected}, got: {got}), inconsistent state")]
    MissedChange { expected: ChangeId, got: ChangeId },
    /// A single JSON line exceeded the codec's maximum length.
    #[error("max line length exceeded")]
    MaxLineLengthExceeded,
    /// The connection terminated before the initial query produced an
    /// [`TypedQueryEvent::EndOfQuery`].
    #[error("initial query never finished")]
    UnfinishedQuery,
    /// Error when maximum number of consecutive reconnect is exceeded.
    #[error("max retry attempts exceeded")]
    MaxRetryAttempts,
}

impl<T> SubscriptionStream<T>
where
    T: DeserializeOwned + Unpin,
{
    pub fn new(
        id: Uuid,
        hash: Option<String>,
        client: reqwest::Client,
        api_addr: SocketAddr,
        body: reqwest::Body,
        change_id: Option<ChangeId>,
    ) -> Self {
        Self {
            id,
            hash,
            client,
            api_addr,
            observed_eoq: change_id.is_some(),
            last_change_id: change_id,
            stream: Some(FramedRead::new(
                StreamReader::new(IoBodyStream { body }),
                LinesBytesCodec::default(),
            )),
            backoff: None,
            backoff_count: 0,
            response: None,
            _deser: Default::default(),
        }
    }

    /// Server-assigned subscription identifier.
    ///
    /// Persist this id (along with the [`ChangeId`] from
    /// [`TypedQueryEvent::Change`]) to resume the subscription later via
    /// [`crate::CorrosionApiClient::subscription_typed`].
    pub fn id(&self) -> Uuid {
        self.id
    }

    /// Hash advertised by the server for this subscription's query.
    ///
    pub fn hash(&self) -> Option<&str> {
        self.hash.as_deref()
    }

    /// API address the subscription was opened against.
    pub fn api_addr(&self) -> SocketAddr {
        self.api_addr
    }

    fn poll_stream(
        mut self: Pin<&mut Self>,
        cx: &mut Context<'_>,
    ) -> Poll<Option<Result<TypedQueryEvent<T>, SubscriptionError>>> {
        let stream = loop {
            match self.stream.as_mut() {
                None => match ready!(self.as_mut().poll_request(cx)) {
                    Ok(stream) => {
                        self.stream = Some(stream);
                    }
                    Err(e) => return Poll::Ready(Some(Err(e))),
                },
                Some(stream) => {
                    break stream;
                }
            }
        };

        let res = ready!(Pin::new(stream).poll_next(cx));
        match res {
            Some(Ok(b)) => match serde_json::from_slice(&b) {
                Ok(evt) => {
                    if let TypedQueryEvent::EndOfQuery { change_id, .. } = &evt {
                        self.handle_eoq(*change_id);
                    }

                    if let TypedQueryEvent::Change(_, _, _, change_id) = &evt {
                        if let Err(e) = self.handle_change(*change_id) {
                            return Poll::Ready(Some(Err(e)));
                        }
                    }

                    Poll::Ready(Some(Ok(evt)))
                }
                Err(deser_err) => {
                    // It failed to deserialize, try untyped variant to extract the metadata
                    if let Ok(evt) = serde_json::from_slice::<QueryEvent>(&b) {
                        if let TypedQueryEvent::EndOfQuery { change_id, .. } = &evt {
                            self.handle_eoq(*change_id);
                        }

                        if let TypedQueryEvent::Change(_, _, _, change_id) = &evt {
                            if let Err(e) = self.handle_change(*change_id) {
                                return Poll::Ready(Some(Err(e)));
                            }
                        }
                    }

                    // But return the original error anyway (unless this is out-of-order event)
                    Poll::Ready(Some(Err(deser_err.into())))
                }
            },
            Some(Err(e)) => match e {
                LinesCodecError::MaxLineLengthExceeded => {
                    Poll::Ready(Some(Err(SubscriptionError::MaxLineLengthExceeded)))
                }
                LinesCodecError::Io(io_err) => Poll::Ready(Some(Err(io_err.into()))),
            },
            None => Poll::Ready(None),
        }
    }

    fn handle_eoq(&mut self, change_id: Option<ChangeId>) {
        self.observed_eoq = true;
        self.last_change_id = change_id;
    }

    fn handle_change(&mut self, change_id: ChangeId) -> Result<(), SubscriptionError> {
        match self.last_change_id {
            Some(id) if id + 1 != change_id => {
                return Err(SubscriptionError::MissedChange {
                    expected: id + 1,
                    got: change_id,
                })
            }
            _ => (),
        }

        self.last_change_id = Some(change_id);

        Ok(())
    }

    fn poll_request(
        mut self: Pin<&mut Self>,
        cx: &mut Context<'_>,
    ) -> Poll<Result<FramedBody, SubscriptionError>> {
        loop {
            if let Some(res_fut) = self.response.as_mut() {
                // return early w/ Poll::Pending if response is not ready
                let res = ready!(Pin::new(res_fut).poll(cx));

                // reset response
                self.response = None;

                return match res {
                    Ok(res) => Poll::Ready(Ok(FramedRead::new(
                        StreamReader::new(IoBodyStream { body: res.into() }),
                        LinesBytesCodec::default(),
                    ))),
                    Err(e) => {
                        let io_err = match e
                            .source()
                            .and_then(|source| source.downcast_ref::<io::Error>())
                        {
                            Some(io_err) => io::Error::from(io_err.kind()),
                            None => io::Error::other(e),
                        };
                        Poll::Ready(Err(io_err.into()))
                    }
                };
            } else if self.observed_eoq {
                let response = self
                    .client
                    .get(format!(
                        "http://{}/v1/subscriptions/{}?from={}",
                        self.api_addr,
                        self.id,
                        self.last_change_id.unwrap_or_default()
                    ))
                    .header(http::header::ACCEPT, "application/json")
                    .send();

                self.response = Some(Box::new(response));
                // loop around!
            } else {
                return Poll::Ready(Err(SubscriptionError::UnfinishedQuery));
            }
        }
    }
}

impl<T> Stream for SubscriptionStream<T>
where
    T: DeserializeOwned + Unpin,
{
    type Item = Result<TypedQueryEvent<T>, SubscriptionError>;

    fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
        // first, check if we need to wait for a backoff...
        if let Some(backoff) = self.backoff.as_mut() {
            ready!(backoff.as_mut().poll(cx));
            self.backoff = None;
        }

        let io_err = match ready!(self.as_mut().poll_stream(cx)) {
            Some(Err(SubscriptionError::Io(io_err))) => io_err,
            other => {
                self.backoff_count = 0;
                return Poll::Ready(other);
            }
        };

        // reset the stream
        self.stream = None;

        if self.backoff_count >= 10 {
            return Poll::Ready(Some(Err(SubscriptionError::MaxRetryAttempts)));
        }

        error!("encountered a stream IO error: {io_err}, retrying in a bit");

        let mut backoff = Box::pin(sleep(Duration::from_secs(1)));

        // register w/ waker
        _ = backoff.as_mut().poll(cx);

        // this can't return Ready, right?
        self.backoff = Some(backoff);

        self.backoff_count += 1;

        Poll::Pending
    }
}

/// Stream returned by [`crate::CorrosionApiClient::updates_typed`].
///
/// Yields a [`TypedNotifyEvent`] for every row inserted, updated or deleted
/// in the watched table after the update was registered.
pub struct UpdatesStream<T> {
    id: Uuid,
    stream: FramedBody,
    _deser: std::marker::PhantomData<T>,
}

/// Errors yielded by [`UpdatesStream`].
#[derive(Debug, thiserror::Error)]
pub enum UpdatesError {
    /// Underlying I/O error on the HTTP body.
    #[error(transparent)]
    Io(#[from] io::Error),
    /// A frame could not be decoded as a [`TypedNotifyEvent`].
    #[error(transparent)]
    Deserialize(#[from] serde_json::Error),
    /// A single JSON line exceeded the codec's maximum length.
    #[error("max line length exceeded")]
    MaxLineLengthExceeded,
}

impl<T> UpdatesStream<T>
where
    T: DeserializeOwned + Unpin,
{
    /// Build an `UpdatesStream` from a freshly opened HTTP response.
    pub fn new(id: Uuid, body: reqwest::Body) -> Self {
        Self {
            id,
            stream: FramedRead::new(
                StreamReader::new(IoBodyStream { body }),
                LinesBytesCodec::default(),
            ),
            _deser: Default::default(),
        }
    }

    /// Server-assigned subscription identifier for this stream.
    pub fn id(&self) -> Uuid {
        self.id
    }
}

impl<T> Stream for UpdatesStream<T>
where
    T: DeserializeOwned + Unpin,
{
    type Item = Result<TypedNotifyEvent<T>, UpdatesError>;

    fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
        let res = ready!(Pin::new(&mut self.stream).poll_next(cx));
        match res {
            Some(Ok(b)) => match serde_json::from_slice(&b) {
                Ok(evt) => Poll::Ready(Some(Ok(evt))),
                Err(e) => Poll::Ready(Some(Err(e.into()))),
            },
            Some(Err(e)) => match e {
                LinesCodecError::MaxLineLengthExceeded => {
                    Poll::Ready(Some(Err(UpdatesError::MaxLineLengthExceeded)))
                }
                LinesCodecError::Io(io_err) => Poll::Ready(Some(Err(io_err.into()))),
            },
            None => Poll::Ready(None),
        }
    }
}

/// Stream returned by [`crate::CorrosionApiClient::query_typed`] for a single query.
///
/// Yields a [`TypedQueryEvent`] with columns, each row of the result set, and a final [`TypedQueryEvent::EndOfQuery`].
pub struct QueryStream<T> {
    stream: FramedBody,
    _deser: std::marker::PhantomData<T>,
}

/// Errors yielded by [`QueryStream`].
#[derive(Debug, thiserror::Error)]
pub enum QueryError {
    /// Underlying I/O error on the HTTP body.
    #[error(transparent)]
    Io(#[from] io::Error),
    /// A frame could not be decoded as a [`TypedQueryEvent`].
    #[error(transparent)]
    Deserialize(#[from] serde_json::Error),
    /// A single JSON line exceeded the codec's maximum length.
    #[error("max line length exceeded")]
    MaxLineLengthExceeded,
}

impl<T> QueryStream<T>
where
    T: DeserializeOwned + Unpin,
{
    /// Build a `QueryStream` from a freshly opened HTTP response.
    pub fn new(body: reqwest::Body) -> Self {
        Self {
            stream: FramedRead::new(
                StreamReader::new(IoBodyStream { body }),
                LinesBytesCodec::default(),
            ),
            _deser: Default::default(),
        }
    }
}

impl<T> Stream for QueryStream<T>
where
    T: DeserializeOwned + Unpin,
{
    type Item = Result<TypedQueryEvent<T>, QueryError>;

    fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
        match ready!(Pin::new(&mut self.stream).poll_next(cx)) {
            Some(Ok(b)) => match serde_json::from_slice(&b) {
                Ok(evt) => Poll::Ready(Some(Ok(evt))),
                Err(e) => Poll::Ready(Some(Err(e.into()))),
            },
            Some(Err(e)) => match e {
                LinesCodecError::MaxLineLengthExceeded => {
                    Poll::Ready(Some(Err(QueryError::MaxLineLengthExceeded)))
                }
                LinesCodecError::Io(io_err) => Poll::Ready(Some(Err(io_err.into()))),
            },
            None => Poll::Ready(None),
        }
    }
}

/// `LinesBytesCodec` used to to split up bytes into lines.
/// It uses the `\n` character as the line delimiter.
pub struct LinesBytesCodec {
    // Stored index of the next index to examine for a `\n` character.
    // This is used to optimize searching.
    // For example, if `decode` was called with `abc`, it would hold `3`,
    // because that is the next index to examine.
    // The next time `decode` is called with `abcde\n`, the method will
    // only look at `de\n` before returning.
    next_index: usize,

    /// The maximum length for a given line. If `usize::MAX`, lines will be
    /// read until a `\n` character is reached.
    max_length: usize,

    /// Are we currently discarding the remainder of a line which was over
    /// the length limit?
    is_discarding: bool,
}

impl Default for LinesBytesCodec {
    /// Returns a `LinesBytesCodec` for splitting up data into lines.
    ///
    /// # Note
    ///
    /// The returned `LinesBytesCodec` will not have an upper bound on the length
    /// of a buffered line. See the documentation for [`new_with_max_length`]
    /// for information on why this could be a potential security risk.
    ///
    fn default() -> Self {
        LinesBytesCodec {
            next_index: 0,
            max_length: usize::MAX,
            is_discarding: false,
        }
    }
}

impl Decoder for LinesBytesCodec {
    type Item = BytesMut;
    type Error = LinesCodecError;

    fn decode(&mut self, buf: &mut BytesMut) -> Result<Option<BytesMut>, LinesCodecError> {
        loop {
            // Determine how far into the buffer we'll search for a newline. If
            // there's no max_length set, we'll read to the end of the buffer.
            let read_to = std::cmp::min(self.max_length.saturating_add(1), buf.len());

            let newline_offset = buf[self.next_index..read_to]
                .iter()
                .position(|b| *b == b'\n');

            match (self.is_discarding, newline_offset) {
                (true, Some(offset)) => {
                    // If we found a newline, discard up to that offset and
                    // then stop discarding. On the next iteration, we'll try
                    // to read a line normally.
                    buf.advance(offset + self.next_index + 1);
                    self.is_discarding = false;
                    self.next_index = 0;
                }
                (true, None) => {
                    // Otherwise, we didn't find a newline, so we'll discard
                    // everything we read. On the next iteration, we'll continue
                    // discarding up to max_len bytes unless we find a newline.
                    buf.advance(read_to);
                    self.next_index = 0;
                    if buf.is_empty() {
                        return Ok(None);
                    }
                }
                (false, Some(offset)) => {
                    // Found a line!
                    let newline_index = offset + self.next_index;
                    self.next_index = 0;
                    let mut line = buf.split_to(newline_index + 1);
                    line.truncate(line.len() - 1);
                    without_carriage_return(&mut line);
                    return Ok(Some(line));
                }
                (false, None) if buf.len() > self.max_length => {
                    // Reached the maximum length without finding a
                    // newline, return an error and start discarding on the
                    // next call.
                    self.is_discarding = true;
                    return Err(LinesCodecError::MaxLineLengthExceeded);
                }
                (false, None) => {
                    // We didn't find a line or reach the length limit, so the next
                    // call will resume searching at the current offset.
                    self.next_index = read_to;
                    return Ok(None);
                }
            }
        }
    }

    fn decode_eof(&mut self, buf: &mut BytesMut) -> Result<Option<BytesMut>, LinesCodecError> {
        Ok(match self.decode(buf)? {
            Some(frame) => Some(frame),
            None => {
                // No terminating newline - return remaining data, if any
                if buf.is_empty() || buf == &b"\r"[..] {
                    None
                } else {
                    let mut line = buf.split_to(buf.len());
                    line.truncate(line.len() - 1);
                    without_carriage_return(&mut line);
                    self.next_index = 0;
                    Some(line)
                }
            }
        })
    }
}

fn without_carriage_return(s: &mut BytesMut) {
    if let Some(&b'\r') = s.last() {
        s.truncate(s.len() - 1);
    }
}