@@ -78,7 +78,8 @@ export default class RoutingConnectionProvider extends PooledConnectionProvider
7878 this . _createConnectionErrorHandler ( ) ,
7979 this . _log ,
8080 await this . _clientCertificateHolder . getClientCertificate ( ) ,
81- this . _routingContext
81+ this . _routingContext ,
82+ this . _channelSsrCallback . bind ( this )
8283 )
8384 } )
8485
@@ -99,6 +100,8 @@ export default class RoutingConnectionProvider extends PooledConnectionProvider
99100 )
100101
101102 this . _refreshRoutingTable = functional . reuseOngoingRequest ( this . _refreshRoutingTable , this )
103+ this . _withSSR = 0
104+ this . _withoutSSR = 0
102105 }
103106
104107 _createConnectionErrorHandler ( ) {
@@ -139,19 +142,30 @@ export default class RoutingConnectionProvider extends PooledConnectionProvider
139142 * See {@link ConnectionProvider} for more information about this method and
140143 * its arguments.
141144 */
142- async acquireConnection ( { accessMode, database, bookmarks, impersonatedUser, onDatabaseNameResolved, auth } = { } ) {
143- let name
144- let address
145+ async acquireConnection ( { accessMode, database, bookmarks, impersonatedUser, onDatabaseNameResolved, auth, homeDb } = { } ) {
145146 const context = { database : database || DEFAULT_DB_NAME }
146147
147148 const databaseSpecificErrorHandler = new ConnectionErrorHandler (
148149 SESSION_EXPIRED ,
149150 ( error , address ) => this . _handleUnavailability ( error , address , context . database ) ,
150- ( error , address ) => this . _handleWriteFailure ( error , address , context . database ) ,
151- ( error , address , conn ) =>
152- this . _handleSecurityError ( error , address , conn , context . database )
151+ ( error , address ) => this . _handleWriteFailure ( error , address , homeDb ?? context . database ) ,
152+ ( error , address , conn ) => this . _handleSecurityError ( error , address , conn , context . database )
153153 )
154154
155+ let conn
156+ if ( this . SSREnabled ( ) && homeDb !== undefined && database === '' ) {
157+ const currentRoutingTable = this . _routingTableRegistry . get (
158+ homeDb ,
159+ ( ) => new RoutingTable ( { database : homeDb } )
160+ )
161+ if ( currentRoutingTable && ! currentRoutingTable . isStaleFor ( accessMode ) ) {
162+ conn = await this . getConnectionFromRoutingTable ( currentRoutingTable , auth , accessMode , databaseSpecificErrorHandler )
163+ if ( this . SSREnabled ( ) ) {
164+ return conn
165+ }
166+ conn . release ( )
167+ }
168+ }
155169 const routingTable = await this . _freshRoutingTable ( {
156170 accessMode,
157171 database : context . database ,
@@ -165,7 +179,12 @@ export default class RoutingConnectionProvider extends PooledConnectionProvider
165179 }
166180 }
167181 } )
182+ return this . getConnectionFromRoutingTable ( routingTable , auth , accessMode , databaseSpecificErrorHandler )
183+ }
168184
185+ async getConnectionFromRoutingTable ( routingTable , auth , accessMode , databaseSpecificErrorHandler ) {
186+ let name
187+ let address
169188 // select a target server based on specified access mode
170189 if ( accessMode === READ ) {
171190 address = this . _loadBalancingStrategy . selectReader ( routingTable . readers )
@@ -663,6 +682,28 @@ export default class RoutingConnectionProvider extends PooledConnectionProvider
663682 routingTable . forgetRouter ( address )
664683 }
665684 }
685+
686+ _channelSsrCallback ( isEnabled , action ) {
687+ if ( action === 'OPEN' ) {
688+ if ( isEnabled === true ) {
689+ this . _withSSR = this . _withSSR + 1
690+ } else {
691+ this . _withoutSSR = this . _withoutSSR + 1
692+ }
693+ } else if ( action === 'CLOSE' ) {
694+ if ( isEnabled === true ) {
695+ this . _withSSR = this . _withSSR - 1
696+ } else {
697+ this . _withoutSSR = this . _withoutSSR - 1
698+ }
699+ } else {
700+ throw newError ( "Channel SSR Callback invoked with action other than 'OPEN' or 'CLOSE'" )
701+ }
702+ }
703+
704+ SSREnabled ( ) {
705+ return this . _withSSR > 0 && this . _withoutSSR === 0
706+ }
666707}
667708
668709/**
0 commit comments