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

[WIP] Feat: Support TCP Keep-alive options #156

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
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/socket_client_with_options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<dyn Error>> {
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::default()
.set_idle(1)
.set_count(5)
.set_interval(10),
);

let mut socket = zeromq::ReqSocket::with_options(options);
socket
Expand Down
8 changes: 7 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -129,13 +129,19 @@ pub enum SocketEvent {
#[derive(Default)]
pub struct SocketOptions {
pub(crate) peer_id: Option<PeerIdentity>,
pub(crate) tcp_keepalive: Option<TcpKeepalive>,
}

impl SocketOptions {
pub fn peer_identity(&mut self, peer_id: PeerIdentity) -> &mut Self {
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]
Expand Down
44 changes: 44 additions & 0 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,50 @@ pub(crate) async fn connect_forever(endpoint: Endpoint) -> ZmqResult<(FramedIo,
}
}

#[derive(PartialEq, Eq, PartialOrd, Ord, Debug, Hash, Clone)]
pub struct TcpKeepalive {
pub idle: Option<u64>,
pub count: Option<u64>,
pub interval: Option<u64>,
}

impl TcpKeepalive {
pub const fn new() -> Self {
TcpKeepalive {
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
}
}
}

impl Default for TcpKeepalive {
fn default() -> Self {
Self::new()
}
}

#[cfg(test)]
pub(crate) mod tests {
use super::*;
Expand Down