Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor stream names #787

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 26 additions & 83 deletions async-nats/src/jetstream/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -484,8 +484,8 @@ impl Context {
/// # Ok(())
/// # }
/// ```
pub fn stream_names(&self) -> StreamNames {
StreamNames {
pub fn stream_names(&self) -> Streams<String> {
Streams {
context: self.clone(),
offset: 0,
page_request: None,
Expand All @@ -511,7 +511,7 @@ impl Context {
/// # Ok(())
/// # }
/// ```
pub fn streams(&self) -> Streams {
pub fn streams(&self) -> Streams<Info> {
Streams {
context: self.clone(),
offset: 0,
Expand Down Expand Up @@ -930,99 +930,41 @@ impl IntoFuture for PublishAckFuture {
}

#[derive(Deserialize, Debug)]
struct StreamPage {
total: usize,
streams: Option<Vec<String>>,
}

#[derive(Deserialize, Debug)]
struct StreamInfoPage {
struct StreamPage<T> {
total: usize,
streams: Option<Vec<super::stream::Info>>,
streams: Option<Vec<T>>,
}

type PageRequest = Pin<Box<dyn Future<Output = Result<StreamPage, Error>>>>;

pub struct StreamNames {
context: Context,
offset: usize,
page_request: Option<PageRequest>,
streams: Vec<String>,
done: bool,
impl RequestSubject for String {
fn subject() -> String {
"STREAM.NAMES".to_string()
}
}

impl futures::Stream for StreamNames {
type Item = Result<String, Error>;

fn poll_next(
mut self: Pin<&mut Self>,
cx: &mut std::task::Context<'_>,
) -> std::task::Poll<Option<Self::Item>> {
match self.page_request.as_mut() {
Some(page) => match page.try_poll_unpin(cx) {
std::task::Poll::Ready(page) => {
self.page_request = None;
let page = page?;
if let Some(streams) = page.streams {
self.offset += streams.len();
self.streams = streams;
if self.offset >= page.total {
self.done = true;
}
match self.streams.pop() {
Some(stream) => Poll::Ready(Some(Ok(stream))),
None => Poll::Ready(None),
}
} else {
Poll::Ready(None)
}
}
std::task::Poll::Pending => std::task::Poll::Pending,
},
None => {
if let Some(stream) = self.streams.pop() {
Poll::Ready(Some(Ok(stream)))
} else {
if self.done {
return Poll::Ready(None);
}
let context = self.context.clone();
let offset = self.offset;
self.page_request = Some(Box::pin(async move {
match context
.request(
"STREAM.NAMES".to_string(),
&json!({
"offset": offset,
}),
)
.await?
{
Response::Err { error } => {
Err(Box::from(std::io::Error::new(ErrorKind::Other, error)))
}
Response::Ok(page) => Ok(page),
}
}));
self.poll_next(cx)
}
}
}
impl RequestSubject for Info {
fn subject() -> String {
"STREAM.LIST".to_string()
}
}

type PageInfoRequest = Pin<Box<dyn Future<Output = Result<StreamInfoPage, Error>>>>;
trait RequestSubject {
fn subject() -> String;
}
type PageRequest<T> = Option<Pin<Box<dyn Future<Output = Result<StreamPage<T>, Error>>>>>;

pub struct Streams {
pub struct Streams<T> {
Copy link
Collaborator

@caspervonb caspervonb Jan 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still StreamNames, since T isn't a valid stream handle?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't have to be.
It can accept stream::Info or String, so not really StreamNames, isn't it?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm but the endpoint that is using this is, stream_names right? 🤔

context: Context,
offset: usize,
page_request: Option<PageInfoRequest>,
streams: Vec<super::stream::Info>,
page_request: PageRequest<T>,
streams: Vec<T>,
done: bool,
}

impl futures::Stream for Streams {
type Item = Result<super::stream::Info, Error>;
impl<T> futures::Stream for Streams<T>
where
T: RequestSubject + DeserializeOwned + Unpin,
{
type Item = Result<T, Error>;

fn poll_next(
mut self: Pin<&mut Self>,
Expand Down Expand Up @@ -1061,7 +1003,7 @@ impl futures::Stream for Streams {
self.page_request = Some(Box::pin(async move {
match context
.request(
"STREAM.LIST".to_string(),
T::subject(),
&json!({
"offset": offset,
}),
Expand All @@ -1080,6 +1022,7 @@ impl futures::Stream for Streams {
}
}
}

/// Used for building customized `publish` message.
#[derive(Default, Clone, Debug)]
pub struct Publish {
Expand Down