Skip to content

Commit

Permalink
Teach outbound-http to use connection pool
Browse files Browse the repository at this point in the history
`reqwest::Client` manages an internal connection pool. For executions
that make multiple requests to the same host we can take advantage of
that pool.

Signed-off-by: Lann Martin <lann.martin@fermyon.com>
  • Loading branch information
lann committed Sep 30, 2022
1 parent dfb3a90 commit b03a66b
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions crates/outbound-http/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,10 @@ use wasi_outbound_http::*;
pub struct OutboundHttp {
/// List of hosts guest modules are allowed to make requests to.
pub allowed_hosts: AllowedHttpHosts,
client: Option<Client>,
}

impl OutboundHttp {
pub fn new(allowed_hosts: AllowedHttpHosts) -> Self {
Self { allowed_hosts }
}

/// Check if guest module is allowed to send request to URL, based on the list of
/// allowed hosts defined by the runtime. If the list of allowed hosts contains
/// `insecure:allow-all`, then all hosts are allowed.
Expand Down Expand Up @@ -52,7 +49,10 @@ impl wasi_outbound_http::WasiOutboundHttp for OutboundHttp {
tracing::log::warn!("HTTP params field is deprecated");
}

let client = Client::builder().build().unwrap();
// Allow reuse of Client's internal connection pool for multiple requests
// in a single component execution
let client = self.client.get_or_insert_with(Default::default);

let resp = client
.request(method, url)
.headers(headers)
Expand Down

0 comments on commit b03a66b

Please sign in to comment.