@@ -774,6 +774,9 @@ where
774774 let mut prepared_statement = None ;
775775 let mut will_prepare = false ;
776776
777+ let client_identifier =
778+ ClientIdentifier :: new ( & self . application_name , & self . username , & self . pool_name ) ;
779+
777780 // Our custom protocol loop.
778781 // We expect the client to either start a transaction with regular queries
779782 // or issue commands for our sharding and server selection protocol.
@@ -812,6 +815,21 @@ where
812815 message_result = read_message( & mut self . read) => message_result?
813816 } ;
814817
818+ // Handle admin database queries.
819+ if self . admin {
820+ debug ! ( "Handling admin command" ) ;
821+ handle_admin ( & mut self . write , message, self . client_server_map . clone ( ) ) . await ?;
822+ continue ;
823+ }
824+
825+ // Get a pool instance referenced by the most up-to-date
826+ // pointer. This ensures we always read the latest config
827+ // when starting a query.
828+ let mut pool = self . get_pool ( ) . await ?;
829+ query_router. update_pool_settings ( pool. settings . clone ( ) ) ;
830+
831+ let mut initial_parsed_ast = None ;
832+
815833 match message[ 0 ] as char {
816834 // Buffer extended protocol messages even if we do not have
817835 // a server connection yet. Hopefully, when we get the S message
@@ -841,24 +859,34 @@ where
841859
842860 'Q' => {
843861 if query_router. query_parser_enabled ( ) {
844- if let Ok ( ast) = QueryRouter :: parse ( & message) {
845- let plugin_result = query_router. execute_plugins ( & ast) . await ;
862+ match query_router. parse ( & message) {
863+ Ok ( ast) => {
864+ let plugin_result = query_router. execute_plugins ( & ast) . await ;
846865
847- match plugin_result {
848- Ok ( PluginOutput :: Deny ( error) ) => {
849- error_response ( & mut self . write , & error) . await ?;
850- continue ;
851- }
866+ match plugin_result {
867+ Ok ( PluginOutput :: Deny ( error) ) => {
868+ error_response ( & mut self . write , & error) . await ?;
869+ continue ;
870+ }
852871
853- Ok ( PluginOutput :: Intercept ( result) ) => {
854- write_all ( & mut self . write , result) . await ?;
855- continue ;
856- }
872+ Ok ( PluginOutput :: Intercept ( result) ) => {
873+ write_all ( & mut self . write , result) . await ?;
874+ continue ;
875+ }
857876
858- _ => ( ) ,
859- } ;
877+ _ => ( ) ,
878+ } ;
879+
880+ let _ = query_router. infer ( & ast) ;
860881
861- let _ = query_router. infer ( & ast) ;
882+ initial_parsed_ast = Some ( ast) ;
883+ }
884+ Err ( error) => {
885+ warn ! (
886+ "Query parsing error: {} (client: {})" ,
887+ error, client_identifier
888+ ) ;
889+ }
862890 }
863891 }
864892 }
@@ -872,13 +900,21 @@ where
872900 self . buffer . put ( & message[ ..] ) ;
873901
874902 if query_router. query_parser_enabled ( ) {
875- if let Ok ( ast) = QueryRouter :: parse ( & message) {
876- if let Ok ( output) = query_router. execute_plugins ( & ast) . await {
877- plugin_output = Some ( output) ;
878- }
903+ match query_router. parse ( & message) {
904+ Ok ( ast) => {
905+ if let Ok ( output) = query_router. execute_plugins ( & ast) . await {
906+ plugin_output = Some ( output) ;
907+ }
879908
880- let _ = query_router. infer ( & ast) ;
881- }
909+ let _ = query_router. infer ( & ast) ;
910+ }
911+ Err ( error) => {
912+ warn ! (
913+ "Query parsing error: {} (client: {})" ,
914+ error, client_identifier
915+ ) ;
916+ }
917+ } ;
882918 }
883919
884920 continue ;
@@ -922,13 +958,6 @@ where
922958 _ => ( ) ,
923959 }
924960
925- // Handle admin database queries.
926- if self . admin {
927- debug ! ( "Handling admin command" ) ;
928- handle_admin ( & mut self . write , message, self . client_server_map . clone ( ) ) . await ?;
929- continue ;
930- }
931-
932961 // Check on plugin results.
933962 match plugin_output {
934963 Some ( PluginOutput :: Deny ( error) ) => {
@@ -941,11 +970,6 @@ where
941970 _ => ( ) ,
942971 } ;
943972
944- // Get a pool instance referenced by the most up-to-date
945- // pointer. This ensures we always read the latest config
946- // when starting a query.
947- let mut pool = self . get_pool ( ) . await ?;
948-
949973 // Check if the pool is paused and wait until it's resumed.
950974 if pool. wait_paused ( ) . await {
951975 // Refresh pool information, something might have changed.
@@ -1165,6 +1189,9 @@ where
11651189 None => {
11661190 trace ! ( "Waiting for message inside transaction or in session mode" ) ;
11671191
1192+ // This is not an initial message so discard the initial_parsed_ast
1193+ initial_parsed_ast. take ( ) ;
1194+
11681195 match tokio:: time:: timeout (
11691196 idle_client_timeout_duration,
11701197 read_message ( & mut self . read ) ,
@@ -1221,7 +1248,22 @@ where
12211248 // Query
12221249 'Q' => {
12231250 if query_router. query_parser_enabled ( ) {
1224- if let Ok ( ast) = QueryRouter :: parse ( & message) {
1251+ // We don't want to parse again if we already parsed it as the initial message
1252+ let ast = match initial_parsed_ast {
1253+ Some ( _) => Some ( initial_parsed_ast. take ( ) . unwrap ( ) ) ,
1254+ None => match query_router. parse ( & message) {
1255+ Ok ( ast) => Some ( ast) ,
1256+ Err ( error) => {
1257+ warn ! (
1258+ "Query parsing error: {} (client: {})" ,
1259+ error, client_identifier
1260+ ) ;
1261+ None
1262+ }
1263+ } ,
1264+ } ;
1265+
1266+ if let Some ( ast) = ast {
12251267 let plugin_result = query_router. execute_plugins ( & ast) . await ;
12261268
12271269 match plugin_result {
@@ -1237,8 +1279,6 @@ where
12371279
12381280 _ => ( ) ,
12391281 } ;
1240-
1241- let _ = query_router. infer ( & ast) ;
12421282 }
12431283 }
12441284 debug ! ( "Sending query to server" ) ;
@@ -1290,7 +1330,7 @@ where
12901330 }
12911331
12921332 if query_router. query_parser_enabled ( ) {
1293- if let Ok ( ast) = QueryRouter :: parse ( & message) {
1333+ if let Ok ( ast) = query_router . parse ( & message) {
12941334 if let Ok ( output) = query_router. execute_plugins ( & ast) . await {
12951335 plugin_output = Some ( output) ;
12961336 }
0 commit comments