Skip to content

Commit

Permalink
Document that blocking and async do not mix (#1159)
Browse files Browse the repository at this point in the history
See discussion on #1017. This patch adds documentation to `blocking` at
module level and to its `Client::new` and `ClientBuilder::build`, noting
that you can’t create or use a blocking client from within an async
runtime, and suggesting potential alternatives.

Presumably, all the other methods on `Client` also have this property,
but hitting the failure mode would require building a `blocking::Client`
outside an async runtime and then moving it into a runtime to send
requests; seems potentially not worth polluting all the docs.

Test Plan:
Ran `cargo doc --features blocking` and verified that the links work.

wchargin-branch: docs-blocking-no-async
wchargin-source: 0eb36352959cd2ca0b19df5836e75230dc619b9d
  • Loading branch information
wchargin authored Feb 5, 2021
1 parent 31b11c3 commit 9ae11b2
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/blocking/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,11 @@ impl ClientBuilder {
///
/// This method fails if TLS backend cannot be initialized, or the resolver
/// cannot load the system configuration.
///
/// # Panics
///
/// This method panics if called from within an async runtime. See docs on
/// [`reqwest::blocking`][crate::blocking] for details.
pub fn build(self) -> crate::Result<Client> {
ClientHandle::new(self).map(|handle| Client { inner: handle })
}
Expand Down Expand Up @@ -606,6 +611,9 @@ impl Client {
///
/// Use `Client::builder()` if you wish to handle the failure as an `Error`
/// instead of panicking.
///
/// This method also panics if called from within an async runtime. See docs
/// on [`reqwest::blocking`][crate::blocking] for details.
pub fn new() -> Client {
ClientBuilder::new().build().expect("Client::new()")
}
Expand Down
7 changes: 7 additions & 0 deletions src/blocking/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@
//! The blocking `Client` will block the current thread to execute, instead
//! of returning futures that need to be executed on a runtime.
//!
//! Conversely, the functionality in `reqwest::blocking` must *not* be executed
//! within an async runtime, or it will panic when attempting to block. If
//! calling directly from an async function, consider using an async
//! [`reqwest::Client`][crate::Client] instead. If the immediate context is only
//! synchronous, but a transitive caller is async, consider changing that caller
//! to use [`tokio::task::spawn_blocking`] around the calls that need to block.
//!
//! # Optional
//!
//! This requires the optional `blocking` feature to be enabled.
Expand Down

0 comments on commit 9ae11b2

Please sign in to comment.