lox/crates/rdsys-backend/src/lib.rs

254 lines
8.3 KiB
Rust
Raw Normal View History

2023-01-30 15:23:01 -05:00
//! # Rdsys Backend Distributor API
//!
//! `rdsys_backend` is an implementation of the rdsys backend API
//! https://gitlab.torproject.org/tpo/anti-censorship/rdsys/-/blob/main/doc/backend-api.md
2023-01-30 15:08:30 -05:00
use bytes::{self, Buf, Bytes};
use core::pin::Pin;
use core::task::Poll;
use futures_util::{Stream, StreamExt};
2023-01-30 15:08:30 -05:00
use reqwest::Client;
use std::io::{self, BufRead};
use tokio::sync::mpsc;
use tokio_stream::wrappers::ReceiverStream;
2023-01-20 16:01:03 -05:00
2023-01-30 15:08:30 -05:00
pub mod proto;
2023-01-20 16:01:03 -05:00
2023-01-30 15:08:30 -05:00
#[derive(Debug)]
pub enum Error {
Reqwest(reqwest::Error),
Io(io::Error),
JSON(serde_json::Error),
}
2023-01-30 15:08:30 -05:00
impl From<serde_json::Error> for Error {
fn from(value: serde_json::Error) -> Self {
Self::JSON(value)
}
}
impl From<reqwest::Error> for Error {
fn from(value: reqwest::Error) -> Self {
Self::Reqwest(value)
}
2023-01-20 16:01:03 -05:00
}
2023-01-30 15:08:30 -05:00
impl From<io::Error> for Error {
fn from(value: io::Error) -> Self {
Self::Io(value)
}
2023-01-20 16:01:03 -05:00
}
2023-01-30 15:23:01 -05:00
/// An iterable wrapper of ResourceDiff items for the streamed chunks of Bytes
/// received from the connection to the rdsys backend
2023-01-30 15:08:30 -05:00
pub struct ResourceStream {
rx: ReceiverStream<Bytes>,
2023-01-30 15:08:30 -05:00
buf: Vec<u8>,
partial: Option<bytes::buf::Reader<Bytes>>,
}
impl ResourceStream {
pub fn new(rx: mpsc::Receiver<Bytes>) -> ResourceStream {
ResourceStream {
rx: ReceiverStream::new(rx),
buf: vec![],
partial: None,
}
}
}
impl Stream for ResourceStream {
2023-01-30 15:08:30 -05:00
type Item = proto::ResourceDiff;
fn poll_next(mut self: Pin<&mut Self>, cx: &mut core::task::Context) -> Poll<Option<Self::Item>> {
let parse =
|buffer: &mut bytes::buf::Reader<Bytes>, buf: &mut Vec<u8>| -> Result<Option<Self::Item>, Error> {
match buffer.read_until(b'\r', buf) {
Ok(_) => match buf.pop() {
Some(b'\r') => match serde_json::from_slice(buf) {
2023-01-30 15:08:30 -05:00
Ok(diff) => {
buf.clear();
2023-01-30 17:05:38 -05:00
Ok(Some(diff))
2023-01-30 15:08:30 -05:00
}
2023-01-30 17:05:38 -05:00
Err(e) => Err(Error::JSON(e)),
2023-01-30 15:08:30 -05:00
},
Some(n) => {
buf.push(n);
2023-01-30 17:05:38 -05:00
Ok(None)
2023-01-30 15:08:30 -05:00
}
2023-01-30 17:05:38 -05:00
None => Ok(None),
2023-01-30 15:08:30 -05:00
},
Err(e) => Err(Error::Io(e)),
}
};
// This clone is here to avoid having multiple mutable references to self
// it's not optimal performance-wise but given that these resource streams aren't large
// this feels like an acceptable trade-off to the complexity of interior mutability
let mut buf = self.buf.clone();
2023-01-30 15:08:30 -05:00
if let Some(p) = &mut self.partial {
match parse(p, &mut buf) {
Ok(Some(diff)) => return Poll::Ready(Some(diff)),
2023-01-30 15:08:30 -05:00
Ok(None) => self.partial = None,
Err(_) => return Poll::Ready(None),
2023-01-30 15:08:30 -05:00
}
}
self.buf = buf;
match Pin::new(&mut self.rx).poll_next(cx) {
Poll::Ready(Some(chunk)) => {
let mut buffer = chunk.reader();
match parse(&mut buffer, &mut self.buf) {
Ok(Some(diff)) => {
self.partial = Some(buffer);
return Poll::Ready(Some(diff));
}
Ok(None) => Poll::Pending, //maybe loop here?
Err(_) => return Poll::Ready(None),
2023-01-30 15:08:30 -05:00
}
}
Poll::Ready(None) => Poll::Ready(None),
Poll::Pending => Poll::Pending,
2023-01-30 15:08:30 -05:00
}
}
2023-01-20 16:01:03 -05:00
}
#[cfg(test)]
mod tests {
use super::*;
#[tokio::test]
async fn parse_resource() {
let mut cx = std::task::Context::from_waker(futures::task::noop_waker_ref());
2023-01-30 15:08:30 -05:00
let chunk = Bytes::from_static(
b"{\"new\": null,\"changed\": null,\"gone\": null,\"full_update\": true}\r",
2023-01-20 16:12:29 -05:00
);
let (tx, rx) = mpsc::channel(100);
tx.send(chunk).await.unwrap();
let mut diffs = ResourceStream::new(rx);
let res = Pin::new(&mut diffs).poll_next(&mut cx);
assert_ne!(res, Poll::Ready(None));
assert_ne!(res, Poll::Pending);
if let Poll::Ready(Some(diff)) = res {
2023-01-30 15:08:30 -05:00
assert_eq!(diff.new, None);
assert_eq!(diff.full_update, true);
}
}
#[tokio::test]
async fn parse_across_chunks() {
let mut cx = std::task::Context::from_waker(futures::task::noop_waker_ref());
2023-01-30 15:08:30 -05:00
let chunk1 = Bytes::from_static(b"{\"new\": null,\"changed\": null,");
let chunk2 = Bytes::from_static(b"\"gone\": null,\"full_update\": true}\r");
let (tx, rx) = mpsc::channel(100);
tx.send(chunk1).await.unwrap();
tx.send(chunk2).await.unwrap();
let mut diffs = ResourceStream::new(rx);
let mut res = Pin::new(&mut diffs).poll_next(&mut cx);
while let Poll::Pending = res {
res = Pin::new(&mut diffs).poll_next(&mut cx);
}
assert_ne!(res, Poll::Ready(None));
assert_ne!(res, Poll::Pending);
if let Poll::Ready(Some(diff)) = res {
2023-01-30 15:08:30 -05:00
assert_eq!(diff.new, None);
assert_eq!(diff.full_update, true);
}
2023-01-20 16:01:03 -05:00
}
#[tokio::test]
async fn parse_multi_diff_partial_chunks() {
let mut cx = std::task::Context::from_waker(futures::task::noop_waker_ref());
2023-01-30 15:08:30 -05:00
let chunk1 = Bytes::from_static(b"{\"new\": null,\"changed\": null,");
let chunk2 =
Bytes::from_static(b"\"gone\": null,\"full_update\": true}\r{\"new\": null,\"changed");
let chunk3 = Bytes::from_static(b"\": null,\"gone\": null,\"full_update\": true}");
let chunk4 = Bytes::from_static(b"\r");
let (tx, rx) = mpsc::channel(100);
tx.send(chunk1).await.unwrap();
tx.send(chunk2).await.unwrap();
tx.send(chunk3).await.unwrap();
tx.send(chunk4).await.unwrap();
let mut diffs = ResourceStream::new(rx);
let mut res = Pin::new(&mut diffs).poll_next(&mut cx);
while let Poll::Pending = res {
res = Pin::new(&mut diffs).poll_next(&mut cx);
}
assert_ne!(res, Poll::Ready(None));
assert_ne!(res, Poll::Pending);
if let Poll::Ready(Some(diff)) = res {
2023-01-30 15:08:30 -05:00
assert_eq!(diff.new, None);
assert_eq!(diff.full_update, true);
}
res = Pin::new(&mut diffs).poll_next(&mut cx);
while let Poll::Pending = res {
res = Pin::new(&mut diffs).poll_next(&mut cx);
}
assert_ne!(res, Poll::Ready(None));
assert_ne!(res, Poll::Pending);
if let Poll::Ready(Some(diff)) = res {
2023-01-30 15:08:30 -05:00
assert_eq!(diff.new, None);
assert_eq!(diff.full_update, true);
}
}
2023-01-20 16:01:03 -05:00
}
2023-01-30 15:08:30 -05:00
2023-01-30 15:23:01 -05:00
/// Makes an http connection to the rdsys backend api endpoint and returns a ResourceStream
/// if successful
///
/// # Examples
2023-01-30 17:05:38 -05:00
///
/// ```ignore
2023-01-30 15:23:01 -05:00
/// use rdsys_backend::start_stream;
///
/// let endpoint = String::from("http://127.0.0.1:7100/resource-stream");
/// let name = String::from("https");
/// let token = String::from("HttpsApiTokenPlaceholder");
/// let types = vec![String::from("obfs2"), String::from("scramblesuit")];
/// let stream = start_stream(endpoint, name, token, types).await.unwrap();
/// loop {
/// match Pin::new(&mut stream).poll_next(&mut cx) {
/// Poll::Ready(Some(diff)) => println!("Received diff: {:?}", diff),
/// Poll::Ready(None) => break,
/// Poll::Pending => continue,
/// }
2023-01-30 15:23:01 -05:00
/// }
/// ```
///
2023-01-30 15:08:30 -05:00
pub async fn start_stream(
api_endpoint: String,
name: String,
token: String,
resource_types: Vec<String>,
) -> Result<ResourceStream, Error> {
let (tx, rx) = mpsc::channel(100);
2023-01-30 15:08:30 -05:00
let req = proto::ResourceRequest {
request_origin: name,
2023-01-30 17:05:38 -05:00
resource_types,
2023-01-30 15:08:30 -05:00
};
let json = serde_json::to_string(&req)?;
let auth_value = format!("Bearer {}", token);
let client = Client::new();
let mut stream = client
.get(api_endpoint)
.header("Authorization", &auth_value)
.body(json)
.send()
.await?
.bytes_stream();
tokio::spawn(async move {
while let Some(chunk) = stream.next().await {
let bytes = match chunk {
Ok(b) => b,
Err(_e) => {
return;
}
};
tx.send(bytes).await.unwrap();
2023-01-30 15:08:30 -05:00
}
});
Ok(ResourceStream::new(rx))
2023-01-30 15:08:30 -05:00
}