@@ -6,13 +6,15 @@ use crate::{config::General, frontend::ClientRequest, state::State};
66pub struct Timeouts {
77 pub ( super ) query_timeout : Duration ,
88 pub ( super ) client_idle_timeout : Duration ,
9+ pub ( super ) idle_in_transaction_timeout : Duration ,
910}
1011
1112impl Default for Timeouts {
1213 fn default ( ) -> Self {
1314 Self {
1415 query_timeout : Duration :: MAX ,
1516 client_idle_timeout : Duration :: MAX ,
17+ idle_in_transaction_timeout : Duration :: MAX ,
1618 }
1719 }
1820}
@@ -22,6 +24,7 @@ impl Timeouts {
2224 Self {
2325 query_timeout : general. query_timeout ( ) ,
2426 client_idle_timeout : general. client_idle_timeout ( ) ,
27+ idle_in_transaction_timeout : general. client_idle_in_transaction_timeout ( ) ,
2528 }
2629 }
2730
@@ -48,7 +51,40 @@ impl Timeouts {
4851 Duration :: MAX
4952 }
5053 }
54+ State :: IdleInTransaction => {
55+ // Client is sending the request, don't fire.
56+ if !client_request. messages . is_empty ( ) {
57+ Duration :: MAX
58+ } else {
59+ self . idle_in_transaction_timeout
60+ }
61+ }
62+
5163 _ => Duration :: MAX ,
5264 }
5365 }
5466}
67+
68+ #[ cfg( test) ]
69+ mod test {
70+
71+ use crate :: { config:: config, net:: Query } ;
72+
73+ use super :: * ;
74+
75+ #[ test]
76+ fn test_idle_in_transaction_timeout ( ) {
77+ let config = config ( ) ; // Will be default.
78+ let timeout = Timeouts :: from_config ( & config. config . general ) ;
79+
80+ let actual = timeout. client_idle_timeout ( & State :: IdleInTransaction , & ClientRequest :: new ( ) ) ;
81+ assert_eq ! ( actual, timeout. idle_in_transaction_timeout) ;
82+ assert_eq ! ( actual. as_millis( ) , u64 :: MAX . into( ) ) ;
83+
84+ let actual = timeout. client_idle_timeout (
85+ & State :: IdleInTransaction ,
86+ & ClientRequest :: from ( vec ! [ Query :: new( "SELECT 1" ) . into( ) ] ) ,
87+ ) ;
88+ assert_eq ! ( actual, Duration :: MAX ) ;
89+ }
90+ }
0 commit comments