@@ -185,7 +185,18 @@ class Client extends EventEmitter {
185
185
}
186
186
return true
187
187
}
188
-
188
+ incrementConnectionCount ( ) {
189
+ let prevCount = 0
190
+ if ( Client . connectionMap . has ( this . host ) ) {
191
+ prevCount = Client . connectionMap . get ( this . host )
192
+ } else {
193
+ let serverInfo = Client . failedHosts . get ( this . host )
194
+ Client . hostServerInfo . set ( this . host , serverInfo )
195
+ Client . failedHosts . delete ( this . host )
196
+ }
197
+ // console.log('inc cnt', this.host)
198
+ Client . connectionMap . set ( this . host , prevCount + 1 )
199
+ }
189
200
_connect ( callback ) {
190
201
var self = this
191
202
var con = this . connection
@@ -227,20 +238,11 @@ class Client extends EventEmitter {
227
238
} else {
228
239
con . connect ( this . port , this . host )
229
240
}
241
+ // console.log('_connect() - connection created to', this.host)
230
242
231
- if ( this . connectionParameters . load_balance ) {
232
- let prevCount = 0
233
- if ( Client . connectionMap . has ( this . host ) ) {
234
- prevCount = Client . connectionMap . get ( this . host )
235
- } else {
236
- let serverInfo = Client . failedHosts . get ( this . host )
237
- Client . hostServerInfo . set ( this . host , serverInfo )
238
- Client . failedHosts . delete ( this . host )
239
- }
240
- Client . connectionMap . set ( this . host , prevCount + 1 )
241
- }
242
243
// once connection is established send startup message
243
244
con . on ( 'connect' , function ( ) {
245
+ // console.log("_connect() in on('connect') - connection created to", this.host)
244
246
if ( self . ssl ) {
245
247
con . requestSsl ( )
246
248
} else {
@@ -445,6 +447,7 @@ class Client extends EventEmitter {
445
447
. then ( ( res ) => {
446
448
result = res
447
449
lock . release ( )
450
+ this . incrementConnectionCount ( )
448
451
// console.log(Client.connectionMap)
449
452
// console.log('lock released first by', i)
450
453
} )
@@ -456,20 +459,26 @@ class Client extends EventEmitter {
456
459
Client . hostServerInfo . delete ( this . host )
457
460
this . nowConnect ( callback ) . then ( ( res ) => {
458
461
result = res
462
+ this . incrementConnectionCount ( )
463
+ lock . release ( )
464
+ // console.log(Client.connectionMap)
465
+ // console.log('lock released first by', i)
459
466
} )
460
467
} )
461
468
return result
462
469
} )
463
470
. catch ( ( err ) => {
464
471
let result = this . nowConnect ( callback )
465
472
lock . release ( )
473
+ // console.log(Client.connectionMap)
466
474
// console.log('lock released first by', i)
467
475
return result
468
476
} )
469
477
} )
470
478
. catch ( ( err ) => {
471
479
let result = this . nowConnect ( callback )
472
480
lock . release ( )
481
+ // console.log(Client.connectionMap)
473
482
// console.log('lock released first by', i)
474
483
return result
475
484
} )
@@ -484,6 +493,11 @@ class Client extends EventEmitter {
484
493
this . nowConnect ( callback )
485
494
. then ( ( res ) => {
486
495
result = res
496
+ this . incrementConnectionCount ( )
497
+ lock . release ( )
498
+ // console.log(Client.connectionMap)
499
+ // console.log('lock release after refresh by error', i)
500
+ return result
487
501
} )
488
502
. catch ( ( err ) => {
489
503
if ( Client . hostServerInfo . has ( this . host ) ) {
@@ -493,43 +507,51 @@ class Client extends EventEmitter {
493
507
Client . hostServerInfo . delete ( this . host )
494
508
this . nowConnect ( callback ) . then ( ( res ) => {
495
509
result = res
510
+ this . incrementConnectionCount ( )
511
+ lock . release ( )
512
+ // console.log(Client.connectionMap)
513
+ // console.log('lock release after refresh by error', i)
496
514
} )
515
+ return result
497
516
} )
498
- lock . release ( )
499
- // console.log(Client.connectionMap)
500
- // console.log('lock release after refresh by error', i)
501
- return result
502
517
} )
503
518
. catch ( ( err ) => {
504
519
this . nowConnect ( callback ) . then ( ( res ) => {
505
520
result = res
521
+ this . incrementConnectionCount ( )
522
+ lock . release ( )
523
+ // console.log(Client.connectionMap)
524
+ // console.log('lock release after refresh by', i)
525
+ return result
506
526
} )
507
- lock . release ( )
508
- // console.log(Client.connectionMap)
509
- // console.log('lock release after refresh by', i)
510
- return result
511
527
} )
512
528
} else {
513
529
// console.log('lock acquired without refresh by', i)
514
530
let result
515
531
this . nowConnect ( callback )
516
532
. then ( ( res ) => {
517
533
result = res
534
+ this . incrementConnectionCount ( )
535
+ lock . release ( )
536
+ // console.log(Client.connectionMap)
537
+ // console.log('lock release without refresh by', i)
538
+ return result
518
539
} )
519
540
. catch ( ( ) => {
520
541
if ( Client . hostServerInfo . has ( this . host ) ) {
521
542
Client . failedHosts . set ( this . host , Client . hostServerInfo . get ( this . host ) )
522
543
}
523
544
Client . connectionMap . delete ( this . host )
524
545
Client . hostServerInfo . delete ( this . host )
525
- this . nowConnect ( callback ) . then ( ( res ) => {
546
+ this . nowConnect ( callback ) . then ( ( res ) => { //retries untill successful or connectionMap empty
526
547
result = res
548
+ this . incrementConnectionCount ( )
527
549
} )
550
+ lock . release ( )
551
+ // console.log(Client.connectionMap)
552
+ // console.log('lock release without refresh by error', i)
553
+ return result
528
554
} )
529
- lock . release ( )
530
- // console.log(Client.connectionMap)
531
- // console.log('lock release without refresh by error', i)
532
- return result
533
555
}
534
556
}
535
557
} )
@@ -661,6 +683,7 @@ class Client extends EventEmitter {
661
683
// TODO(bmc): this is swallowing errors - we shouldn't do this
662
684
return
663
685
}
686
+ // console.log('in _handleErrorWhileConnecting')
664
687
this . _connectionError = true
665
688
clearTimeout ( this . connectionTimeoutHandle )
666
689
if ( this . _connectionCallback ) {
@@ -678,6 +701,7 @@ class Client extends EventEmitter {
678
701
}
679
702
this . _queryable = false
680
703
this . _errorAllQueries ( err )
704
+ // console.log('in _handleErrorEvent now emitting error')
681
705
this . emit ( 'error' , err )
682
706
}
683
707
0 commit comments