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

MAIN: Add read/write timeout options for ClientAssociation #530

Merged
merged 2 commits into from
Jul 5, 2024
Merged
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
45 changes: 43 additions & 2 deletions ul/src/association/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::{
borrow::Cow,
convert::TryInto,
io::Write,
net::{TcpStream, ToSocketAddrs},
net::{TcpStream, ToSocketAddrs}, time::Duration,
};

use crate::{
Expand Down Expand Up @@ -39,6 +39,18 @@ pub enum Error {
source: std::io::Error,
backtrace: Backtrace,
},

/// Could not set tcp read timeout
SetReadTimeout{
source: std::io::Error,
backtrace: Backtrace,
},

/// Could not set tcp write timeout
SetWriteTimeout{
source: std::io::Error,
backtrace: Backtrace,
},

/// failed to send association request
SendRequest {
Expand Down Expand Up @@ -177,6 +189,10 @@ pub struct ClientAssociationOptions<'a> {
saml_assertion: Option<Cow<'a, str>>,
/// User identity JWT
jwt: Option<Cow<'a, str>>,
/// TCP read timeout
read_timeout: Option<Duration>,
/// TCP write timeout
write_timeout: Option<Duration>,
}

impl<'a> Default for ClientAssociationOptions<'a> {
Expand All @@ -198,6 +214,8 @@ impl<'a> Default for ClientAssociationOptions<'a> {
kerberos_service_ticket: None,
saml_assertion: None,
jwt: None,
read_timeout: None,
write_timeout: None,
}
}
}
Expand Down Expand Up @@ -431,6 +449,22 @@ impl<'a> ClientAssociationOptions<'a> {
}
}

/// Set the read timeout for the underlying TCP socket
pub fn read_timeout(self, timeout: Duration) -> Self {
Self {
read_timeout: Some(timeout),
..self
}
}

/// Set the write timeout for the underlying TCP socket
pub fn write_timeout(self, timeout: Duration) -> Self {
Self {
write_timeout: Some(timeout),
..self
}
}

fn establish_impl<T>(self, ae_address: AeAddr<T>) -> Result<ClientAssociation>
where
T: ToSocketAddrs,
Expand All @@ -448,6 +482,8 @@ impl<'a> ClientAssociationOptions<'a> {
kerberos_service_ticket,
saml_assertion,
jwt,
read_timeout,
write_timeout
} = self;

// fail if no presentation contexts were provided: they represent intent,
Expand Down Expand Up @@ -510,7 +546,12 @@ impl<'a> ClientAssociationOptions<'a> {
user_variables,
});

let mut socket = std::net::TcpStream::connect(ae_address).context(ConnectSnafu)?;
let mut socket = std::net::TcpStream::connect(ae_address)
.context(ConnectSnafu)?;
socket.set_read_timeout(read_timeout)
.context(SetReadTimeoutSnafu)?;
socket.set_write_timeout(write_timeout)
.context(SetWriteTimeoutSnafu)?;
let mut buffer: Vec<u8> = Vec::with_capacity(max_pdu_length as usize);
// send request

Expand Down
Loading