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

Add Consumer::info and Consumer::cached_info #510

Merged
merged 1 commit into from
Jun 16, 2022
Merged
Show file tree
Hide file tree
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
67 changes: 67 additions & 0 deletions async-nats/src/jetstream/consumer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,14 @@

pub mod pull;
pub mod push;
use std::io::ErrorKind;
use std::time::Duration;

use serde::{Deserialize, Serialize};
use serde_json::json;
use time::serde::rfc3339;

use super::response::Response;
use super::Context;
use crate::jetstream::consumer;
use crate::Error;
Expand All @@ -43,6 +46,70 @@ impl<T: IntoConsumerConfig> Consumer<T> {
}
}
}
impl<T: IntoConsumerConfig> Consumer<T> {
/// Retrieves `info` about [Consumer] from the server, updates the cached `info` inside
/// [Consumer] and returns it.
///
/// # Examples:
///
/// ```no_run
/// # #[tokio::main]
/// # async fn main() -> Result<(), async_nats::Error> {
/// use async_nats::jetstream::consumer::PullConsumer;
/// let client = async_nats::connect("localhost:4222").await?;
/// let jetstream = async_nats::jetstream::new(client);
///
/// let mut consumer: PullConsumer = jetstream
/// .get_stream("events").await?
/// .get_consumer("pull").await?;
///
/// let info = consumer.info().await?;
/// # Ok(())
/// # }
/// ```
pub async fn info(&mut self) -> Result<&consumer::Info, Error> {
let subject = format!("CONSUMER.INFO.{}.{}", self.info.stream_name, self.info.name);

match self.context.request(subject, &json!({})).await? {
Response::Ok::<Info>(info) => {
self.info = info;
Ok(&self.info)
}
Response::Err { error } => Err(Box::new(std::io::Error::new(
ErrorKind::Other,
format!(
"nats: error while getting consumer info: {}, {}, {}",
error.code, error.status, error.description
),
))),
}
}

/// Returns cached [Info] for the [Consumer].
/// Cache is either from initial creation/retrival of the [Consumer] or last call to
/// [Consumer::info].
///
/// # Examples:
///
/// ```no_run
/// # #[tokio::main]
/// # async fn main() -> Result<(), async_nats::Error> {
/// use async_nats::jetstream::consumer::PullConsumer;
/// let client = async_nats::connect("localhost:4222").await?;
/// let jetstream = async_nats::jetstream::new(client);
///
/// let consumer: PullConsumer = jetstream
/// .get_stream("events").await?
/// .get_consumer("pull").await?;
///
/// let info = consumer.cached_info();
/// # Ok(())
/// # }
/// ```
pub fn cached_info(&self) -> &consumer::Info {
&self.info
}
}

/// Trait used to convert generic [Stream Config][crate::jetstream::consumer::Config] into either
/// [Pull][crate::jetstream::consumer::pull::Config] or
Expand Down
36 changes: 36 additions & 0 deletions async-nats/tests/jetstream_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -511,4 +511,40 @@ mod jetstream {
}
}
}

#[tokio::test]
async fn consumer_info() {
let server = nats_server::run_server("tests/configs/jetstream.conf");
let client = ConnectOptions::new()
.error_callback(|err| async move { println!("error: {:?}", err) })
.connect(server.client_url())
.await
.unwrap();

let context = async_nats::jetstream::new(client);

context
.create_stream(stream::Config {
name: "events".to_string(),
subjects: vec!["events".to_string()],
..Default::default()
})
.await
.unwrap();

let stream = context.get_stream("events").await.unwrap();
stream
.create_consumer(&Config {
durable_name: Some("pull".to_string()),
..Default::default()
})
.await
.unwrap();

let mut consumer: PullConsumer = stream.get_consumer("pull").await.unwrap();
assert_eq!(
consumer.info().await.unwrap().clone(),
consumer.cached_info().clone()
);
}
}