Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions lib/ld-eventsource/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ class Client
# if you want to use something other than the default `TCPSocket`; it must implement
# `open(uri, timeout)` to return a connected `Socket`
# @yieldparam [Client] client the new client instance, before opening the connection
#
#
def initialize(uri,
headers: {},
connect_timeout: DEFAULT_CONNECT_TIMEOUT,
Expand All @@ -107,7 +107,7 @@ def initialize(uri,
if socket_factory
http_client_options["socket_class"] = socket_factory
end

if proxy
@proxy = proxy
else
Expand Down Expand Up @@ -202,12 +202,12 @@ def closed?
end

private

def reset_http
@http_client.close if !@http_client.nil?
close_connection
end

def close_connection
@lock.synchronize do
@cxn.connection.close if [email protected]?
Expand Down Expand Up @@ -258,7 +258,7 @@ def connect
interval = @first_attempt ? 0 : @backoff.next_interval
@first_attempt = false
if interval > 0
@logger.info { "Will retry connection after #{'%.3f' % interval} seconds" }
@logger.info { "Will retry connection after #{'%.3f' % interval} seconds" }
sleep(interval)
end
cxn = nil
Expand All @@ -268,14 +268,14 @@ def connect
headers: build_headers
})
if cxn.status.code == 200
content_type = cxn.headers["content-type"]
content_type = cxn.content_type.mime_type
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cxn.content_type returns a struct, mime_type gets us the appropriate string which matches the header.

if content_type && content_type.start_with?("text/event-stream")
return cxn # we're good to proceed
else
reset_http
err = Errors::HTTPContentTypeError.new(cxn.headers["content-type"])
err = Errors::HTTPContentTypeError.new(content_type)
@on[:error].call(err)
@logger.warn { "Event source returned unexpected content type '#{cxn.headers["content-type"]}'" }
@logger.warn { "Event source returned unexpected content type '#{content_type}'" }
end
else
body = cxn.to_s # grab the whole response body in case it has error details
Expand Down Expand Up @@ -309,7 +309,7 @@ def read_stream(cxn)
# readpartial gives us a string, which may not be a valid UTF-8 string because a
# multi-byte character might not yet have been fully read, but BufferedLineReader
# will handle that.
rescue HTTP::TimeoutError
rescue HTTP::TimeoutError
# For historical reasons, we rethrow this as our own type
raise Errors::ReadTimeoutError.new(@read_timeout)
end
Expand Down Expand Up @@ -344,7 +344,7 @@ def log_and_dispatch_error(e, message)
@logger.warn { "#{message}: #{e.inspect}"}
@logger.debug { "Exception trace: #{e.backtrace}" }
begin
@on[:error].call(e)
@on[:error].call(e)
rescue StandardError => ee
@logger.warn { "Error handler threw an exception: #{ee.inspect}"}
@logger.debug { "Exception trace: #{ee.backtrace}" }
Expand Down