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

Implement Sync on OwnedHeaders #203

Merged
merged 1 commit into from
Dec 31, 2019
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
11 changes: 9 additions & 2 deletions examples/asynchronous_processing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,18 @@ use crate::example_utils::setup_logger;

mod example_utils;

async fn record_message_receipt(msg: &BorrowedMessage<'_>) {
async fn record_borrowed_message_receipt(msg: &BorrowedMessage<'_>) {
// Simulate some work that must be done in the same order as messages are
// received; i.e., before truly parallel processing can begin.
info!("Message received: {}", msg.offset());
}

async fn record_owned_message_receipt(msg: &OwnedMessage) {
// Like `record_borrowed_message_receipt`, but takes an `OwnedMessage`
// instead, as in a real-world use case an `OwnedMessage` might be more
// convenient than a `BorrowedMessage`.
}

// Emulates an expensive, synchronous computation.
fn expensive_computation<'a>(msg: OwnedMessage) -> String {
info!("Starting expensive computation on message {}", msg.offset());
Expand Down Expand Up @@ -79,10 +85,11 @@ async fn run_async_processor(
let output_topic = output_topic.to_string();
async move {
// Process each message
record_message_receipt(&borrowed_message).await;
record_borrowed_message_receipt(&borrowed_message).await;
// Borrowed messages can't outlive the consumer they are received from, so they need to
// be owned in order to be sent to a separate thread.
let owned_message = borrowed_message.detach();
record_owned_message_receipt(&owned_message).await;
tokio::spawn(async move {
// The body of this block will be executed on the main thread pool,
// but we perform `expensive_computation` on a separate thread pool
Expand Down
1 change: 1 addition & 0 deletions src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,7 @@ pub struct OwnedHeaders {
}

unsafe impl Send for OwnedHeaders {}
unsafe impl Sync for OwnedHeaders {}

impl OwnedHeaders {
/// Create a new `OwnedHeaders` struct with initial capacity 5.
Expand Down