@@ -66,7 +66,7 @@ function RedisClient(stream, options) {
66
66
this . server_info = { } ;
67
67
this . auth_pass = null ;
68
68
this . parser_module = null ;
69
- this . selected_db = null ; // save the selected db here, used when reconnecting
69
+ this . selected_db = null ; // save the selected db here, used when reconnecting
70
70
71
71
this . old_state = null ;
72
72
@@ -93,14 +93,23 @@ RedisClient.prototype.initialize_stream_listeners = function () {
93
93
self . on_error ( msg . message ) ;
94
94
} ) ;
95
95
96
- this . stream . on ( "close" , function ( ) {
97
- self . connection_gone ( "close" ) ;
96
+ this . stream . on ( "close" , function ( had_error ) {
97
+ if ( had_error !== true ) {
98
+ // Only in case the error event wasn't emitted earlier, to prevent duplicate call to connection_gone
99
+ self . connection_gone ( "close" ) ;
100
+ }
98
101
} ) ;
99
102
100
103
this . stream . on ( "end" , function ( ) {
101
104
self . connection_gone ( "end" ) ;
102
105
} ) ;
103
106
107
+ if ( this . options . socket_timeout ) {
108
+ this . stream . setTimeout ( this . options . socket_timeout , function ( ) {
109
+ self . stream . destroy ( ) ;
110
+ } ) ;
111
+ }
112
+
104
113
this . stream . on ( "drain" , function ( ) {
105
114
self . should_buffer = false ;
106
115
self . emit ( "drain" ) ;
@@ -824,12 +833,16 @@ RedisClient.prototype.pub_sub_command = function (command_obj) {
824
833
} ;
825
834
826
835
RedisClient . prototype . end = function ( ) {
827
- this . stream . _events = { } ;
828
836
this . connected = false ;
829
837
this . ready = false ;
830
838
this . closing = true ;
831
839
clearTimeout ( this . retry_timer ) ;
832
- return this . stream . end ( ) ;
840
+ if ( this . stream ) {
841
+ this . stream . _events = { } ;
842
+ if ( this . stream . end ) {
843
+ this . stream . end ( ) ;
844
+ }
845
+ }
833
846
} ;
834
847
835
848
function Multi ( client , args ) {
@@ -886,9 +899,9 @@ commands.forEach(function (command) {
886
899
887
900
// store db in this.select_db to restore it on reconnect
888
901
RedisClient . prototype . select = function ( db , callback ) {
889
- var self = this ;
902
+ var self = this ;
890
903
891
- this . send_command ( 'select' , [ db ] , function ( err , res ) {
904
+ this . send_command ( 'select' , [ db ] , function ( err , res ) {
892
905
if ( err === null ) {
893
906
self . selected_db = db ;
894
907
}
@@ -1122,4 +1135,4 @@ exports.print = function (err, reply) {
1122
1135
} else {
1123
1136
console . log ( "Reply: " + reply ) ;
1124
1137
}
1125
- } ;
1138
+ } ;
0 commit comments