From 1201e2ba1e138f988eeadd45e72b619a0b6647ce Mon Sep 17 00:00:00 2001 From: Timothy Clem Date: Wed, 19 Jul 2023 10:31:17 -0700 Subject: [PATCH] Client fixes, streamline tokio deps --- Cargo.lock | 73 ++----------------------------- crates/twirp/Cargo.toml | 6 +-- crates/twirp/src/client.rs | 59 +++++++++++++------------ example/Cargo.toml | 2 +- example/src/bin/example-client.rs | 2 +- 5 files changed, 37 insertions(+), 105 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 1a9b411..9384470 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -355,15 +355,6 @@ version = "0.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8" -[[package]] -name = "hermit-abi" -version = "0.2.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ee512640fe35acbfb4bb779db6f0d80704c2cacfa2e39b601ef3e3f47d1ae4c7" -dependencies = [ - "libc", -] - [[package]] name = "hermit-abi" version = "0.3.1" @@ -485,7 +476,7 @@ version = "1.0.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "eae7b9aee968036d54dce06cebaefd919e4472e753296daccd6d344e3e2df0c2" dependencies = [ - "hermit-abi 0.3.1", + "hermit-abi", "libc", "windows-sys", ] @@ -538,16 +529,6 @@ version = "0.3.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ef53942eb7bf7ff43a617b3e2c1c4a5ecf5944a7c1bc12d7ee39bbb15e5c1519" -[[package]] -name = "lock_api" -version = "0.4.10" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c1cc9717a20b1bb222f333e6a92fd32f7d8a18ddc5a3191a11af45dcbf4dcd16" -dependencies = [ - "autocfg", - "scopeguard", -] - [[package]] name = "log" version = "0.4.19" @@ -618,11 +599,11 @@ dependencies = [ [[package]] name = "num_cpus" -version = "1.15.0" +version = "1.16.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0fac9e2da13b5eb447a6ce3d392f23a29d8694bff781bf03a16cd9ac8697593b" +checksum = "4161fcb6d602d4d2081af7c3a45852d875a03dd337a6bfdd6e06407b61342a43" dependencies = [ - "hermit-abi 0.2.6", + "hermit-abi", "libc", ] @@ -676,29 +657,6 @@ dependencies = [ "vcpkg", ] -[[package]] -name = "parking_lot" -version = "0.12.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3742b2c103b9f06bc9fff0a37ff4912935851bee6d36f3c02bcc755bcfec228f" -dependencies = [ - "lock_api", - "parking_lot_core", -] - -[[package]] -name = "parking_lot_core" -version = "0.9.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "93f00c865fe7cabf650081affecd3871070f26767e7b2070a3ffae14c654b447" -dependencies = [ - "cfg-if", - "libc", - "redox_syscall", - "smallvec", - "windows-targets", -] - [[package]] name = "percent-encoding" version = "2.3.0" @@ -954,12 +912,6 @@ dependencies = [ "windows-sys", ] -[[package]] -name = "scopeguard" -version = "1.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd" - [[package]] name = "security-framework" version = "2.9.1" @@ -1026,15 +978,6 @@ dependencies = [ "serde", ] -[[package]] -name = "signal-hook-registry" -version = "1.4.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d8229b473baa5980ac72ef434c4415e70c4b5e71b423043adb4ba059f89c99a1" -dependencies = [ - "libc", -] - [[package]] name = "slab" version = "0.4.8" @@ -1044,12 +987,6 @@ dependencies = [ "autocfg", ] -[[package]] -name = "smallvec" -version = "1.10.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a507befe795404456341dfab10cef66ead4c041f62b8b11bbb92bffe5d0953e0" - [[package]] name = "socket2" version = "0.4.9" @@ -1142,9 +1079,7 @@ dependencies = [ "libc", "mio", "num_cpus", - "parking_lot", "pin-project-lite", - "signal-hook-registry", "socket2", "tokio-macros", "windows-sys", diff --git a/crates/twirp/Cargo.toml b/crates/twirp/Cargo.toml index 5b7b699..a3eedf7 100644 --- a/crates/twirp/Cargo.toml +++ b/crates/twirp/Cargo.toml @@ -26,8 +26,4 @@ hyper = { version = "0.14", features = ["full"], optional = true } # For the test-support feature async-trait = { version = "0.1", optional = true } -tokio = { version = "1.21.2", features = ["full"], optional = true } - -[dev-dependencies] -tokio = { version = "1.28", features = ["full", "test-util"] } -async-trait = "0.1" +tokio = { version = "1.28", features = [], optional = true } diff --git a/crates/twirp/src/client.rs b/crates/twirp/src/client.rs index fd36668..3841546 100644 --- a/crates/twirp/src/client.rs +++ b/crates/twirp/src/client.rs @@ -56,6 +56,9 @@ impl ClientBuilder { } } + /// Add middleware to the client that will be called on each request. + /// Middlewares are invoked in the order they are added as part of the + /// request cycle. pub fn with(self, middleware: M) -> Self where M: Middleware, @@ -102,51 +105,49 @@ impl std::fmt::Debug for Client { } impl Client { - /// Creates a `twirp::Client` with the default `reqwest::ClientBuilder`. + /// Creates a `twirp::Client`. /// /// The underlying `reqwest::Client` holds a connection pool internally, so it is advised that /// you create one and **reuse** it. - pub fn default(base_url: Url) -> Result { + pub fn new( + base_url: Url, + b: reqwest::ClientBuilder, + middlewares: Vec>, + ) -> Result { if base_url.path().ends_with('/') { - Self::new(base_url, reqwest::ClientBuilder::default(), vec![]) + let mut headers: HeaderMap = HeaderMap::default(); + headers.insert(CONTENT_TYPE, CONTENT_TYPE_PROTOBUF.try_into()?); + let client = b.default_headers(headers).build()?; + Ok(Client { + base_url: Arc::new(base_url), + client: Arc::new(client), + middlewares, + }) } else { Err(ClientError::InvalidBaseUrl(base_url)) } } - /// Creates a `twirp::Client`. + /// Creates a `twirp::Client` with the default `reqwest::ClientBuilder`. /// /// The underlying `reqwest::Client` holds a connection pool internally, so it is advised that /// you create one and **reuse** it. - pub fn new( - base_url: Url, - b: reqwest::ClientBuilder, - middlewares: Vec>, - ) -> Result { - let mut headers: HeaderMap = HeaderMap::default(); - headers.insert(CONTENT_TYPE, CONTENT_TYPE_PROTOBUF.try_into()?); - let client = b.default_headers(headers).build()?; - Ok(Client { - base_url: Arc::new(base_url), - client: Arc::new(client), - middlewares, - }) + pub fn from_base_url(base_url: Url) -> Result { + Self::new(base_url, reqwest::ClientBuilder::default(), vec![]) } - /// Add some middleware to the request stack. - pub fn with(&self, middleware: M) -> Self + /// Add middleware to this specific request stack. Middlewares are invoked + /// in the order they are added as part of the request cycle. Middleware + /// added here will run after any middleware added with the `ClientBuilder`. + pub fn with(mut self, middleware: M) -> Self where M: Middleware, { - let mut middlewares = self.middlewares.clone(); - middlewares.push(Arc::new(middleware)); - Self { - base_url: self.base_url.clone(), - client: self.client.clone(), - middlewares, - } + self.middlewares.push(Arc::new(middleware)); + self } + /// Make an HTTP twirp request. pub async fn request(&self, url: Url, body: I) -> Result where I: prost::Message, @@ -258,10 +259,10 @@ mod tests { #[tokio::test] async fn test_base_url() { let url = Url::parse("http://localhost:3001/twirp/").unwrap(); - assert!(Client::default(url).is_ok()); + assert!(Client::from_base_url(url).is_ok()); let url = Url::parse("http://localhost:3001/twirp").unwrap(); assert_eq!( - Client::default(url).unwrap_err().to_string(), + Client::from_base_url(url).unwrap_err().to_string(), "base_url must end in /, but got: http://localhost:3001/twirp", ); } @@ -288,7 +289,7 @@ mod tests { async fn test_standard_client() { let h = run_test_server(3001).await; let base_url = Url::parse("http://localhost:3001/twirp/").unwrap(); - let client = Client::default(base_url).unwrap(); + let client = Client::from_base_url(base_url).unwrap(); let resp = client .ping(PingRequest { name: "hi".to_string(), diff --git a/example/Cargo.toml b/example/Cargo.toml index 163cee4..a6ceca6 100644 --- a/example/Cargo.toml +++ b/example/Cargo.toml @@ -11,7 +11,7 @@ prost = "0.11" prost-wkt = "0.3" prost-wkt-types = "0.3" serde = { version = "1.0", features = ["derive"] } -tokio = { version = "1.21.2", features = ["full"] } +tokio = { version = "1.28", features = ["rt-multi-thread", "macros"] } [build-dependencies] fs-err = "2.8" diff --git a/example/src/bin/example-client.rs b/example/src/bin/example-client.rs index 3b9f1b2..a0ae847 100644 --- a/example/src/bin/example-client.rs +++ b/example/src/bin/example-client.rs @@ -18,7 +18,7 @@ use service::haberdash::v1::{HaberdasherAPIClient, MakeHatRequest, MakeHatRespon pub async fn main() -> Result<(), GenericError> { // basic client use service::haberdash::v1::HaberdasherAPIClient; - let client = Client::default(Url::parse("http://localhost:3000/twirp/")?)?; + let client = Client::from_base_url(Url::parse("http://localhost:3000/twirp/")?)?; let resp = client.make_hat(MakeHatRequest { inches: 1 }).await; eprintln!("{:?}", resp);