Skip to content

feat: make HttpClient dyn-clonable #92

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -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]
3 changes: 2 additions & 1 deletion src/h1/mod.rs
Original file line number Diff line number Diff line change
@@ -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"))]
6 changes: 4 additions & 2 deletions src/hyper.rs
Original file line number Diff line number Diff line change
@@ -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;
@@ -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;
}

@@ -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,
2 changes: 1 addition & 1 deletion src/isahc.rs
Original file line number Diff line number Diff line change
@@ -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,
5 changes: 4 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -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;

@@ -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>;

2 changes: 1 addition & 1 deletion src/wasm.rs
Original file line number Diff line number Diff line change
@@ -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: (),
}