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

Poll error callbacks #669

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "rdkafka"
version = "0.36.2"
version = "0.36.3"
authors = ["Federico Giraud <[email protected]>"]
repository = "https://github.com/fede1024/rust-rdkafka"
readme = "README.md"
Expand Down
3 changes: 3 additions & 0 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,8 @@ impl<T> From<EventPollResult<T>> for Option<T> {
pub struct Client<C: ClientContext = DefaultClientContext> {
native: NativeClient,
context: Arc<C>,
/// A poll error callback for main queue
pub queue_poll_error_cb: Option<fn(String)>,
}

impl<C: ClientContext> Client<C> {
Expand Down Expand Up @@ -282,6 +284,7 @@ impl<C: ClientContext> Client<C> {
Ok(Client {
native: unsafe { NativeClient::from_ptr(client_ptr) },
context,
queue_poll_error_cb: config.queue_poll_error_cb,
})
}

Expand Down
9 changes: 9 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,8 @@ pub struct ClientConfig {
/// The librdkafka logging level. Refer to [`RDKafkaLogLevel`] for the list
/// of available levels.
pub log_level: RDKafkaLogLevel,
/// Poll error callback
pub queue_poll_error_cb: Option<fn(String)>,
}

impl Default for ClientConfig {
Expand All @@ -201,6 +203,7 @@ impl ClientConfig {
ClientConfig {
conf_map: HashMap::new(),
log_level: log_level_from_global_config(),
queue_poll_error_cb: None,
}
}

Expand Down Expand Up @@ -248,6 +251,12 @@ impl ClientConfig {
self
}

/// Sets the error callback for poll method.
pub fn set_queue_poll_error_cb(&mut self, error_cb: fn(String)) -> &mut ClientConfig {
self.queue_poll_error_cb = Some(error_cb);
self
}

/// Builds a native librdkafka configuration.
pub fn create_native_config(&self) -> KafkaResult<NativeClientConfig> {
let conf = unsafe { NativeClientConfig::from_ptr(rdsys::rd_kafka_conf_new()) };
Expand Down
8 changes: 8 additions & 0 deletions src/producer/base_producer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,14 @@ where
let evtype = unsafe { rdsys::rd_kafka_event_type(ev.ptr()) };
match evtype {
rdsys::RD_KAFKA_EVENT_DR => self.handle_delivery_report_event(ev),
rdsys::RD_KAFKA_EVENT_ERROR if self.client.queue_poll_error_cb.is_some() => {
let queue_poll_error_cb = self.client.queue_poll_error_cb.unwrap();
let event_error_str = unsafe {
let event_error_str = rdsys::rd_kafka_event_error_string(ev.ptr());
CStr::from_ptr(event_error_str).to_string_lossy()
};
queue_poll_error_cb(String::from(event_error_str))
},
_ => {
let evname = unsafe {
let evname = rdsys::rd_kafka_event_name(ev.ptr());
Expand Down