Skip to content

Commit af9f0c0

Browse files
committed
polish
1 parent 514e6e0 commit af9f0c0

File tree

1 file changed

+23
-10
lines changed

1 file changed

+23
-10
lines changed

lib/splitclient-rb/sse/event_source/client.rb

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def initialize(config,
3838

3939
def close(status = nil)
4040
unless connected?
41-
@config.logger.error('SSEClient already disconected.') if @config.debug_enabled
41+
log_if_debug('SSEClient already disconected.', 3)
4242
return
4343
end
4444

@@ -76,10 +76,10 @@ def connected?
7676

7777
def connect_thread(latch)
7878
@config.threads[:connect_stream] = Thread.new do
79-
@config.logger.info('Starting connect_stream thread ...') if @config.debug_enabled
79+
log_if_debug('Starting connect_stream thread ...', 2)
8080
new_status = connect_stream(latch)
8181
push_status(new_status)
82-
@config.logger.info('connect_stream thread finished.') if @config.debug_enabled
82+
log_if_debug('connect_stream thread finished.', 2)
8383
end
8484
end
8585

@@ -95,21 +95,21 @@ def connect_stream(latch)
9595

9696
raise 'eof exception' if partial_data == :eof
9797
rescue Timeout::Error => e
98-
@config.logger.error("SSE read operation timed out!: #{e.inspect}") if @config.debug_enabled
98+
log_if_debug("SSE read operation timed out!: #{e.inspect}", 3)
9999
return nil
100100
rescue EOFError
101101
break
102102
rescue Errno::EAGAIN => e
103-
@config.logger.debug("SSE client transient error: #{e.inspect}") if @config.debug_enabled
103+
log_if_debug("SSE client transient error: #{e.inspect}", 1)
104104
IO.select([tcp_socket])
105105
retry
106106
rescue Errno::EBADF, IOError => e
107-
@config.logger.error(e.inspect) if @config.debug_enabled
107+
log_if_debug(e.inspect, 3)
108108
return nil
109109
rescue StandardError => e
110110
return nil if ENV['SPLITCLIENT_ENV'] == 'test'
111111

112-
@config.logger.error("Error reading partial data: #{e.inspect}") if @config.debug_enabled
112+
log_if_debug("Error reading partial data: #{e.inspect}", 3)
113113
return Constants::PUSH_RETRYABLE_ERROR
114114
end
115115

@@ -124,7 +124,7 @@ def socket_write(latch)
124124
@socket.puts(build_request(@uri))
125125
true
126126
rescue StandardError => e
127-
@config.logger.error("Error during connecting to #{@uri.host}. Error: #{e.inspect}")
127+
log_if_debug("Error during connecting to #{@uri.host}. Error: #{e.inspect}", 3)
128128
latch.count_down
129129
false
130130
end
@@ -171,7 +171,7 @@ def socket_connect
171171
def process_data(partial_data)
172172
return if partial_data.nil? || partial_data == KEEP_ALIVE_RESPONSE
173173

174-
@config.logger.debug("Event partial data: #{partial_data}") if @config.debug_enabled
174+
log_if_debug("Event partial data: #{partial_data}", 1)
175175
events = @event_parser.parse(partial_data)
176176
events.each { |event| process_event(event) }
177177
rescue StandardError => e
@@ -187,7 +187,7 @@ def build_request(uri)
187187
req << "SplitSDKMachineName: #{@config.machine_name}\r\n"
188188
req << "SplitSDKClientKey: #{@api_key.split(//).last(4).join}\r\n" unless @api_key.nil?
189189
req << "Cache-Control: no-cache\r\n\r\n"
190-
@config.logger.debug("Request info: #{req}") if @config.debug_enabled
190+
log_if_debug("Request info: #{req}", 1)
191191
req
192192
end
193193

@@ -225,6 +225,19 @@ def push_status(status)
225225
@config.logger.debug("Pushing new sse status: #{status}")
226226
@status_queue.push(status)
227227
end
228+
229+
def log_if_debug(text, level)
230+
if @config.debug_enabled
231+
case level
232+
when 1
233+
@config.logger.debug(text)
234+
when 2
235+
@config.logger.info(text)
236+
when 3
237+
@config.logger.error(text)
238+
end
239+
end
240+
end
228241
end
229242
end
230243
end

0 commit comments

Comments
 (0)