Switch timeout APIs to use Duration.
This commit is contained in:
committed by
Martin Algesten
parent
00e3294ac1
commit
257d4e54dd
@@ -39,9 +39,9 @@ pub struct Request {
|
||||
// from request itself
|
||||
pub(crate) headers: Vec<Header>,
|
||||
pub(crate) query: QString,
|
||||
pub(crate) timeout_connect: u64,
|
||||
pub(crate) timeout_read: u64,
|
||||
pub(crate) timeout_write: u64,
|
||||
pub(crate) timeout_connect: Option<time::Duration>,
|
||||
pub(crate) timeout_read: Option<time::Duration>,
|
||||
pub(crate) timeout_write: Option<time::Duration>,
|
||||
pub(crate) timeout: Option<time::Duration>,
|
||||
pub(crate) redirects: u32,
|
||||
pub(crate) proxy: Option<Proxy>,
|
||||
@@ -99,7 +99,7 @@ impl Request {
|
||||
///
|
||||
/// ```
|
||||
/// let r = ureq::get("/my_page")
|
||||
/// .timeout_connect(10_000) // max 10 seconds
|
||||
/// .timeout_connect(std::time::Duration::from_secs(10)) // max 10 seconds
|
||||
/// .call();
|
||||
///
|
||||
/// println!("{:?}", r);
|
||||
@@ -346,12 +346,12 @@ impl Request {
|
||||
///
|
||||
/// ```
|
||||
/// let r = ureq::get("/my_page")
|
||||
/// .timeout_connect(1_000) // wait max 1 second to connect
|
||||
/// .timeout(std::time::Duration::from_secs(1))
|
||||
/// .call();
|
||||
/// println!("{:?}", r);
|
||||
/// ```
|
||||
pub fn timeout_connect(&mut self, millis: u64) -> &mut Request {
|
||||
self.timeout_connect = millis;
|
||||
pub fn timeout_connect(&mut self, timeout: time::Duration) -> &mut Request {
|
||||
self.timeout_connect = Some(timeout);
|
||||
self
|
||||
}
|
||||
|
||||
@@ -363,12 +363,12 @@ impl Request {
|
||||
///
|
||||
/// ```
|
||||
/// let r = ureq::get("/my_page")
|
||||
/// .timeout_read(1_000) // wait max 1 second for the read
|
||||
/// .timeout(std::time::Duration::from_secs(1))
|
||||
/// .call();
|
||||
/// println!("{:?}", r);
|
||||
/// ```
|
||||
pub fn timeout_read(&mut self, millis: u64) -> &mut Request {
|
||||
self.timeout_read = millis;
|
||||
pub fn timeout_read(&mut self, timeout: time::Duration) -> &mut Request {
|
||||
self.timeout_read = Some(timeout);
|
||||
self
|
||||
}
|
||||
|
||||
@@ -380,12 +380,12 @@ impl Request {
|
||||
///
|
||||
/// ```
|
||||
/// let r = ureq::get("/my_page")
|
||||
/// .timeout_write(1_000) // wait max 1 second for sending.
|
||||
/// .timeout(std::time::Duration::from_secs(1))
|
||||
/// .call();
|
||||
/// println!("{:?}", r);
|
||||
/// ```
|
||||
pub fn timeout_write(&mut self, millis: u64) -> &mut Request {
|
||||
self.timeout_write = millis;
|
||||
pub fn timeout_write(&mut self, timeout: time::Duration) -> &mut Request {
|
||||
self.timeout_write = Some(timeout);
|
||||
self
|
||||
}
|
||||
|
||||
|
||||
@@ -379,8 +379,8 @@ pub(crate) fn connect_https(unit: &Unit, hostname: &str) -> Result<Stream, Error
|
||||
}
|
||||
|
||||
pub(crate) fn connect_host(unit: &Unit, hostname: &str, port: u16) -> Result<TcpStream, Error> {
|
||||
let deadline: Option<Instant> = if unit.req.timeout_connect > 0 {
|
||||
Instant::now().checked_add(Duration::from_millis(unit.req.timeout_connect))
|
||||
let deadline: Option<Instant> = if let Some(timeout_connect) = unit.req.timeout_connect {
|
||||
Instant::now().checked_add(timeout_connect)
|
||||
} else {
|
||||
unit.deadline
|
||||
};
|
||||
@@ -447,30 +447,20 @@ pub(crate) fn connect_host(unit: &Unit, hostname: &str, port: u16) -> Result<Tcp
|
||||
return Err(err);
|
||||
};
|
||||
|
||||
// rust's absurd api returns Err if we set 0.
|
||||
// Setting it to None will disable the native system timeout
|
||||
if let Some(deadline) = deadline {
|
||||
stream
|
||||
.set_read_timeout(Some(time_until_deadline(deadline)?))
|
||||
.ok();
|
||||
} else if unit.req.timeout_read > 0 {
|
||||
stream
|
||||
.set_read_timeout(Some(Duration::from_millis(unit.req.timeout_read as u64)))
|
||||
.ok();
|
||||
stream.set_read_timeout(Some(time_until_deadline(deadline)?))?;
|
||||
} else if let Some(timeout_read) = unit.req.timeout_read {
|
||||
stream.set_read_timeout(Some(timeout_read))?;
|
||||
} else {
|
||||
stream.set_read_timeout(None).ok();
|
||||
stream.set_read_timeout(None)?;
|
||||
}
|
||||
|
||||
if let Some(deadline) = deadline {
|
||||
stream
|
||||
.set_write_timeout(Some(time_until_deadline(deadline)?))
|
||||
.ok();
|
||||
} else if unit.req.timeout_write > 0 {
|
||||
stream
|
||||
.set_write_timeout(Some(Duration::from_millis(unit.req.timeout_write as u64)))
|
||||
.ok();
|
||||
stream.set_write_timeout(Some(time_until_deadline(deadline)?))?;
|
||||
} else if let Some(timeout_write) = unit.req.timeout_write {
|
||||
stream.set_write_timeout(Some(timeout_write)).ok();
|
||||
} else {
|
||||
stream.set_write_timeout(None).ok();
|
||||
stream.set_write_timeout(None)?;
|
||||
}
|
||||
|
||||
if proto == Some(Proto::HTTPConnect) {
|
||||
|
||||
Reference in New Issue
Block a user