Skip to content

Commit

Permalink
Add Consumer::info and Consumer::cached_info
Browse files Browse the repository at this point in the history
Signed-off-by: Tomasz Pietrek <[email protected]>
  • Loading branch information
Jarema committed Jun 16, 2022
1 parent 0a653b9 commit 3a40fee
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 0 deletions.
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.clone();
Ok(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.clone()
}
}

/// Trait used to convert generic [Stream Config][crate::jetstream::consumer::Config] into either
/// [Pull][crate::jetstream::consumer::pull::Config] or
Expand Down
34 changes: 34 additions & 0 deletions async-nats/tests/jetstream_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -511,4 +511,38 @@ 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();
let info = consumer.cached_info().clone();
assert_eq!(consumer.info().await.unwrap(), consumer.cached_info());
}
}

0 comments on commit 3a40fee

Please sign in to comment.