Skip to content

Commit 92f1928

Browse files
committed
add connection_pool_max_size option to memcached and redis builders
1 parent 81e2653 commit 92f1928

File tree

6 files changed

+50
-4
lines changed

6 files changed

+50
-4
lines changed

core/src/services/memcached/backend.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,20 @@ impl MemcachedBuilder {
7575
self.config.default_ttl = Some(ttl);
7676
self
7777
}
78+
79+
/// Sets the maximum number of connections managed by the pool.
80+
///
81+
/// Defaults to 10.
82+
///
83+
/// # Panics
84+
///
85+
/// Will panic if `max_size` is 0.
86+
#[must_use]
87+
pub fn connection_pool_max_size(mut self, max_size: u32) -> Self {
88+
assert!(max_size > 0, "max_size must be greater than zero!");
89+
self.config.connection_pool_max_size = Some(max_size);
90+
self
91+
}
7892
}
7993

8094
impl Builder for MemcachedBuilder {
@@ -144,6 +158,7 @@ impl Builder for MemcachedBuilder {
144158
username: self.config.username.clone(),
145159
password: self.config.password.clone(),
146160
default_ttl: self.config.default_ttl,
161+
connection_pool_max_size: self.config.connection_pool_max_size,
147162
})
148163
.with_normalized_root(root))
149164
}

core/src/services/memcached/config.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ pub struct MemcachedConfig {
4242
pub password: Option<String>,
4343
/// The default ttl for put operations.
4444
pub default_ttl: Option<Duration>,
45+
/// The maximum number of connections allowed.
46+
///
47+
/// default is 10
48+
pub connection_pool_max_size: Option<u32>,
4549
}
4650

4751
impl Debug for MemcachedConfig {

core/src/services/memcached/core.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ pub struct MemcachedCore {
7676
pub username: Option<String>,
7777
pub password: Option<String>,
7878
pub default_ttl: Option<Duration>,
79+
pub connection_pool_max_size: Option<u32>,
7980
}
8081

8182
impl MemcachedCore {
@@ -89,10 +90,14 @@ impl MemcachedCore {
8990
self.password.clone(),
9091
);
9192

92-
bb8::Pool::builder().build(mgr).await.map_err(|err| {
93-
Error::new(ErrorKind::ConfigInvalid, "connect to memecached failed")
94-
.set_source(err)
95-
})
93+
bb8::Pool::builder()
94+
.max_size(self.connection_pool_max_size.unwrap_or(10))
95+
.build(mgr)
96+
.await
97+
.map_err(|err| {
98+
Error::new(ErrorKind::ConfigInvalid, "connect to memecached failed")
99+
.set_source(err)
100+
})
96101
})
97102
.await?;
98103

core/src/services/redis/backend.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,20 @@ impl RedisBuilder {
123123

124124
self
125125
}
126+
127+
/// Sets the maximum number of connections managed by the pool.
128+
///
129+
/// Defaults to 10.
130+
///
131+
/// # Panics
132+
///
133+
/// Will panic if `max_size` is 0.
134+
#[must_use]
135+
pub fn connection_pool_max_size(mut self, max_size: u32) -> Self {
136+
assert!(max_size > 0, "max_size must be greater than zero!");
137+
self.config.connection_pool_max_size = Some(max_size);
138+
self
139+
}
126140
}
127141

128142
impl Builder for RedisBuilder {
@@ -159,6 +173,7 @@ impl Builder for RedisBuilder {
159173
cluster_client: Some(client),
160174
conn,
161175
default_ttl: self.config.default_ttl,
176+
connection_pool_max_size: self.config.connection_pool_max_size,
162177
})
163178
.with_normalized_root(root))
164179
} else {
@@ -184,6 +199,7 @@ impl Builder for RedisBuilder {
184199
cluster_client: None,
185200
conn,
186201
default_ttl: self.config.default_ttl,
202+
connection_pool_max_size: self.config.connection_pool_max_size,
187203
})
188204
.with_normalized_root(root))
189205
}

core/src/services/redis/config.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ pub struct RedisConfig {
3636
///
3737
/// default is None
3838
pub cluster_endpoints: Option<String>,
39+
/// The maximum number of connections allowed.
40+
///
41+
/// default is 10
42+
pub connection_pool_max_size: Option<u32>,
3943
/// the username to connect redis service.
4044
///
4145
/// default is None

core/src/services/redis/core.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ pub struct RedisCore {
119119
pub cluster_client: Option<ClusterClient>,
120120
pub conn: OnceCell<bb8::Pool<RedisConnectionManager>>,
121121
pub default_ttl: Option<Duration>,
122+
pub connection_pool_max_size: Option<u32>,
122123
}
123124

124125
impl Debug for RedisCore {
@@ -135,6 +136,7 @@ impl RedisCore {
135136
.conn
136137
.get_or_try_init(|| async {
137138
bb8::Pool::builder()
139+
.max_size(self.connection_pool_max_size.unwrap_or(10))
138140
.build(self.get_redis_connection_manager())
139141
.await
140142
.map_err(|err| {

0 commit comments

Comments
 (0)