Skip to content

Commit

Permalink
feat: make HttpClient dyn-clonable
Browse files Browse the repository at this point in the history
Kinda reverts http-rs#48

Related to http-rs/surf#237

Desirable for Surf-level config.
  • Loading branch information
Fishrock123 committed Jul 5, 2021
1 parent cdb4972 commit 6ecac07
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 6 deletions.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ hyper = { version = "0.13.6", features = ["tcp"], optional = true }
hyper-tls = { version = "0.4.3", optional = true }
futures-util = { version = "0.3.5", features = ["io"], optional = true }
tokio = { version = "0.2", features = ["time"], optional = true }
dyn-clone = "1.0.4"
dyn-clonable = "0.9.0"

# curl_client
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
Expand Down
3 changes: 2 additions & 1 deletion src/h1/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ type HttpPool = DashMap<SocketAddr, Pool<TcpStream, std::io::Error>>;
#[cfg(any(feature = "native-tls", feature = "rustls"))]
type HttpsPool = DashMap<SocketAddr, Pool<TlsStream<TcpStream>, Error>>;

/// Async-h1 based HTTP Client, with connecton pooling ("Keep-Alive").
/// Async-h1 based HTTP Client, with connection pooling ("Keep-Alive").
#[derive(Clone)]
pub struct H1Client {
http_pools: HttpPool,
#[cfg(any(feature = "native-tls", feature = "rustls"))]
Expand Down
6 changes: 4 additions & 2 deletions src/hyper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use std::fmt::Debug;
use std::io;
use std::str::FromStr;

use dyn_clonable::*;
use futures_util::stream::TryStreamExt;
use http_types::headers::{HeaderName, HeaderValue};
use http_types::StatusCode;
Expand All @@ -21,7 +22,8 @@ use super::{async_trait, Error, HttpClient, Request, Response};
type HyperRequest = hyper::Request<hyper::Body>;

// Avoid leaking Hyper generics into HttpClient by hiding it behind a dynamic trait object pointer.
trait HyperClientObject: Debug + Send + Sync + 'static {
#[clonable]
trait HyperClientObject: Clone + Debug + Send + Sync + 'static {
fn dyn_request(&self, req: hyper::Request<hyper::Body>) -> hyper::client::ResponseFuture;
}

Expand All @@ -32,7 +34,7 @@ impl<C: Clone + Connect + Debug + Send + Sync + 'static> HyperClientObject for h
}

/// Hyper-based HTTP Client.
#[derive(Debug)]
#[derive(Clone, Debug)]
pub struct HyperClient {
client: Box<dyn HyperClientObject>,
config: Config,
Expand Down
2 changes: 1 addition & 1 deletion src/isahc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::Config;
use super::{async_trait, Body, Error, HttpClient, Request, Response};

/// Curl-based HTTP Client.
#[derive(Debug)]
#[derive(Clone, Debug)]
pub struct IsahcClient {
client: isahc::HttpClient,
config: Config,
Expand Down
5 changes: 4 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ pub type Request = http_types::Request;
/// An HTTP Response type with a streaming body.
pub type Response = http_types::Response;

use dyn_clonable::*;

pub use async_trait::async_trait;
pub use http_types;

Expand All @@ -64,7 +66,8 @@ pub use http_types;
/// though middleware for one of its own requests, and in order to do so should be wrapped in an
/// `Rc`/`Arc` to enable reference cloning.
#[async_trait]
pub trait HttpClient: std::fmt::Debug + Unpin + Send + Sync + 'static {
#[clonable]
pub trait HttpClient: Clone + std::fmt::Debug + Unpin + Send + Sync + 'static {
/// Perform a request.
async fn send(&self, req: Request) -> Result<Response, Error>;

Expand Down
2 changes: 1 addition & 1 deletion src/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use std::pin::Pin;
use std::task::{Context, Poll};

/// WebAssembly HTTP Client.
#[derive(Debug)]
#[derive(Clone, Debug)]
pub struct WasmClient {
_priv: (),
}
Expand Down

0 comments on commit 6ecac07

Please sign in to comment.