@@ -70,10 +70,10 @@ class RoutingDriver extends Driver {
7070 let url = 'UNKNOWN' ;
7171 if ( conn ) {
7272 url = conn . url ;
73- this . _routingTable . writers . remove ( conn . url ) ;
73+ this . _routingTable . forgetWriter ( conn . url ) ;
7474 } else {
7575 connectionPromise . then ( ( conn ) => {
76- this . _routingTable . writers . remove ( conn . url ) ;
76+ this . _routingTable . forgetWriter ( conn . url ) ;
7777 } ) . catch ( ( ) => { /*ignore*/ } ) ;
7878 }
7979 return newError ( "No longer possible to write to server at " + url , SESSION_EXPIRED ) ;
@@ -118,35 +118,43 @@ class RoutingDriver extends Driver {
118118 const refreshedTablePromise = knownRouters . reduce ( ( refreshedTablePromise , currentRouter , currentIndex ) => {
119119 return refreshedTablePromise . then ( newRoutingTable => {
120120 if ( newRoutingTable ) {
121- // correct routing table was fetched, just return it
122- return newRoutingTable
123- }
124-
125- // returned routing table was undefined, this means a connection error happened and we need to forget the
126- // previous router and try the next one
127- const previousRouter = knownRouters [ currentIndex - 1 ] ;
128- if ( previousRouter ) {
129- this . _forget ( previousRouter ) ;
121+ if ( ! newRoutingTable . writers . isEmpty ( ) ) {
122+ // valid routing table was fetched - just return it, try next router otherwise
123+ return newRoutingTable ;
124+ }
125+ } else {
126+ // returned routing table was undefined, this means a connection error happened and we need to forget the
127+ // previous router and try the next one
128+ const previousRouter = knownRouters [ currentIndex - 1 ] ;
129+ if ( previousRouter ) {
130+ currentRoutingTable . forgetRouter ( previousRouter ) ;
131+ }
130132 }
131133
132- const connection = this . _pool . acquire ( currentRouter ) ;
133- const connectionPromise = Promise . resolve ( connection ) ;
134- const session = this . _createSession ( connectionPromise , this . _releaseConnection ( connectionPromise ) ) ;
135-
136134 // try next router
135+ const session = this . _createSessionForRediscovery ( currentRouter ) ;
137136 return this . _rediscovery . lookupRoutingTableOnRouter ( session , currentRouter ) ;
138137 } )
139138 } , Promise . resolve ( null ) ) ;
140139
141140 return refreshedTablePromise . then ( newRoutingTable => {
142- if ( newRoutingTable ) {
141+ if ( newRoutingTable && ! newRoutingTable . writers . isEmpty ( ) ) {
143142 this . _updateRoutingTable ( newRoutingTable ) ;
144143 return newRoutingTable
145144 }
146145 throw newError ( 'Could not perform discovery. No routing servers available.' , SERVICE_UNAVAILABLE ) ;
147146 } ) ;
148147 }
149148
149+ _createSessionForRediscovery ( routerAddress ) {
150+ const connection = this . _pool . acquire ( routerAddress ) ;
151+ const connectionPromise = Promise . resolve ( connection ) ;
152+ // error transformer here is a no-op unlike the one in a regular session, this is so because errors are
153+ // handled in the rediscovery promise chain and we do not need to do this in the error transformer
154+ const errorTransformer = error => error ;
155+ return new RoutingSession ( connectionPromise , this . _releaseConnection ( connectionPromise ) , errorTransformer ) ;
156+ }
157+
150158 _forget ( url ) {
151159 this . _routingTable . forget ( url ) ;
152160 this . _pool . purge ( url ) ;
0 commit comments