@@ -83,13 +83,36 @@ class RoutingDriver extends Driver {
8383 } ) ;
8484 }
8585
86- _refreshedRoutingTable ( ) {
86+ _acquireConnection ( mode ) {
87+ return this . _freshRoutingTable ( ) . then ( routingTable => {
88+ if ( mode === READ ) {
89+ return this . _acquireConnectionToServer ( routingTable . readers , "read" ) ;
90+ } else if ( mode === WRITE ) {
91+ return this . _acquireConnectionToServer ( routingTable . writers , "write" ) ;
92+ } else {
93+ throw newError ( 'Illegal session mode ' + mode ) ;
94+ }
95+ } ) ;
96+ }
97+
98+ _acquireConnectionToServer ( serversRoundRobinArray , serverName ) {
99+ const address = serversRoundRobinArray . next ( ) ;
100+ if ( ! address ) {
101+ return Promise . reject ( newError ( 'No ' + serverName + ' servers available' , SESSION_EXPIRED ) ) ;
102+ }
103+ return this . _pool . acquire ( address ) ;
104+ }
105+
106+ _freshRoutingTable ( ) {
87107 const currentRoutingTable = this . _routingTable ;
88108
89109 if ( ! currentRoutingTable . isStale ( ) ) {
90110 return Promise . resolve ( currentRoutingTable ) ;
91111 }
112+ return this . _refreshRoutingTable ( currentRoutingTable ) ;
113+ }
92114
115+ _refreshRoutingTable ( currentRoutingTable ) {
93116 const knownRouters = currentRoutingTable . routers . toArray ( ) ;
94117
95118 const refreshedTablePromise = knownRouters . reduce ( ( refreshedTablePromise , currentRouter , currentIndex ) => {
@@ -117,44 +140,29 @@ class RoutingDriver extends Driver {
117140
118141 return refreshedTablePromise . then ( newRoutingTable => {
119142 if ( newRoutingTable ) {
120- // valid routing table fetched, close old connections to servers not present in the new routing table
121- const staleServers = currentRoutingTable . serversDiff ( newRoutingTable ) ;
122- staleServers . forEach ( server => this . _pool . purge ) ;
123-
124- // make this driver instance aware of the new table
125- this . _routingTable = newRoutingTable ;
126-
143+ this . _updateRoutingTable ( newRoutingTable ) ;
127144 return newRoutingTable
128145 }
129146 throw newError ( 'Could not perform discovery. No routing servers available.' , SERVICE_UNAVAILABLE ) ;
130147 } ) ;
131148 }
132149
133- _acquireConnection ( mode ) {
134- return this . _refreshedRoutingTable ( ) . then ( routingTable => {
135- if ( mode === READ ) {
136- return this . _acquireConnectionToServer ( routingTable . readers , "read" ) ;
137- } else if ( mode === WRITE ) {
138- return this . _acquireConnectionToServer ( routingTable . writers , "write" ) ;
139- } else {
140- throw newError ( 'Illegal session mode ' + mode ) ;
141- }
142- } ) ;
143- }
144-
145- _acquireConnectionToServer ( serversRoundRobinArray , serverName ) {
146- const address = serversRoundRobinArray . next ( ) ;
147- if ( ! address ) {
148- return Promise . reject ( newError ( 'No ' + serverName + ' servers available' , SESSION_EXPIRED ) ) ;
149- }
150- return this . _pool . acquire ( address ) ;
151- }
152-
153150 _forget ( url ) {
154151 this . _routingTable . forget ( url ) ;
155152 this . _pool . purge ( url ) ;
156153 }
157154
155+ _updateRoutingTable ( newRoutingTable ) {
156+ const currentRoutingTable = this . _routingTable ;
157+
158+ // close old connections to servers not present in the new routing table
159+ const staleServers = currentRoutingTable . serversDiff ( newRoutingTable ) ;
160+ staleServers . forEach ( server => this . _pool . purge ) ;
161+
162+ // make this driver instance aware of the new table
163+ this . _routingTable = newRoutingTable ;
164+ }
165+
158166 static _validateConfig ( config ) {
159167 if ( config . trust === 'TRUST_ON_FIRST_USE' ) {
160168 throw newError ( 'The chosen trust mode is not compatible with a routing driver' ) ;
0 commit comments