@@ -13,8 +13,8 @@ use tokio::{
1313use tracing:: { debug, error, info, trace, warn} ;
1414
1515use super :: {
16- pool:: Address , prepared_statements:: HandleResult , Error , PreparedStatements , ServerOptions ,
17- Stats ,
16+ pool:: Address , prepared_statements:: HandleResult , DisconnectReason , Error , PreparedStatements ,
17+ ServerOptions , Stats ,
1818} ;
1919use crate :: {
2020 auth:: { md5, scram:: Client } ,
@@ -61,6 +61,7 @@ pub struct Server {
6161 replication_mode : bool ,
6262 pooler_mode : PoolerMode ,
6363 stream_buffer : MessageBuffer ,
64+ disconnect_reason : Option < DisconnectReason > ,
6465}
6566
6667impl MemoryUsage for Server {
@@ -265,6 +266,7 @@ impl Server {
265266 re_synced : false ,
266267 pooler_mode : PoolerMode :: Transaction ,
267268 stream_buffer : MessageBuffer :: new ( cfg. config . memory . message_buffer ) ,
269+ disconnect_reason : None ,
268270 } ;
269271
270272 server. stats . memory_used ( server. memory_stats ( ) ) ; // Stream capacity.
@@ -839,6 +841,13 @@ impl Server {
839841 self . re_synced
840842 }
841843
844+ #[ inline]
845+ pub fn disconnect_reason ( & mut self , reason : DisconnectReason ) {
846+ if self . disconnect_reason . is_none ( ) {
847+ self . disconnect_reason = Some ( reason) ;
848+ }
849+ }
850+
842851 /// Server connection unique identifier.
843852 #[ inline]
844853 pub fn id ( & self ) -> & BackendKeyData {
@@ -956,15 +965,12 @@ impl Drop for Server {
956965 fn drop ( & mut self ) {
957966 self . stats ( ) . disconnect ( ) ;
958967 if let Some ( mut stream) = self . stream . take ( ) {
959- // If you see a lot of these, tell your clients
960- // to not send queries unless they are willing to stick
961- // around for results.
962- let out_of_sync = if self . done ( ) {
963- " " . into ( )
964- } else {
965- format ! ( " {} " , self . stats( ) . state)
966- } ;
967- info ! ( "closing{}server connection [{}]" , out_of_sync, self . addr, ) ;
968+ info ! (
969+ "closing server connection [{}, state: {}, reason: {}]" ,
970+ self . addr,
971+ self . stats. state,
972+ self . disconnect_reason. take( ) . unwrap_or_default( ) ,
973+ ) ;
968974
969975 spawn ( async move {
970976 stream. write_all ( & Terminate . to_bytes ( ) ?) . await ?;
@@ -1010,6 +1016,7 @@ pub mod test {
10101016 replication_mode : false ,
10111017 pooler_mode : PoolerMode :: Transaction ,
10121018 stream_buffer : MessageBuffer :: new ( 4096 ) ,
1019+ disconnect_reason : None ,
10131020 }
10141021 }
10151022 }
0 commit comments