-
Notifications
You must be signed in to change notification settings - Fork 120
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
219 additions
and
12 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
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 |
---|---|---|
@@ -0,0 +1,126 @@ | ||
use tokio::net::TcpSocket; | ||
|
||
use crate::configs::tcp::TcpSocketConfig; | ||
|
||
pub fn build(config: TcpSocketConfig) -> TcpSocket { | ||
let socket = if config.ipv6 { | ||
TcpSocket::new_v6().expect("Unable to create an ipv6 socket") | ||
} else { | ||
TcpSocket::new_v4().expect("Unable to create an ipv4 socket") | ||
}; | ||
|
||
if let Some(size) = config.recv_buffer_size { | ||
socket | ||
.set_recv_buffer_size(size) | ||
.expect("Unable to set SO_RCVBUF on socket"); | ||
} | ||
if let Some(size) = config.send_buffer_size { | ||
socket | ||
.set_send_buffer_size(size) | ||
.expect("Unable to set SO_SNDBUF on socket"); | ||
} | ||
if let Some(keepalive) = config.keepalive { | ||
socket | ||
.set_keepalive(keepalive) | ||
.expect("Unable to set SO_KEEPALIVE on socket"); | ||
} | ||
if let Some(nodelay) = config.nodelay { | ||
socket | ||
.set_nodelay(nodelay) | ||
.expect("Unable to set TCP_NODELAY on socket"); | ||
} | ||
if let Some(linger) = config.linger { | ||
socket | ||
.set_linger(Some(linger.get_duration())) | ||
.expect("Unable to set SO_LINGER on socket"); | ||
} | ||
|
||
socket | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use std::time::Duration; | ||
|
||
use iggy::utils::duration::IggyDuration; | ||
|
||
use super::*; | ||
|
||
#[test] | ||
fn given_recv_config_socket_should_be_configured() { | ||
let recv_buffer_size = 425984; | ||
let config = TcpSocketConfig { | ||
ipv6: true, | ||
recv_buffer_size: Some(recv_buffer_size), | ||
send_buffer_size: None, | ||
keepalive: None, | ||
nodelay: None, | ||
linger: None, | ||
}; | ||
let socket = build(config); | ||
let recv = socket.recv_buffer_size().unwrap(); | ||
assert_eq!(recv, recv_buffer_size); | ||
} | ||
|
||
#[test] | ||
fn given_send_config_socket_should_be_configured() { | ||
let send_buffer_size = 425984; | ||
let config = TcpSocketConfig { | ||
ipv6: true, | ||
recv_buffer_size: None, | ||
send_buffer_size: Some(send_buffer_size), | ||
keepalive: None, | ||
nodelay: None, | ||
linger: None, | ||
}; | ||
let socket = build(config); | ||
let send = socket.send_buffer_size().unwrap(); | ||
assert_eq!(send, send_buffer_size); | ||
} | ||
|
||
#[test] | ||
fn given_keepalive_config_socket_should_be_configured() { | ||
let config = TcpSocketConfig { | ||
ipv6: true, | ||
recv_buffer_size: None, | ||
send_buffer_size: None, | ||
keepalive: Some(true), | ||
nodelay: None, | ||
linger: None, | ||
}; | ||
let socket = build(config); | ||
let keepalive = socket.keepalive().unwrap(); | ||
assert!(keepalive); | ||
} | ||
|
||
#[test] | ||
fn given_nodelay_config_socket_should_be_configured() { | ||
let config = TcpSocketConfig { | ||
ipv6: true, | ||
recv_buffer_size: None, | ||
send_buffer_size: None, | ||
keepalive: None, | ||
nodelay: Some(true), | ||
linger: None, | ||
}; | ||
let socket = build(config); | ||
let nodelay = socket.nodelay().unwrap(); | ||
assert!(nodelay); | ||
} | ||
|
||
#[test] | ||
fn given_linger_config_socket_should_be_configured() { | ||
let linger_dur = Duration::new(1, 0); | ||
let config = TcpSocketConfig { | ||
ipv6: true, | ||
recv_buffer_size: None, | ||
send_buffer_size: None, | ||
keepalive: None, | ||
nodelay: None, | ||
linger: Some(IggyDuration::new(linger_dur)), | ||
}; | ||
let socket = build(config); | ||
let linger = socket.linger().unwrap(); | ||
assert_eq!(linger, Some(linger_dur)); | ||
} | ||
} |
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