-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from antonilol/reuse_socket
use 1 socket to subscribe to multiple endpoints
- Loading branch information
Showing
10 changed files
with
103 additions
and
122 deletions.
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
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
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
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
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
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
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 |
---|---|---|
@@ -1,63 +1,52 @@ | ||
use super::{new_socket_internal, subscribe_internal}; | ||
use crate::{error::Result, message::Message}; | ||
use core::{convert::Infallible, ops::ControlFlow}; | ||
use std::{sync::mpsc::channel, thread}; | ||
use zmq::Context; | ||
|
||
fn break_on_err(is_err: bool) -> ControlFlow<()> { | ||
if is_err { | ||
ControlFlow::Break(()) | ||
} else { | ||
ControlFlow::Continue(()) | ||
} | ||
} | ||
|
||
/// Subscribes to a single ZMQ endpoint and blocks the thread until [`ControlFlow::Break`] is | ||
/// returned by the callback. | ||
#[inline] | ||
#[deprecated( | ||
since = "1.3.2", | ||
note = "Use subscribe_blocking. This function has no performance benefit over subscribe_multi_blocking anymore." | ||
)] | ||
pub fn subscribe_single_blocking<F, B>( | ||
endpoint: &str, | ||
callback: F, | ||
) -> Result<ControlFlow<B, Infallible>> | ||
where | ||
F: Fn(Result<Message>) -> ControlFlow<B>, | ||
{ | ||
let context = Context::new(); | ||
|
||
let socket = new_socket_internal(&context, endpoint)?; | ||
|
||
Ok(subscribe_internal(socket, callback)) | ||
subscribe_blocking(&[endpoint], callback) | ||
} | ||
|
||
/// Subscribes to multiple ZMQ endpoints and blocks the thread until [`ControlFlow::Break`] is | ||
/// returned by the callback. | ||
#[inline] | ||
#[deprecated( | ||
since = "1.3.2", | ||
note = "Use subscribe_blocking. The name changed because there is no distinction made anymore between subscribing to 1 or more endpoints." | ||
)] | ||
pub fn subscribe_multi_blocking<F, B>( | ||
endpoints: &[&str], | ||
callback: F, | ||
) -> Result<ControlFlow<B, Infallible>> | ||
where | ||
F: Fn(Result<Message>) -> ControlFlow<B>, | ||
{ | ||
let (tx, rx) = channel(); | ||
let context = Context::new(); | ||
|
||
for endpoint in endpoints { | ||
let tx = tx.clone(); | ||
|
||
let socket = new_socket_internal(&context, endpoint)?; | ||
|
||
thread::spawn(move || { | ||
subscribe_internal(socket, |msg| break_on_err(tx.send(msg).is_err())) | ||
}); | ||
} | ||
subscribe_blocking(endpoints, callback) | ||
} | ||
|
||
Ok((|| { | ||
for msg in rx { | ||
callback(msg)?; | ||
} | ||
/// Subscribes to multiple ZMQ endpoints and blocks the thread until [`ControlFlow::Break`] is | ||
/// returned by the callback. | ||
#[inline] | ||
pub fn subscribe_blocking<F, B>( | ||
endpoints: &[&str], | ||
callback: F, | ||
) -> Result<ControlFlow<B, Infallible>> | ||
where | ||
F: Fn(Result<Message>) -> ControlFlow<B>, | ||
{ | ||
let (_context, socket) = new_socket_internal(endpoints)?; | ||
|
||
// `tx` is dropped at the end of this function | ||
unreachable!(); | ||
})()) | ||
Ok(subscribe_internal(socket, callback)) | ||
} |
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
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
Oops, something went wrong.