@@ -774,6 +774,9 @@ where
774
774
let mut prepared_statement = None ;
775
775
let mut will_prepare = false ;
776
776
777
+ let client_identifier =
778
+ ClientIdentifier :: new ( & self . application_name , & self . username , & self . pool_name ) ;
779
+
777
780
// Our custom protocol loop.
778
781
// We expect the client to either start a transaction with regular queries
779
782
// or issue commands for our sharding and server selection protocol.
@@ -812,6 +815,21 @@ where
812
815
message_result = read_message( & mut self . read) => message_result?
813
816
} ;
814
817
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
+
815
833
match message[ 0 ] as char {
816
834
// Buffer extended protocol messages even if we do not have
817
835
// a server connection yet. Hopefully, when we get the S message
@@ -841,24 +859,34 @@ where
841
859
842
860
'Q' => {
843
861
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 ;
846
865
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
+ }
852
871
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
+ }
857
876
858
- _ => ( ) ,
859
- } ;
877
+ _ => ( ) ,
878
+ } ;
879
+
880
+ let _ = query_router. infer ( & ast) ;
860
881
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
+ }
862
890
}
863
891
}
864
892
}
@@ -872,13 +900,21 @@ where
872
900
self . buffer . put ( & message[ ..] ) ;
873
901
874
902
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
+ }
879
908
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
+ } ;
882
918
}
883
919
884
920
continue ;
@@ -922,13 +958,6 @@ where
922
958
_ => ( ) ,
923
959
}
924
960
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
-
932
961
// Check on plugin results.
933
962
match plugin_output {
934
963
Some ( PluginOutput :: Deny ( error) ) => {
@@ -941,11 +970,6 @@ where
941
970
_ => ( ) ,
942
971
} ;
943
972
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
-
949
973
// Check if the pool is paused and wait until it's resumed.
950
974
if pool. wait_paused ( ) . await {
951
975
// Refresh pool information, something might have changed.
@@ -1165,6 +1189,9 @@ where
1165
1189
None => {
1166
1190
trace ! ( "Waiting for message inside transaction or in session mode" ) ;
1167
1191
1192
+ // This is not an initial message so discard the initial_parsed_ast
1193
+ initial_parsed_ast. take ( ) ;
1194
+
1168
1195
match tokio:: time:: timeout (
1169
1196
idle_client_timeout_duration,
1170
1197
read_message ( & mut self . read ) ,
@@ -1221,7 +1248,22 @@ where
1221
1248
// Query
1222
1249
'Q' => {
1223
1250
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 {
1225
1267
let plugin_result = query_router. execute_plugins ( & ast) . await ;
1226
1268
1227
1269
match plugin_result {
@@ -1237,8 +1279,6 @@ where
1237
1279
1238
1280
_ => ( ) ,
1239
1281
} ;
1240
-
1241
- let _ = query_router. infer ( & ast) ;
1242
1282
}
1243
1283
}
1244
1284
debug ! ( "Sending query to server" ) ;
@@ -1290,7 +1330,7 @@ where
1290
1330
}
1291
1331
1292
1332
if query_router. query_parser_enabled ( ) {
1293
- if let Ok ( ast) = QueryRouter :: parse ( & message) {
1333
+ if let Ok ( ast) = query_router . parse ( & message) {
1294
1334
if let Ok ( output) = query_router. execute_plugins ( & ast) . await {
1295
1335
plugin_output = Some ( output) ;
1296
1336
}
0 commit comments