@@ -28,6 +28,7 @@ import {
28
28
ConnectionErrorHandler ,
29
29
DelegateConnection
30
30
} from '../connection'
31
+ import { functional } from '../lang'
31
32
32
33
const { SERVICE_UNAVAILABLE , SESSION_EXPIRED } = error
33
34
const {
@@ -85,6 +86,8 @@ export default class RoutingConnectionProvider extends PooledConnectionProvider
85
86
? int ( routingTablePurgeDelay )
86
87
: DEFAULT_ROUTING_TABLE_PURGE_DELAY
87
88
)
89
+
90
+ this . _refreshRoutingTable = functional . reuseOngoingRequest ( this . _refreshRoutingTable , this )
88
91
}
89
92
90
93
_createConnectionErrorHandler ( ) {
@@ -140,7 +143,7 @@ export default class RoutingConnectionProvider extends PooledConnectionProvider
140
143
const routingTable = await this . _freshRoutingTable ( {
141
144
accessMode,
142
145
database : context . database ,
143
- bookmark : bookmarks ,
146
+ bookmarks,
144
147
impersonatedUser,
145
148
onDatabaseNameResolved : ( databaseName ) => {
146
149
context . database = context . database || databaseName
@@ -259,7 +262,7 @@ export default class RoutingConnectionProvider extends PooledConnectionProvider
259
262
return this . _connectionPool . acquire ( address )
260
263
}
261
264
262
- _freshRoutingTable ( { accessMode, database, bookmark , impersonatedUser, onDatabaseNameResolved } = { } ) {
265
+ _freshRoutingTable ( { accessMode, database, bookmarks , impersonatedUser, onDatabaseNameResolved } = { } ) {
263
266
const currentRoutingTable = this . _routingTableRegistry . get (
264
267
database ,
265
268
( ) => new RoutingTable ( { database } )
@@ -271,44 +274,45 @@ export default class RoutingConnectionProvider extends PooledConnectionProvider
271
274
this . _log . info (
272
275
`Routing table is stale for database: "${ database } " and access mode: "${ accessMode } ": ${ currentRoutingTable } `
273
276
)
274
- return this . _refreshRoutingTable ( currentRoutingTable , bookmark , impersonatedUser , onDatabaseNameResolved )
277
+ return this . _refreshRoutingTable ( currentRoutingTable , bookmarks , impersonatedUser )
278
+ . then ( newRoutingTable => {
279
+ onDatabaseNameResolved ( newRoutingTable . database )
280
+ return newRoutingTable
281
+ } )
275
282
}
276
283
277
- _refreshRoutingTable ( currentRoutingTable , bookmark , impersonatedUser , onDatabaseNameResolved ) {
284
+ _refreshRoutingTable ( currentRoutingTable , bookmarks , impersonatedUser ) {
278
285
const knownRouters = currentRoutingTable . routers
279
286
280
287
if ( this . _useSeedRouter ) {
281
288
return this . _fetchRoutingTableFromSeedRouterFallbackToKnownRouters (
282
289
knownRouters ,
283
290
currentRoutingTable ,
284
- bookmark ,
285
- impersonatedUser ,
286
- onDatabaseNameResolved
291
+ bookmarks ,
292
+ impersonatedUser
287
293
)
288
294
}
289
295
return this . _fetchRoutingTableFromKnownRoutersFallbackToSeedRouter (
290
296
knownRouters ,
291
297
currentRoutingTable ,
292
- bookmark ,
293
- impersonatedUser ,
294
- onDatabaseNameResolved
298
+ bookmarks ,
299
+ impersonatedUser
295
300
)
296
301
}
297
302
298
303
async _fetchRoutingTableFromSeedRouterFallbackToKnownRouters (
299
304
knownRouters ,
300
305
currentRoutingTable ,
301
- bookmark ,
302
- impersonatedUser ,
303
- onDatabaseNameResolved
306
+ bookmarks ,
307
+ impersonatedUser
304
308
) {
305
309
// we start with seed router, no routers were probed before
306
310
const seenRouters = [ ]
307
311
let newRoutingTable = await this . _fetchRoutingTableUsingSeedRouter (
308
312
seenRouters ,
309
313
this . _seedRouter ,
310
314
currentRoutingTable ,
311
- bookmark ,
315
+ bookmarks ,
312
316
impersonatedUser
313
317
)
314
318
@@ -319,29 +323,27 @@ export default class RoutingConnectionProvider extends PooledConnectionProvider
319
323
newRoutingTable = await this . _fetchRoutingTableUsingKnownRouters (
320
324
knownRouters ,
321
325
currentRoutingTable ,
322
- bookmark ,
326
+ bookmarks ,
323
327
impersonatedUser
324
328
)
325
329
}
326
330
327
331
return await this . _applyRoutingTableIfPossible (
328
332
currentRoutingTable ,
329
- newRoutingTable ,
330
- onDatabaseNameResolved
333
+ newRoutingTable
331
334
)
332
335
}
333
336
334
337
async _fetchRoutingTableFromKnownRoutersFallbackToSeedRouter (
335
338
knownRouters ,
336
339
currentRoutingTable ,
337
- bookmark ,
340
+ bookmarks ,
338
341
impersonatedUser ,
339
- onDatabaseNameResolved
340
342
) {
341
343
let newRoutingTable = await this . _fetchRoutingTableUsingKnownRouters (
342
344
knownRouters ,
343
345
currentRoutingTable ,
344
- bookmark ,
346
+ bookmarks ,
345
347
impersonatedUser
346
348
)
347
349
@@ -351,28 +353,27 @@ export default class RoutingConnectionProvider extends PooledConnectionProvider
351
353
knownRouters ,
352
354
this . _seedRouter ,
353
355
currentRoutingTable ,
354
- bookmark ,
356
+ bookmarks ,
355
357
impersonatedUser
356
358
)
357
359
}
358
360
359
361
return await this . _applyRoutingTableIfPossible (
360
362
currentRoutingTable ,
361
- newRoutingTable ,
362
- onDatabaseNameResolved
363
+ newRoutingTable
363
364
)
364
365
}
365
366
366
367
async _fetchRoutingTableUsingKnownRouters (
367
368
knownRouters ,
368
369
currentRoutingTable ,
369
- bookmark ,
370
+ bookmarks ,
370
371
impersonatedUser
371
372
) {
372
373
const newRoutingTable = await this . _fetchRoutingTable (
373
374
knownRouters ,
374
375
currentRoutingTable ,
375
- bookmark ,
376
+ bookmarks ,
376
377
impersonatedUser
377
378
)
378
379
@@ -397,7 +398,7 @@ export default class RoutingConnectionProvider extends PooledConnectionProvider
397
398
seenRouters ,
398
399
seedRouter ,
399
400
routingTable ,
400
- bookmark ,
401
+ bookmarks ,
401
402
impersonatedUser
402
403
) {
403
404
const resolvedAddresses = await this . _resolveSeedRouter ( seedRouter )
@@ -407,7 +408,7 @@ export default class RoutingConnectionProvider extends PooledConnectionProvider
407
408
address => seenRouters . indexOf ( address ) < 0
408
409
)
409
410
410
- return await this . _fetchRoutingTable ( newAddresses , routingTable , bookmark , impersonatedUser )
411
+ return await this . _fetchRoutingTable ( newAddresses , routingTable , bookmarks , impersonatedUser )
411
412
}
412
413
413
414
async _resolveSeedRouter ( seedRouter ) {
@@ -419,7 +420,7 @@ export default class RoutingConnectionProvider extends PooledConnectionProvider
419
420
return [ ] . concat . apply ( [ ] , dnsResolvedAddresses )
420
421
}
421
422
422
- _fetchRoutingTable ( routerAddresses , routingTable , bookmark , impersonatedUser ) {
423
+ _fetchRoutingTable ( routerAddresses , routingTable , bookmarks , impersonatedUser ) {
423
424
return routerAddresses . reduce (
424
425
async ( refreshedTablePromise , currentRouter , currentIndex ) => {
425
426
const newRoutingTable = await refreshedTablePromise
@@ -441,7 +442,7 @@ export default class RoutingConnectionProvider extends PooledConnectionProvider
441
442
// try next router
442
443
const session = await this . _createSessionForRediscovery (
443
444
currentRouter ,
444
- bookmark ,
445
+ bookmarks ,
445
446
impersonatedUser
446
447
)
447
448
if ( session ) {
@@ -474,7 +475,7 @@ export default class RoutingConnectionProvider extends PooledConnectionProvider
474
475
)
475
476
}
476
477
477
- async _createSessionForRediscovery ( routerAddress , bookmark , impersonatedUser ) {
478
+ async _createSessionForRediscovery ( routerAddress , bookmarks , impersonatedUser ) {
478
479
try {
479
480
const connection = await this . _connectionPool . acquire ( routerAddress )
480
481
@@ -490,15 +491,15 @@ export default class RoutingConnectionProvider extends PooledConnectionProvider
490
491
if ( protocolVersion < 4.0 ) {
491
492
return new Session ( {
492
493
mode : WRITE ,
493
- bookmark : Bookmark . empty ( ) ,
494
+ bookmarks : Bookmark . empty ( ) ,
494
495
connectionProvider
495
496
} )
496
497
}
497
498
498
499
return new Session ( {
499
500
mode : READ ,
500
501
database : SYSTEM_DB_NAME ,
501
- bookmark ,
502
+ bookmarks ,
502
503
connectionProvider,
503
504
impersonatedUser
504
505
} )
@@ -513,7 +514,7 @@ export default class RoutingConnectionProvider extends PooledConnectionProvider
513
514
}
514
515
}
515
516
516
- async _applyRoutingTableIfPossible ( currentRoutingTable , newRoutingTable , onDatabaseNameResolved ) {
517
+ async _applyRoutingTableIfPossible ( currentRoutingTable , newRoutingTable ) {
517
518
if ( ! newRoutingTable ) {
518
519
// none of routing servers returned valid routing table, throw exception
519
520
throw newError (
@@ -528,21 +529,18 @@ export default class RoutingConnectionProvider extends PooledConnectionProvider
528
529
this . _useSeedRouter = true
529
530
}
530
531
531
- await this . _updateRoutingTable ( newRoutingTable , onDatabaseNameResolved )
532
+ await this . _updateRoutingTable ( newRoutingTable )
532
533
533
534
return newRoutingTable
534
535
}
535
536
536
- async _updateRoutingTable ( newRoutingTable , onDatabaseNameResolved ) {
537
+ async _updateRoutingTable ( newRoutingTable ) {
537
538
// close old connections to servers not present in the new routing table
538
539
await this . _connectionPool . keepAll ( newRoutingTable . allServers ( ) )
539
540
this . _routingTableRegistry . removeExpired ( )
540
541
this . _routingTableRegistry . register (
541
542
newRoutingTable
542
543
)
543
-
544
- onDatabaseNameResolved ( newRoutingTable . database )
545
-
546
544
this . _log . info ( `Updated routing table ${ newRoutingTable } ` )
547
545
}
548
546
0 commit comments