-
Notifications
You must be signed in to change notification settings - Fork 169
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
Direct get #636
Merged
Merged
Direct get #636
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
a4b3adb
direct wip
Jarema d347404
Add direct get methods
Jarema 2e476b7
Fix clippy
Jarema 5b438f8
Add docs to direct_get
Jarema 070d9b9
Simplify the API
Jarema fe01051
Merge remote-tracking branch 'origin/main' into jarema/direct-get
Jarema 9b54d2d
Fix conflicts
Jarema 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
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -14,19 +14,21 @@ | |||||
//! Manage operations on a [Stream], create/delete/update [Consumer][crate::jetstream::consumer::Consumer]. | ||||||
|
||||||
use std::{ | ||||||
fmt::Debug, | ||||||
io::{self, ErrorKind}, | ||||||
time::Duration, | ||||||
}; | ||||||
|
||||||
use crate::Error; | ||||||
use bytes::Bytes; | ||||||
use serde::{Deserialize, Serialize}; | ||||||
use serde_json::json; | ||||||
use time::serde::rfc3339; | ||||||
|
||||||
use super::{ | ||||||
consumer::{self, Consumer, FromConsumer, IntoConsumerConfig}, | ||||||
response::Response, | ||||||
Context, | ||||||
Context, Message, | ||||||
}; | ||||||
|
||||||
/// Handle to operations that can be performed on a `Stream`. | ||||||
|
@@ -95,6 +97,130 @@ impl Stream { | |||||
pub fn cached_info(&self) -> &Info { | ||||||
&self.info | ||||||
} | ||||||
|
||||||
pub async fn direct_get_next_for_subject_after_sequence( | ||||||
&self, | ||||||
subject: String, | ||||||
sequence: u64, | ||||||
) -> Result<Message, Error> { | ||||||
let request_subject = format!( | ||||||
"{}.DIRECT.GET.{}", | ||||||
&self.context.prefix, &self.info.config.name | ||||||
); | ||||||
let payload = json!({ | ||||||
"seq": sequence, | ||||||
"next_by_subj": subject, | ||||||
}); | ||||||
|
||||||
let response = self | ||||||
.context | ||||||
.client | ||||||
.request( | ||||||
request_subject, | ||||||
serde_json::to_vec(&payload).map(Bytes::from)?, | ||||||
) | ||||||
.await | ||||||
.map(|message| Message { | ||||||
message, | ||||||
context: self.context.clone(), | ||||||
})?; | ||||||
if let Some(status) = response.status { | ||||||
if let Some(ref description) = response.description { | ||||||
return Err(Box::from(std::io::Error::new( | ||||||
ErrorKind::Other, | ||||||
format!("{} {}", status, description), | ||||||
))); | ||||||
} | ||||||
} | ||||||
Ok(response) | ||||||
} | ||||||
pub async fn direct_get_next_for_subject(&self, subject: String) -> Result<Message, Error> { | ||||||
let request_subject = format!( | ||||||
"{}.DIRECT.GET.{}", | ||||||
&self.context.prefix, &self.info.config.name | ||||||
); | ||||||
let payload = json!({ | ||||||
"next_by_subj": subject, | ||||||
}); | ||||||
|
||||||
let response = self | ||||||
.context | ||||||
.client | ||||||
.request( | ||||||
request_subject, | ||||||
serde_json::to_vec(&payload).map(Bytes::from)?, | ||||||
) | ||||||
.await | ||||||
.map(|message| Message { | ||||||
message, | ||||||
context: self.context.clone(), | ||||||
})?; | ||||||
if let Some(status) = response.status { | ||||||
if let Some(ref description) = response.description { | ||||||
return Err(Box::from(std::io::Error::new( | ||||||
ErrorKind::Other, | ||||||
format!("{} {}", status, description), | ||||||
))); | ||||||
} | ||||||
} | ||||||
Ok(response) | ||||||
} | ||||||
|
||||||
pub async fn direct_get_by_sequence(&self, sequence: u64) -> Result<Message, Error> { | ||||||
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. Naming:
Suggested change
|
||||||
let subject = format!( | ||||||
"{}.DIRECT.GET.{}", | ||||||
&self.context.prefix, &self.info.config.name | ||||||
); | ||||||
let payload = json!({ | ||||||
"seq": sequence, | ||||||
}); | ||||||
|
||||||
let response = self | ||||||
.context | ||||||
.client | ||||||
.request(subject, serde_json::to_vec(&payload).map(Bytes::from)?) | ||||||
.await | ||||||
.map(|message| Message { | ||||||
context: self.context.clone(), | ||||||
message, | ||||||
})?; | ||||||
|
||||||
if let Some(status) = response.status { | ||||||
if let Some(ref description) = response.description { | ||||||
return Err(Box::from(std::io::Error::new( | ||||||
ErrorKind::Other, | ||||||
format!("{} {}", status, description), | ||||||
))); | ||||||
} | ||||||
} | ||||||
Ok(response) | ||||||
} | ||||||
|
||||||
pub async fn direct_get_last_for_subject(&self, subject: String) -> Result<Message, Error> { | ||||||
let subject = format!( | ||||||
"{}.DIRECT.GET.{}.{}", | ||||||
&self.context.prefix, &self.info.config.name, subject | ||||||
); | ||||||
|
||||||
let response = self | ||||||
.context | ||||||
.client | ||||||
.request(subject, "".into()) | ||||||
.await | ||||||
.map(|message| Message { | ||||||
context: self.context.clone(), | ||||||
message, | ||||||
})?; | ||||||
if let Some(status) = response.status { | ||||||
if let Some(ref description) = response.description { | ||||||
return Err(Box::from(std::io::Error::new( | ||||||
ErrorKind::Other, | ||||||
format!("{} {}", status, description), | ||||||
))); | ||||||
} | ||||||
} | ||||||
Ok(response) | ||||||
} | ||||||
/// Get a raw message from the stream. | ||||||
/// | ||||||
/// # Examples | ||||||
|
@@ -564,6 +690,9 @@ pub struct Config { | |||||
|
||||||
#[serde(default, skip_serializing_if = "is_default")] | ||||||
pub republish: Option<Republish>, | ||||||
|
||||||
#[serde(default, skip_serializing_if = "is_default")] | ||||||
pub allow_direct: bool, | ||||||
} | ||||||
|
||||||
impl From<&Config> for Config { | ||||||
|
Oops, something went wrong.
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.
Mirrors raw message API
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.
direct.get
->get_direct
is fine. The difference in API is though, that direct supports alsonext
, somessage_by_subject
is ambiguous enough, that user would have to read docs to be sure if its next or last.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.
Completely qualify it? e.g get_next_direct_message_by_subject
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 don't like phrasing
get....direct_message
. It's not a direct message. The name comes from the fact it can be ... fetched from any replica set member. Direct message does not make sense. Its Directly getting a normal Stream Message.