Switch timeout APIs to use Duration.
This commit is contained in:
committed by
Martin Algesten
parent
00e3294ac1
commit
257d4e54dd
@@ -42,7 +42,7 @@ type Result<T> = result::Result<T, Oops>;
|
|||||||
fn get(agent: &ureq::Agent, url: &String) -> Result<Vec<u8>> {
|
fn get(agent: &ureq::Agent, url: &String) -> Result<Vec<u8>> {
|
||||||
let response = agent
|
let response = agent
|
||||||
.get(url)
|
.get(url)
|
||||||
.timeout_connect(5_000)
|
.timeout_connect(std::time::Duration::from_secs(5))
|
||||||
.timeout(Duration::from_secs(20))
|
.timeout(Duration::from_secs(20))
|
||||||
.call();
|
.call();
|
||||||
if let Some(err) = response.synthetic_error() {
|
if let Some(err) = response.synthetic_error() {
|
||||||
|
|||||||
@@ -39,9 +39,9 @@ pub struct Request {
|
|||||||
// from request itself
|
// from request itself
|
||||||
pub(crate) headers: Vec<Header>,
|
pub(crate) headers: Vec<Header>,
|
||||||
pub(crate) query: QString,
|
pub(crate) query: QString,
|
||||||
pub(crate) timeout_connect: u64,
|
pub(crate) timeout_connect: Option<time::Duration>,
|
||||||
pub(crate) timeout_read: u64,
|
pub(crate) timeout_read: Option<time::Duration>,
|
||||||
pub(crate) timeout_write: u64,
|
pub(crate) timeout_write: Option<time::Duration>,
|
||||||
pub(crate) timeout: Option<time::Duration>,
|
pub(crate) timeout: Option<time::Duration>,
|
||||||
pub(crate) redirects: u32,
|
pub(crate) redirects: u32,
|
||||||
pub(crate) proxy: Option<Proxy>,
|
pub(crate) proxy: Option<Proxy>,
|
||||||
@@ -99,7 +99,7 @@ impl Request {
|
|||||||
///
|
///
|
||||||
/// ```
|
/// ```
|
||||||
/// let r = ureq::get("/my_page")
|
/// 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();
|
/// .call();
|
||||||
///
|
///
|
||||||
/// println!("{:?}", r);
|
/// println!("{:?}", r);
|
||||||
@@ -346,12 +346,12 @@ impl Request {
|
|||||||
///
|
///
|
||||||
/// ```
|
/// ```
|
||||||
/// let r = ureq::get("/my_page")
|
/// let r = ureq::get("/my_page")
|
||||||
/// .timeout_connect(1_000) // wait max 1 second to connect
|
/// .timeout(std::time::Duration::from_secs(1))
|
||||||
/// .call();
|
/// .call();
|
||||||
/// println!("{:?}", r);
|
/// println!("{:?}", r);
|
||||||
/// ```
|
/// ```
|
||||||
pub fn timeout_connect(&mut self, millis: u64) -> &mut Request {
|
pub fn timeout_connect(&mut self, timeout: time::Duration) -> &mut Request {
|
||||||
self.timeout_connect = millis;
|
self.timeout_connect = Some(timeout);
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -363,12 +363,12 @@ impl Request {
|
|||||||
///
|
///
|
||||||
/// ```
|
/// ```
|
||||||
/// let r = ureq::get("/my_page")
|
/// 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();
|
/// .call();
|
||||||
/// println!("{:?}", r);
|
/// println!("{:?}", r);
|
||||||
/// ```
|
/// ```
|
||||||
pub fn timeout_read(&mut self, millis: u64) -> &mut Request {
|
pub fn timeout_read(&mut self, timeout: time::Duration) -> &mut Request {
|
||||||
self.timeout_read = millis;
|
self.timeout_read = Some(timeout);
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -380,12 +380,12 @@ impl Request {
|
|||||||
///
|
///
|
||||||
/// ```
|
/// ```
|
||||||
/// let r = ureq::get("/my_page")
|
/// let r = ureq::get("/my_page")
|
||||||
/// .timeout_write(1_000) // wait max 1 second for sending.
|
/// .timeout(std::time::Duration::from_secs(1))
|
||||||
/// .call();
|
/// .call();
|
||||||
/// println!("{:?}", r);
|
/// println!("{:?}", r);
|
||||||
/// ```
|
/// ```
|
||||||
pub fn timeout_write(&mut self, millis: u64) -> &mut Request {
|
pub fn timeout_write(&mut self, timeout: time::Duration) -> &mut Request {
|
||||||
self.timeout_write = millis;
|
self.timeout_write = Some(timeout);
|
||||||
self
|
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> {
|
pub(crate) fn connect_host(unit: &Unit, hostname: &str, port: u16) -> Result<TcpStream, Error> {
|
||||||
let deadline: Option<Instant> = if unit.req.timeout_connect > 0 {
|
let deadline: Option<Instant> = if let Some(timeout_connect) = unit.req.timeout_connect {
|
||||||
Instant::now().checked_add(Duration::from_millis(unit.req.timeout_connect))
|
Instant::now().checked_add(timeout_connect)
|
||||||
} else {
|
} else {
|
||||||
unit.deadline
|
unit.deadline
|
||||||
};
|
};
|
||||||
@@ -447,30 +447,20 @@ pub(crate) fn connect_host(unit: &Unit, hostname: &str, port: u16) -> Result<Tcp
|
|||||||
return Err(err);
|
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 {
|
if let Some(deadline) = deadline {
|
||||||
stream
|
stream.set_read_timeout(Some(time_until_deadline(deadline)?))?;
|
||||||
.set_read_timeout(Some(time_until_deadline(deadline)?))
|
} else if let Some(timeout_read) = unit.req.timeout_read {
|
||||||
.ok();
|
stream.set_read_timeout(Some(timeout_read))?;
|
||||||
} else if unit.req.timeout_read > 0 {
|
|
||||||
stream
|
|
||||||
.set_read_timeout(Some(Duration::from_millis(unit.req.timeout_read as u64)))
|
|
||||||
.ok();
|
|
||||||
} else {
|
} else {
|
||||||
stream.set_read_timeout(None).ok();
|
stream.set_read_timeout(None)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(deadline) = deadline {
|
if let Some(deadline) = deadline {
|
||||||
stream
|
stream.set_write_timeout(Some(time_until_deadline(deadline)?))?;
|
||||||
.set_write_timeout(Some(time_until_deadline(deadline)?))
|
} else if let Some(timeout_write) = unit.req.timeout_write {
|
||||||
.ok();
|
stream.set_write_timeout(Some(timeout_write)).ok();
|
||||||
} else if unit.req.timeout_write > 0 {
|
|
||||||
stream
|
|
||||||
.set_write_timeout(Some(Duration::from_millis(unit.req.timeout_write as u64)))
|
|
||||||
.ok();
|
|
||||||
} else {
|
} else {
|
||||||
stream.set_write_timeout(None).ok();
|
stream.set_write_timeout(None)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
if proto == Some(Proto::HTTPConnect) {
|
if proto == Some(Proto::HTTPConnect) {
|
||||||
|
|||||||
Reference in New Issue
Block a user