@@ -558,13 +558,13 @@ RPC.prototype._accept_new_connection = function(conn) {
558558 conn . _sent_requests = new Map ( ) ;
559559 conn . _received_requests = new Map ( ) ;
560560 if ( self . _disconnected_state ) {
561- conn . close ( ) ;
562- throw new Error ( 'RPC IN DISCONNECTED STATE - rejecting connection ' + conn . connid ) ;
561+ const err = new Error ( 'RPC IN DISCONNECTED STATE - rejecting connection ' + conn . connid ) ;
562+ conn . emit ( 'error' , err ) ;
563+ throw err ;
563564 }
564565 conn . on ( 'message' , msg => self . _on_message ( conn , msg ) ) ;
565566 conn . on ( 'close' , err => self . _connection_closed ( conn , err ) ) ;
566- // we prefer to let the connection handle it's own errors and decide if to close or not
567- // conn.on('error', self._connection_error.bind(self, conn));
567+ // we let the connection handle it's own errors and decide if to close or not
568568
569569 if ( ! conn . transient ) {
570570 // always replace previous connection in the address map,
@@ -575,11 +575,21 @@ RPC.prototype._accept_new_connection = function(conn) {
575575 // send pings to keepalive
576576 conn . _ping_interval = setInterval ( function ( ) {
577577 dbg . log4 ( 'RPC PING' , conn . connid ) ;
578- conn . _ping_last_reqid = conn . _alloc_reqid ( ) ;
578+ const reqid = conn . _alloc_reqid ( ) ;
579+ conn . _ping_reqid_set = conn . _ping_reqid_set || new Set ( ) ;
580+ conn . _ping_reqid_set . add ( reqid ) ;
581+ if ( conn . _ping_reqid_set . size > 3 ) {
582+ const err = new Error ( `RPC PINGPONG EXHAUSTED pings ${
583+ Array . from ( conn . _ping_reqid_set ) . join ( ',' )
584+ } connid ${ conn . connid } `) ;
585+ dbg . warn ( err ) ;
586+ conn . emit ( 'error' , err ) ;
587+ return null ;
588+ }
579589 P . resolve ( )
580590 . then ( ( ) => conn . send ( RpcRequest . encode_message ( {
581591 op : 'ping' ,
582- reqid : conn . _ping_last_reqid
592+ reqid : reqid
583593 } ) ) )
584594 . catch ( _ . noop ) ; // already means the conn is closed
585595 return null ;
@@ -652,15 +662,6 @@ RPC.prototype.disconnect_all = function() {
652662} ;
653663
654664
655- /**
656- *
657- */
658- RPC . prototype . _connection_error = function ( conn , err ) {
659- dbg . error ( 'RPC CONNECTION ERROR:' , conn . connid , conn . url . href , err . stack || err ) ;
660- conn . close ( ) ;
661- } ;
662-
663-
664665/**
665666 *
666667 */
@@ -746,16 +747,14 @@ RPC.prototype._on_message = function(conn, msg_buffer) {
746747 . catch ( _ . noop ) ; // already means the conn is closed
747748 break ;
748749 case 'pong' :
749- if ( conn . _ping_last_reqid === msg . header . reqid ) {
750+ if ( conn . _ping_reqid_set && conn . _ping_reqid_set . has ( msg . header . reqid ) ) {
750751 dbg . log4 ( 'RPC PINGPONG' , conn . connid ) ;
751- conn . _ping_mismatch_count = 0 ;
752+ conn . _ping_reqid_set . delete ( msg . header . reqid ) ;
752753 } else {
753- conn . _ping_mismatch_count = ( conn . _ping_mismatch_count || 0 ) + 1 ;
754- if ( conn . _ping_mismatch_count < 3 ) {
755- dbg . warn ( 'RPC PINGPONG MISMATCH #' + conn . _ping_mismatch_count , conn . connid ) ;
756- } else {
757- conn . close ( new Error ( 'RPC PINGPONG MISMATCH #' + conn . _ping_mismatch_count + ' ' + conn . connid ) ) ;
758- }
754+ dbg . warn ( `RPC PINGPONG MISMATCH pings ${
755+ Array . from ( conn . _ping_reqid_set ) . join ( ',' )
756+ } pong ${ msg . header . reqid
757+ } connid ${ conn . connid } `) ;
759758 }
760759 break ;
761760 default :
0 commit comments