-
Notifications
You must be signed in to change notification settings - Fork 173
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
feat: add pause consumer #1234
Draft
yordis
wants to merge
1
commit into
nats-io:main
Choose a base branch
from
yordis:fixes-1218
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
feat: add pause consumer #1234
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -850,6 +850,64 @@ impl Stream { | |
} | ||
} | ||
|
||
/// Pause a [Consumer] until the given time. | ||
/// | ||
/// # Examples | ||
/// | ||
/// ```no_run | ||
/// # #[tokio::main] | ||
/// # async fn main() -> Result<(), async_nats::Error> { | ||
/// use async_nats::jetstream::consumer; | ||
/// use futures::StreamExt; | ||
/// let client = async_nats::connect("localhost:4222").await?; | ||
/// let jetstream = async_nats::jetstream::new(client); | ||
/// let pause_until = time::OffsetDateTime::now_utc() + time::Duration::from_secs(60); | ||
/// | ||
/// jetstream | ||
/// .get_stream("events") | ||
/// .await? | ||
/// .pause_consumer("my_consumer", pause_until) | ||
/// .await?; | ||
/// # Ok(()) | ||
/// # } | ||
/// ``` | ||
pub async fn pause_consumer(&self, name: &str, pause_until: OffsetDateTime) -> Result<PauseResponse, ConsumerError> { | ||
self.request_pause_consumer(name, Some(pause_until)).await | ||
} | ||
|
||
/// Resume a paused [Consumer]. | ||
/// | ||
/// # Examples | ||
/// | ||
/// ```no_run | ||
/// # #[tokio::main] | ||
/// # async fn main() -> Result<(), async_nats::Error> { | ||
/// use async_nats::jetstream::consumer; | ||
/// use futures::StreamExt; | ||
/// let client = async_nats::connect("localhost:4222").await?; | ||
/// let jetstream = async_nats::jetstream::new(client); | ||
/// | ||
/// jetstream | ||
/// .get_stream("events") | ||
/// .await? | ||
/// .resume_consumer("my_consumer") | ||
/// .await?; | ||
/// # Ok(()) | ||
/// # } | ||
/// ``` | ||
pub async fn resume_consumer(&self, name: &str) -> Result<PauseResponse, ConsumerError> { | ||
self.request_pause_consumer(name, None).await | ||
} | ||
|
||
async fn request_pause_consumer(&self, name: &str, pause_until: Option<OffsetDateTime>) -> Result<PauseResponse, ConsumerError> { | ||
let subject = format!("CONSUMER.PAUSE.{}.{}", self.info.config.name, name); | ||
let payload = &PauseConsumerRequest{ pause_until }; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would use literal |
||
match self.context.request(subject, payload).await? { | ||
Response::Ok::<PauseResponse>(resp) => { Ok(resp) } | ||
Response::Err { error } => Err(error.into()), | ||
} | ||
} | ||
|
||
/// Lists names of all consumers for current stream. | ||
/// | ||
/// # Examples | ||
|
@@ -1024,6 +1082,10 @@ pub struct Config { | |
/// Sets the first sequence for the stream. | ||
#[serde(default, skip_serializing_if = "Option::is_none", rename = "first_seq")] | ||
pub first_sequence: Option<u64>, | ||
|
||
/// PauseUntil is for suspending the consumer until the deadline. | ||
#[sende(with = "rfc3339")] | ||
pub pause_until: Option<OffsetDateTime>, | ||
} | ||
|
||
impl From<&Config> for Config { | ||
|
@@ -1167,6 +1229,21 @@ pub struct DeleteStatus { | |
pub success: bool, | ||
} | ||
|
||
#[derive(Deserialize)] | ||
pub struct PauseResponse { | ||
pub paused: bool, | ||
#[sende(with = "rfc3339")] | ||
pub pause_until: Option<OffsetDateTime>, | ||
#[sende(with = "serde_nanos")] | ||
pub pause_remaining: Option<Duration>, | ||
} | ||
|
||
#[derive(Serialize)] | ||
struct PauseConsumerRequest { | ||
#[serde(with = "rfc3339", skip_serializing_if = "Option::is_none")] | ||
pause_until: Option<OffsetDateTime>, | ||
} | ||
|
||
/// information about the given stream. | ||
#[derive(Debug, Deserialize, Clone, Copy)] | ||
pub struct State { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3475,6 +3475,7 @@ mod jetstream { | |
max_ack_pending: 150, | ||
}), | ||
first_sequence: Some(505), | ||
pause_until: None, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need to add test for pause/unpause. |
||
}; | ||
|
||
let stream = jetstream.create_stream(config.clone()).await.unwrap(); | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would add a note about what it means, briefly.