@@ -499,7 +499,7 @@ where
499
499
// The query router determines where the query is going to go,
500
500
// e.g. primary, replica, which shard.
501
501
let mut query_router = QueryRouter :: new ( ) ;
502
- let mut round_robin = 0 ;
502
+ let mut round_robin = rand :: random ( ) ;
503
503
504
504
// Our custom protocol loop.
505
505
// We expect the client to either start a transaction with regular queries
@@ -970,17 +970,54 @@ where
970
970
}
971
971
972
972
async fn receive_server_message (
973
- & self ,
973
+ & mut self ,
974
974
server : & mut Server ,
975
975
address : & Address ,
976
976
shard : usize ,
977
977
pool : & ConnectionPool ,
978
978
) -> Result < BytesMut , Error > {
979
- match server. recv ( ) . await {
980
- Ok ( message) => Ok ( message) ,
981
- Err ( err) => {
982
- pool. ban ( address, shard, self . process_id ) ;
983
- Err ( err)
979
+ if pool. settings . user . statement_timeout > 0 {
980
+ match tokio:: time:: timeout (
981
+ tokio:: time:: Duration :: from_millis ( pool. settings . user . statement_timeout ) ,
982
+ server. recv ( ) ,
983
+ )
984
+ . await
985
+ {
986
+ Ok ( result) => match result {
987
+ Ok ( message) => Ok ( message) ,
988
+ Err ( err) => {
989
+ pool. ban ( address, shard, self . process_id ) ;
990
+ error_response_terminal (
991
+ & mut self . write ,
992
+ & format ! ( "error receiving data from server: {:?}" , err) ,
993
+ )
994
+ . await ?;
995
+ Err ( err)
996
+ }
997
+ } ,
998
+ Err ( _) => {
999
+ error ! (
1000
+ "Statement timeout while talking to {:?} with user {}" ,
1001
+ address, pool. settings. user. username
1002
+ ) ;
1003
+ server. mark_bad ( ) ;
1004
+ pool. ban ( address, shard, self . process_id ) ;
1005
+ error_response_terminal ( & mut self . write , "pool statement timeout" ) . await ?;
1006
+ Err ( Error :: StatementTimeout )
1007
+ }
1008
+ }
1009
+ } else {
1010
+ match server. recv ( ) . await {
1011
+ Ok ( message) => Ok ( message) ,
1012
+ Err ( err) => {
1013
+ pool. ban ( address, shard, self . process_id ) ;
1014
+ error_response_terminal (
1015
+ & mut self . write ,
1016
+ & format ! ( "error receiving data from server: {:?}" , err) ,
1017
+ )
1018
+ . await ?;
1019
+ Err ( err)
1020
+ }
984
1021
}
985
1022
}
986
1023
}
0 commit comments