From e51b55578f73a6c0e22e1f474185c085c0adabb0 Mon Sep 17 00:00:00 2001 From: John Law Date: Tue, 19 Apr 2022 17:01:16 +0000 Subject: [PATCH 1/2] feat: add basic tcp_keepalive option --- examples/socket_client_with_options.rs | 11 +++++++++-- src/lib.rs | 8 +++++++- src/util.rs | 25 +++++++++++++++++++++++++ 3 files changed, 41 insertions(+), 3 deletions(-) diff --git a/examples/socket_client_with_options.rs b/examples/socket_client_with_options.rs index 065cd5c..58b8a49 100644 --- a/examples/socket_client_with_options.rs +++ b/examples/socket_client_with_options.rs @@ -2,13 +2,20 @@ mod async_helpers; use std::convert::TryFrom; use std::error::Error; -use zeromq::util::PeerIdentity; +use zeromq::util::{PeerIdentity, TcpKeepalive}; use zeromq::{Socket, SocketOptions, SocketRecv, SocketSend}; #[async_helpers::main] async fn main() -> Result<(), Box> { let mut options = SocketOptions::default(); - options.peer_identity(PeerIdentity::try_from(Vec::from("SomeCustomId")).unwrap()); + options + .peer_identity(PeerIdentity::try_from(Vec::from("SomeCustomId")).unwrap()) + .tcp_keepalive(TcpKeepalive { + keepalive: 1, + count: 5, + idle: 1, + interval: 10, + }); let mut socket = zeromq::ReqSocket::with_options(options); socket diff --git a/src/lib.rs b/src/lib.rs index 8a3b8e1..50152ea 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -39,7 +39,7 @@ pub use message::*; use crate::codec::*; use crate::transport::AcceptStopHandle; -use util::PeerIdentity; +use util::{PeerIdentity, TcpKeepalive}; #[macro_use] extern crate enum_primitive_derive; @@ -129,6 +129,7 @@ pub enum SocketEvent { #[derive(Default)] pub struct SocketOptions { pub(crate) peer_id: Option, + pub(crate) tcp_keepalive: Option, } impl SocketOptions { @@ -136,6 +137,11 @@ impl SocketOptions { self.peer_id = Some(peer_id); self } + + pub fn tcp_keepalive(&mut self, tcp_keepalive_opt: TcpKeepalive) -> &mut Self { + self.tcp_keepalive = Some(tcp_keepalive_opt); + self + } } #[async_trait] diff --git a/src/util.rs b/src/util.rs index a5c3a3c..f4b25d4 100644 --- a/src/util.rs +++ b/src/util.rs @@ -216,6 +216,31 @@ pub(crate) async fn connect_forever(endpoint: Endpoint) -> ZmqResult<(FramedIo, } } +#[derive(PartialEq, Eq, PartialOrd, Ord, Debug, Hash, Clone)] +pub struct TcpKeepalive { + pub keepalive: i64, + pub idle: i64, + pub count: i64, + pub interval: i64, +} + +impl TcpKeepalive { + pub fn new() -> Self { + TcpKeepalive { + keepalive: -1, + idle: -1, + count: -1, + interval: -1, + } + } +} + +impl Default for TcpKeepalive { + fn default() -> Self { + Self::new() + } +} + #[cfg(test)] pub(crate) mod tests { use super::*; From 19fae3fc311e430c9fc8d186ea3066f6871fd80f Mon Sep 17 00:00:00 2001 From: John Law Date: Sat, 30 Apr 2022 23:13:49 +0000 Subject: [PATCH 2/2] fix: improve interface --- examples/socket_client_with_options.rs | 12 ++++----- src/util.rs | 37 +++++++++++++++++++------- 2 files changed, 34 insertions(+), 15 deletions(-) diff --git a/examples/socket_client_with_options.rs b/examples/socket_client_with_options.rs index 58b8a49..1dda861 100644 --- a/examples/socket_client_with_options.rs +++ b/examples/socket_client_with_options.rs @@ -10,12 +10,12 @@ async fn main() -> Result<(), Box> { let mut options = SocketOptions::default(); options .peer_identity(PeerIdentity::try_from(Vec::from("SomeCustomId")).unwrap()) - .tcp_keepalive(TcpKeepalive { - keepalive: 1, - count: 5, - idle: 1, - interval: 10, - }); + .tcp_keepalive( + TcpKeepalive::default() + .set_idle(1) + .set_count(5) + .set_interval(10), + ); let mut socket = zeromq::ReqSocket::with_options(options); socket diff --git a/src/util.rs b/src/util.rs index f4b25d4..a5703fb 100644 --- a/src/util.rs +++ b/src/util.rs @@ -218,19 +218,38 @@ pub(crate) async fn connect_forever(endpoint: Endpoint) -> ZmqResult<(FramedIo, #[derive(PartialEq, Eq, PartialOrd, Ord, Debug, Hash, Clone)] pub struct TcpKeepalive { - pub keepalive: i64, - pub idle: i64, - pub count: i64, - pub interval: i64, + pub idle: Option, + pub count: Option, + pub interval: Option, } impl TcpKeepalive { - pub fn new() -> Self { + pub const fn new() -> Self { TcpKeepalive { - keepalive: -1, - idle: -1, - count: -1, - interval: -1, + idle: None, + count: None, + interval: None, + } + } + + pub const fn set_idle(self, idle: u64) -> Self { + Self { + idle: Some(idle), + ..self + } + } + + pub const fn set_count(self, count: u64) -> Self { + Self { + count: Some(count), + ..self + } + } + + pub const fn set_interval(self, interval: u64) -> Self { + Self { + interval: Some(interval), + ..self } } }