@@ -70,10 +70,10 @@ class RoutingDriver extends Driver {
70
70
let url = 'UNKNOWN' ;
71
71
if ( conn ) {
72
72
url = conn . url ;
73
- this . _routingTable . writers . remove ( conn . url ) ;
73
+ this . _routingTable . forgetWriter ( conn . url ) ;
74
74
} else {
75
75
connectionPromise . then ( ( conn ) => {
76
- this . _routingTable . writers . remove ( conn . url ) ;
76
+ this . _routingTable . forgetWriter ( conn . url ) ;
77
77
} ) . catch ( ( ) => { /*ignore*/ } ) ;
78
78
}
79
79
return newError ( "No longer possible to write to server at " + url , SESSION_EXPIRED ) ;
@@ -118,35 +118,43 @@ class RoutingDriver extends Driver {
118
118
const refreshedTablePromise = knownRouters . reduce ( ( refreshedTablePromise , currentRouter , currentIndex ) => {
119
119
return refreshedTablePromise . then ( newRoutingTable => {
120
120
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
+ }
130
132
}
131
133
132
- const connection = this . _pool . acquire ( currentRouter ) ;
133
- const connectionPromise = Promise . resolve ( connection ) ;
134
- const session = this . _createSession ( connectionPromise , this . _releaseConnection ( connectionPromise ) ) ;
135
-
136
134
// try next router
135
+ const session = this . _createSessionForRediscovery ( currentRouter ) ;
137
136
return this . _rediscovery . lookupRoutingTableOnRouter ( session , currentRouter ) ;
138
137
} )
139
138
} , Promise . resolve ( null ) ) ;
140
139
141
140
return refreshedTablePromise . then ( newRoutingTable => {
142
- if ( newRoutingTable ) {
141
+ if ( newRoutingTable && ! newRoutingTable . writers . isEmpty ( ) ) {
143
142
this . _updateRoutingTable ( newRoutingTable ) ;
144
143
return newRoutingTable
145
144
}
146
145
throw newError ( 'Could not perform discovery. No routing servers available.' , SERVICE_UNAVAILABLE ) ;
147
146
} ) ;
148
147
}
149
148
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
+
150
158
_forget ( url ) {
151
159
this . _routingTable . forget ( url ) ;
152
160
this . _pool . purge ( url ) ;
0 commit comments