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 ordering key for pubsub #716

Merged
merged 2 commits into from
Nov 1, 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: 8 additions & 2 deletions src/sinks/gcp_pubsub/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,15 @@ use crate::{
utils::{retry, Utils},
};

async fn send_pubsub_msg(publisher: &Publisher, event: &Event) -> Result<(), crate::Error> {
async fn send_pubsub_msg(
publisher: &Publisher,
event: &Event,
ordering_key: &str,
) -> Result<(), crate::Error> {
let body = json!(event).to_string();
let msg = PubsubMessage {
data: body.into(),
ordering_key: ordering_key.into(),
..Default::default()
};

Expand All @@ -37,6 +42,7 @@ pub fn writer_loop(
topic_name: &str,
error_policy: &ErrorPolicy,
retry_policy: &retry::Policy,
ordering_key: &str,
utils: Arc<Utils>,
) -> Result<(), crate::Error> {
let rt = tokio::runtime::Builder::new_current_thread()
Expand All @@ -52,7 +58,7 @@ pub fn writer_loop(

for event in input.iter() {
let result = retry::retry_operation(
|| rt.block_on(send_pubsub_msg(&publisher, &event)),
|| rt.block_on(send_pubsub_msg(&publisher, &event, ordering_key)),
retry_policy,
);

Expand Down
17 changes: 15 additions & 2 deletions src/sinks/gcp_pubsub/setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub struct Config {
pub topic: String,
pub error_policy: Option<ErrorPolicy>,
pub retry_policy: Option<retry::Policy>,
pub ordering_key: Option<String>,

#[warn(deprecated)]
pub credentials: Option<String>,
Expand All @@ -30,12 +31,24 @@ impl SinkProvider for WithUtils<Config> {
.unwrap_or(ErrorPolicy::Exit);

let retry_policy = self.inner.retry_policy.unwrap_or_default();
let ordering_key = self
.inner
.ordering_key
.to_owned()
.unwrap_or_default();

let utils = self.utils.clone();

let handle = std::thread::spawn(move || {
writer_loop(input, &topic_name, &error_policy, &retry_policy, utils)
.expect("writer loop failed");
writer_loop(
input,
&topic_name,
&error_policy,
&retry_policy,
&ordering_key,
utils,
)
.expect("writer loop failed");
});

Ok(handle)
Expand Down