File tree Expand file tree Collapse file tree 6 files changed +50
-4
lines changed Expand file tree Collapse file tree 6 files changed +50
-4
lines changed Original file line number Diff line number Diff line change @@ -76,6 +76,20 @@ impl MemcachedBuilder {
7676 self . config . default_ttl = Some ( ttl) ;
7777 self
7878 }
79+
80+ /// Sets the maximum number of connections managed by the pool.
81+ ///
82+ /// Defaults to 10.
83+ ///
84+ /// # Panics
85+ ///
86+ /// Will panic if `max_size` is 0.
87+ #[ must_use]
88+ pub fn connection_pool_max_size ( mut self , max_size : u32 ) -> Self {
89+ assert ! ( max_size > 0 , "max_size must be greater than zero!" ) ;
90+ self . config . connection_pool_max_size = Some ( max_size) ;
91+ self
92+ }
7993}
8094
8195impl Builder for MemcachedBuilder {
@@ -145,6 +159,7 @@ impl Builder for MemcachedBuilder {
145159 username : self . config . username . clone ( ) ,
146160 password : self . config . password . clone ( ) ,
147161 default_ttl : self . config . default_ttl ,
162+ connection_pool_max_size : self . config . connection_pool_max_size ,
148163 } )
149164 . with_normalized_root ( root) )
150165 }
Original file line number Diff line number Diff line change @@ -43,6 +43,10 @@ pub struct MemcachedConfig {
4343 pub password : Option < String > ,
4444 /// The default ttl for put operations.
4545 pub default_ttl : Option < Duration > ,
46+ /// The maximum number of connections allowed.
47+ ///
48+ /// default is 10
49+ pub connection_pool_max_size : Option < u32 > ,
4650}
4751
4852impl Debug for MemcachedConfig {
Original file line number Diff line number Diff 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
8182impl 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
Original file line number Diff line number Diff line change @@ -124,6 +124,20 @@ impl RedisBuilder {
124124
125125 self
126126 }
127+
128+ /// Sets the maximum number of connections managed by the pool.
129+ ///
130+ /// Defaults to 10.
131+ ///
132+ /// # Panics
133+ ///
134+ /// Will panic if `max_size` is 0.
135+ #[ must_use]
136+ pub fn connection_pool_max_size ( mut self , max_size : u32 ) -> Self {
137+ assert ! ( max_size > 0 , "max_size must be greater than zero!" ) ;
138+ self . config . connection_pool_max_size = Some ( max_size) ;
139+ self
140+ }
127141}
128142
129143impl Builder for RedisBuilder {
@@ -160,6 +174,7 @@ impl Builder for RedisBuilder {
160174 cluster_client : Some ( client) ,
161175 conn,
162176 default_ttl : self . config . default_ttl ,
177+ connection_pool_max_size : self . config . connection_pool_max_size ,
163178 } )
164179 . with_normalized_root ( root) )
165180 } else {
@@ -185,6 +200,7 @@ impl Builder for RedisBuilder {
185200 cluster_client : None ,
186201 conn,
187202 default_ttl : self . config . default_ttl ,
203+ connection_pool_max_size : self . config . connection_pool_max_size ,
188204 } )
189205 . with_normalized_root ( root) )
190206 }
Original file line number Diff line number Diff line change @@ -37,6 +37,10 @@ pub struct RedisConfig {
3737 ///
3838 /// default is None
3939 pub cluster_endpoints : Option < String > ,
40+ /// The maximum number of connections allowed.
41+ ///
42+ /// default is 10
43+ pub connection_pool_max_size : Option < u32 > ,
4044 /// the username to connect redis service.
4145 ///
4246 /// default is None
Original file line number Diff line number Diff 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
124125impl 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| {
You can’t perform that action at this time.
0 commit comments