@@ -35,11 +35,15 @@ def on_action(&action)
3535 @on [ :action ] = action
3636 end
3737
38- def close ( action = Constants ::PUSH_NONRETRYABLE_ERROR )
39- dispatch_action ( action )
38+ def close ( action = nil )
39+ unless connected?
40+ @config . logger . error ( 'SSEClient already disconected.' ) if @config . debug_enabled
41+ return
42+ end
43+
4044 @connected . make_false
41- SplitIoClient ::Helpers ::ThreadHelper . stop ( :connect_stream , @config )
4245 @socket &.close
46+ dispatch_action ( action ) unless action . nil?
4347 rescue StandardError => e
4448 @config . logger . error ( "SSEClient close Error: #{ e . inspect } " )
4549 end
@@ -72,12 +76,14 @@ def connected?
7276 def connect_thread ( latch )
7377 @config . threads [ :connect_stream ] = Thread . new do
7478 @config . logger . info ( 'Starting connect_stream thread ...' ) if @config . debug_enabled
75- connect_stream ( latch )
79+ action = connect_stream ( latch )
80+ dispatch_action ( action ) unless action . nil?
81+ @config . logger . info ( 'connect_stream thread finished.' ) if @config . debug_enabled
7682 end
7783 end
7884
7985 def connect_stream ( latch )
80- socket_write ( latch )
86+ return Constants :: PUSH_NONRETRYABLE_ERROR unless socket_write ( latch )
8187
8288 while connected? || @first_event . value
8389 begin
@@ -86,24 +92,30 @@ def connect_stream(latch)
8692 read_first_event ( partial_data , latch )
8793
8894 raise 'eof exception' if partial_data == :eof
95+ rescue Errno ::EBADF , IOError => e
96+ @config . logger . error ( e . inspect )
97+ return nil
8998 rescue StandardError => e
90- @config . logger . error ( 'Error reading partial data: ' + e . inspect ) if @config . debug_enabled
91- close ( Constants ::PUSH_RETRYABLE_ERROR )
92- return
99+ return nil if ENV [ 'SPLITCLIENT_ENV' ] == 'test'
100+
101+ @config . logger . error ( "Error reading partial data: #{ e . inspect } " ) if @config . debug_enabled
102+ return Constants ::PUSH_RETRYABLE_ERROR
93103 end
94104
95105 process_data ( partial_data )
96106 end
107+ nil
97108 end
98109
99110 def socket_write ( latch )
100111 @first_event . make_true
101112 @socket = socket_connect
102113 @socket . write ( build_request ( @uri ) )
114+ true
103115 rescue StandardError => e
104116 @config . logger . error ( "Error during connecting to #{ @uri . host } . Error: #{ e . inspect } " )
105- close ( Constants ::PUSH_NONRETRYABLE_ERROR )
106117 latch . count_down
118+ false
107119 end
108120
109121 def read_first_event ( data , latch )
@@ -181,8 +193,10 @@ def dispatch_event(event)
181193 end
182194
183195 def dispatch_action ( action )
184- @config . logger . debug ( "Dispatching action: #{ action } " ) if @config . debug_enabled
185- @on [ :action ] . call ( action )
196+ @config . threads [ :dispatch_action ] = Thread . new do
197+ @config . logger . debug ( "Dispatching action: #{ action } " ) if @config . debug_enabled
198+ @on [ :action ] . call ( action )
199+ end
186200 end
187201 end
188202 end
0 commit comments