Skip to content

Commit

Permalink
Refactor client pool limits
Browse files Browse the repository at this point in the history
  • Loading branch information
fafhrd91 committed Oct 9, 2023
1 parent 0feb305 commit ebd466d
Showing 1 changed file with 50 additions and 21 deletions.
71 changes: 50 additions & 21 deletions src/client/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,21 +78,35 @@ impl Client {
idx += 1;
}
}

// check existing connections
let available = connections.iter().filter(|item| item.is_ready()).count();
let client = if available > 0 {
let idx = WyRand::new().generate_range(0_usize..available);
connections
.iter()
.filter(|item| item.is_ready())
.nth(idx)
.cloned()
let num = connections.len();
if self.inner.minconn > 0 && num < self.inner.minconn {
// create new connection
(None, num)
} else {
None
};

(client, connections.len())
// first search for connections with less than 50% capacity usage
let client = connections.iter().find(|item| {
let cap = item.max_streams().unwrap_or(self.inner.max_streams) >> 1;
item.active_streams() <= cap
});
if let Some(client) = client {
(Some(client.clone()), num)
} else {
// check existing connections
let available = connections.iter().filter(|item| item.is_ready()).count();
let client = if available > 0 {
let idx = WyRand::new().generate_range(0_usize..available);
connections
.iter()
.filter(|item| item.is_ready())
.nth(idx)
.cloned()

Check warning on line 102 in src/client/pool.rs

View check run for this annotation

Codecov / codecov/patch

src/client/pool.rs#L97-L102

Added lines #L97 - L102 were not covered by tests
} else {
None
};

(client, num)
}
}
};

if let Some(client) = client {
Expand All @@ -103,7 +117,10 @@ impl Client {
}

// can create new connection
if !self.inner.connecting.get() && num < self.inner.maxconn {
if !self.inner.connecting.get()
&& (num < self.inner.maxconn
|| (self.inner.minconn > 0 && num < self.inner.minconn))
{
// create new connection
self.inner.connecting.set(true);
let (tx, rx) = oneshot::channel();
Expand Down Expand Up @@ -202,11 +219,12 @@ impl Client {
pub struct ClientBuilder(Inner);

struct Inner {
minconn: usize,
maxconn: usize,
conn_timeout: Millis,
conn_lifetime: Duration,
disconnect_timeout: Millis,
limit: usize,
max_streams: u32,
scheme: Scheme,
config: crate::Config,
authority: ByteString,
Expand Down Expand Up @@ -243,7 +261,8 @@ impl ClientBuilder {
conn_timeout: Millis(1_000),
conn_lifetime: Duration::from_secs(0),
disconnect_timeout: Millis(3_000),
limit: 100,
max_streams: 100,
minconn: 1,
maxconn: 16,
scheme: Scheme::HTTP,
config: crate::Config::client(),
Expand Down Expand Up @@ -293,8 +312,8 @@ impl ClientBuilder {
/// If limit is 0, the connector uses "MAX_CONCURRENT_STREAMS" config from connection
/// settings.
/// The default limit size is 100.
pub fn limit(mut self, limit: usize) -> Self {
self.0.limit = limit;
pub fn max_streams(mut self, limit: u32) -> Self {
self.0.max_streams = limit;

Check warning on line 316 in src/client/pool.rs

View check run for this annotation

Codecov / codecov/patch

src/client/pool.rs#L315-L316

Added lines #L315 - L316 were not covered by tests
self
}

Expand All @@ -309,6 +328,14 @@ impl ClientBuilder {
self
}

/// Sets the minimum concurrent connections.
///
/// By default min connections is set to a 1.
pub fn minconn(mut self, num: usize) -> Self {
self.0.minconn = num;
self
}

Check warning on line 337 in src/client/pool.rs

View check run for this annotation

Codecov / codecov/patch

src/client/pool.rs#L334-L337

Added lines #L334 - L337 were not covered by tests

/// Sets the maximum concurrent connections.
///
/// By default max connections is set to a 16.
Expand Down Expand Up @@ -386,8 +413,9 @@ impl fmt::Debug for Client {
.field("conn_timeout", &self.inner.conn_timeout)
.field("conn_lifetime", &self.inner.conn_lifetime)
.field("disconnect_timeout", &self.inner.disconnect_timeout)
.field("minconn", &self.inner.minconn)

Check warning on line 416 in src/client/pool.rs

View check run for this annotation

Codecov / codecov/patch

src/client/pool.rs#L416

Added line #L416 was not covered by tests
.field("maxconn", &self.inner.maxconn)
.field("limit", &self.inner.limit)
.field("max-streams", &self.inner.max_streams)

Check warning on line 418 in src/client/pool.rs

View check run for this annotation

Codecov / codecov/patch

src/client/pool.rs#L418

Added line #L418 was not covered by tests
.field("pool", &self.inner.pool)
.field("config", &self.inner.config)
.finish()
Expand All @@ -402,8 +430,9 @@ impl fmt::Debug for ClientBuilder {
.field("conn_timeout", &self.0.conn_timeout)
.field("conn_lifetime", &self.0.conn_lifetime)
.field("disconnect_timeout", &self.0.disconnect_timeout)
.field("minconn", &self.0.minconn)

Check warning on line 433 in src/client/pool.rs

View check run for this annotation

Codecov / codecov/patch

src/client/pool.rs#L433

Added line #L433 was not covered by tests
.field("maxconn", &self.0.maxconn)
.field("limit", &self.0.limit)
.field("max-streams", &self.0.max_streams)

Check warning on line 435 in src/client/pool.rs

View check run for this annotation

Codecov / codecov/patch

src/client/pool.rs#L435

Added line #L435 was not covered by tests
.field("pool", &self.0.pool)
.field("config", &self.0.config)
.finish()
Expand Down

0 comments on commit ebd466d

Please sign in to comment.