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

Support runtime Topic designation #44

Merged
merged 1 commit into from
Nov 8, 2023
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
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "hedwig"
version = "6.3.0"
version = "6.4.0"
authors = [
"Aniruddha Maru <[email protected]>",
"Simonas Kazlauskas <[email protected]>",
Expand Down Expand Up @@ -39,6 +39,7 @@ bytes = "1"
either = { version = "1", features = ["use_std"], default-features = false }
futures-util = { version = "0.3.17", features = ["std", "sink"], default-features = false }
pin-project = "1"
smallstr = { version = "0.3.0", features = ["union"] }
thiserror = { version = "1", default-features = false }
url = { version = "2", default-features = false }
uuid = { version = "^0.8", features = ["v4"], default-features = false }
Expand Down
23 changes: 14 additions & 9 deletions src/backends/googlepubsub/publisher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -374,16 +374,21 @@ where
let sink = {
let retry_policy = this.retry_policy;
let response_sink = this.response_sink;
this.topic_sinks.entry(topic.clone()).or_insert_with(|| {
Box::pin(TopicSink::new(
client.client.publish_topic_sink(
TopicName::new(topic.as_ref())
.into_project_topic_name(client.project()),

// avoid cloning the topic if the key exists
match this.topic_sinks.get_mut(&topic) {
Some(existing) => existing,
None => this.topic_sinks.entry(topic.clone()).or_insert(Box::pin(
TopicSink::new(
client.client.publish_topic_sink(
TopicName::new(topic.as_ref())
.into_project_topic_name(client.project()),
Copy link
Collaborator

Choose a reason for hiding this comment

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

This particular line is what I think needs to change for me to send messages across various projects. Though you might be wanting to address that in a separate PR.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

yes, different PR

),
retry_policy.clone(),
Shared::clone(response_sink),
),
retry_policy.clone(),
Shared::clone(response_sink),
))
})
)),
}
};

// poll the sink to see if it's ready
Expand Down
29 changes: 22 additions & 7 deletions src/topic.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,36 @@
use smallstr::SmallString;

/// A message queue topic name to which messages can be published
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
pub struct Topic(&'static str);
// A survey of common topics found lengths between 16 and 35 bytes
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Topic(SmallString<[u8; 36]>);
Copy link
Collaborator

Choose a reason for hiding this comment

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

I did run into this problem, since I am creating topic names dynamically - but worked around it by leaking the strings and then using a hashmap to cache the mapping from (site id, prio) -> &'static str

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I guess there's a reasonable bound on that combination, so that's not the worst. But we can do better!

Copy link
Collaborator

Choose a reason for hiding this comment

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

is this saying we can support any string up to 36Bytes? I wonder if any of our topics exceed that? Or is this similar to a stackvec and will falback to heap allocation if we exceed that threshold?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It's backed by SmallVec, which yeah does the fallback to heap


impl Default for Topic {
fn default() -> Self {
Topic(SmallString::new())
}
}

impl std::fmt::Display for Topic {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
std::fmt::Display::fmt(self.0, f)
std::fmt::Display::fmt(self.0.as_str(), f)
}
}

impl<'a> From<&'a str> for Topic {
fn from(s: &'a str) -> Topic {
Topic(s.into())
}
}

impl From<&'static str> for Topic {
fn from(s: &'static str) -> Topic {
Topic(s)
impl From<String> for Topic {
fn from(s: String) -> Topic {
Topic(s.into())
}
}

impl AsRef<str> for Topic {
fn as_ref(&self) -> &str {
self.0
self.0.as_ref()
}
}
Loading