11use std:: { sync:: Arc , time:: Duration } ;
2+ #[ cfg( test) ]
3+ use std:: cmp:: Ordering ;
24
35use derivative:: Derivative ;
4- use serde:: Deserialize ;
6+ use serde:: { Deserialize } ;
7+ #[ cfg( test) ]
8+ use serde:: de:: { Deserializer , Error } ;
59use typed_builder:: TypedBuilder ;
610
711use crate :: {
@@ -40,10 +44,10 @@ pub(crate) struct ConnectionPoolOptions {
4044 #[ serde( skip) ]
4145 pub ( crate ) event_handler : Option < Arc < dyn CmapEventHandler > > ,
4246
43- /// How often the background thread performs its maintenance (e.g. ensure minPoolSize).
47+ /// Interval between background thread maintenance runs (e.g. ensure minPoolSize).
4448 #[ cfg( test) ]
45- #[ serde( skip ) ]
46- pub ( crate ) maintenance_frequency : Option < Duration > ,
49+ #[ serde( default , rename = "backgroundThreadIntervalMS" , deserialize_with = "BackgroundThreadInterval::deserialize_from_i64_millis" ) ]
50+ pub ( crate ) background_thread_interval : Option < BackgroundThreadInterval > ,
4751
4852 /// Connections that have been ready for usage in the pool for longer than `max_idle_time` will
4953 /// not be used.
@@ -101,7 +105,7 @@ impl ConnectionPoolOptions {
101105 credential : options. credential . clone ( ) ,
102106 event_handler : options. cmap_event_handler . clone ( ) ,
103107 #[ cfg( test) ]
104- maintenance_frequency : None ,
108+ background_thread_interval : None ,
105109 #[ cfg( test) ]
106110 ready : None ,
107111 }
@@ -116,6 +120,31 @@ impl ConnectionPoolOptions {
116120 }
117121}
118122
123+ #[ cfg( test) ]
124+ #[ derive( Debug , Copy , Clone , PartialEq , Eq ) ]
125+ pub ( crate ) enum BackgroundThreadInterval {
126+ Never ,
127+ Every ( Duration ) ,
128+ }
129+
130+ #[ cfg( test) ]
131+ impl BackgroundThreadInterval {
132+ pub ( crate ) fn deserialize_from_i64_millis < ' de , D > (
133+ deserializer : D ,
134+ ) -> std:: result:: Result < Option < Self > , D :: Error >
135+ where
136+ D : Deserializer < ' de > ,
137+ {
138+ let millis = Option :: < i64 > :: deserialize ( deserializer) ?;
139+ let millis = if let Some ( m) = millis { m } else { return Ok ( None ) } ;
140+ Ok ( Some ( match millis. cmp ( & 0 ) {
141+ Ordering :: Less => BackgroundThreadInterval :: Never ,
142+ Ordering :: Equal => return Err ( D :: Error :: custom ( "zero is not allowed" ) ) ,
143+ Ordering :: Greater => BackgroundThreadInterval :: Every ( Duration :: from_millis ( millis as u64 ) ) ,
144+ } ) )
145+ }
146+ }
147+
119148/// Options used for constructing a `Connection`.
120149#[ derive( Derivative ) ]
121150#[ derivative( Debug ) ]
0 commit comments