Move Agent construction to new AgentBuilder.

In the process, rename set_foo methods to just foo, since methods on the
builder will always be setters.

Adds a new() method on ConnectionPool so it can be constructed directly
with the desired limits. Removes the setter methods on ConnectionPool
for those limits. This means that connection limits can only be set when
an Agent is built.

There were two tests that verify Send and Sync implementations, one for
Agent and one for Request. This PR moves the Request test to request.rs,
and changes both tests to more directly verify the traits. There may be
another way to do this, I'm not sure.
This commit is contained in:
Jacob Hoffman-Andrews
2020-10-17 23:07:53 -07:00
committed by Martin Algesten
parent 0f083a4436
commit 30162bf3bb
9 changed files with 211 additions and 262 deletions

View File

@@ -73,16 +73,13 @@ impl Default for ConnectionPool {
}
impl ConnectionPool {
pub fn set_max_idle_connections(&mut self, max_connections: usize) {
if self.max_idle_connections == max_connections {
return;
pub(crate) fn new(max_idle_connections: usize, max_idle_connections_per_host: usize) -> Self {
ConnectionPool {
recycle: Default::default(),
lru: Default::default(),
max_idle_connections,
max_idle_connections_per_host,
}
// Remove any extra connections if the number was decreased.
while self.lru.len() > max_connections {
self.remove_oldest();
}
self.max_idle_connections = max_connections;
}
/// Return true if either of the max_* settings is 0, meaning we should do no work.
@@ -90,30 +87,6 @@ impl ConnectionPool {
self.max_idle_connections == 0 || self.max_idle_connections_per_host == 0
}
pub fn set_max_idle_connections_per_host(&mut self, max_connections: usize) {
if self.max_idle_connections_per_host == max_connections {
return;
}
if max_connections == 0 {
// Clear the connection pool, caching is disabled.
self.lru.clear();
self.recycle.clear();
return;
}
// Remove any extra streams if the number was decreased.
for (key, val) in self.recycle.iter_mut() {
while val.len() > max_connections {
// Remove the oldest entry
val.pop_front();
remove_first_match(&mut self.lru, key)
.expect("invariant failed: key in recycle but not in lru");
}
}
self.max_idle_connections_per_host = max_connections;
}
/// How the unit::connect tries to get a pooled connection.
pub fn try_get_connection(&mut self, url: &Url, proxy: &Option<Proxy>) -> Option<Stream> {
let key = PoolKey::new(url, proxy);
@@ -287,50 +260,6 @@ fn pool_per_host_connections_limit() {
assert_eq!(pool.len(), 0);
}
#[test]
fn pool_update_connection_limit() {
let mut pool = ConnectionPool::default();
pool.set_max_idle_connections(50);
let hostnames = (0..pool.max_idle_connections).map(|i| format!("{}.example", i));
let poolkeys = hostnames.map(|hostname| PoolKey {
scheme: "https".to_string(),
hostname,
port: Some(999),
proxy: None,
});
for key in poolkeys.clone() {
pool.add(key, Stream::Cursor(std::io::Cursor::new(vec![])));
}
assert_eq!(pool.len(), 50);
pool.set_max_idle_connections(25);
assert_eq!(pool.len(), 25);
}
#[test]
fn pool_update_per_host_connection_limit() {
let mut pool = ConnectionPool::default();
pool.set_max_idle_connections(50);
pool.set_max_idle_connections_per_host(50);
let poolkey = PoolKey {
scheme: "https".to_string(),
hostname: "example.com".to_string(),
port: Some(999),
proxy: None,
};
for _ in 0..50 {
pool.add(
poolkey.clone(),
Stream::Cursor(std::io::Cursor::new(vec![])),
);
}
assert_eq!(pool.len(), 50);
pool.set_max_idle_connections_per_host(25);
assert_eq!(pool.len(), 25);
}
#[test]
fn pool_checks_proxy() {
// Test inserting different poolkeys with same address but different proxies.