Skip to content

Commit 514e6e0

Browse files
committed
replace socketry with socket for sse client
1 parent 7cd64b1 commit 514e6e0

3 files changed

Lines changed: 34 additions & 10 deletions

File tree

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

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
# frozen_string_literal: false
22

3-
require 'socketry'
3+
require 'socket'
4+
require 'openssl'
45
require 'uri'
6+
require 'timeout'
57

68
module SplitIoClient
79
module SSE
@@ -41,7 +43,7 @@ def close(status = nil)
4143
end
4244

4345
@connected.make_false
44-
@socket&.close
46+
@socket.close
4547
push_status(status)
4648
rescue StandardError => e
4749
@config.logger.error("SSEClient close Error: #{e.inspect}")
@@ -83,14 +85,24 @@ def connect_thread(latch)
8385

8486
def connect_stream(latch)
8587
return Constants::PUSH_NONRETRYABLE_ERROR unless socket_write(latch)
86-
8788
while connected? || @first_event.value
8889
begin
89-
partial_data = @socket.readpartial(10_000, timeout: @read_timeout)
90-
90+
partial_data = ""
91+
Timeout::timeout @read_timeout do
92+
partial_data = @socket.readpartial(10_000)
93+
end
9194
read_first_event(partial_data, latch)
9295

9396
raise 'eof exception' if partial_data == :eof
97+
rescue Timeout::Error => e
98+
@config.logger.error("SSE read operation timed out!: #{e.inspect}") if @config.debug_enabled
99+
return nil
100+
rescue EOFError
101+
break
102+
rescue Errno::EAGAIN => e
103+
@config.logger.debug("SSE client transient error: #{e.inspect}") if @config.debug_enabled
104+
IO.select([tcp_socket])
105+
retry
94106
rescue Errno::EBADF, IOError => e
95107
@config.logger.error(e.inspect) if @config.debug_enabled
96108
return nil
@@ -109,7 +121,7 @@ def connect_stream(latch)
109121
def socket_write(latch)
110122
@first_event.make_true
111123
@socket = socket_connect
112-
@socket.write(build_request(@uri))
124+
@socket.puts(build_request(@uri))
113125
true
114126
rescue StandardError => e
115127
@config.logger.error("Error during connecting to #{@uri.host}. Error: #{e.inspect}")
@@ -138,9 +150,22 @@ def read_first_event(data, latch)
138150
end
139151

140152
def socket_connect
141-
return Socketry::SSL::Socket.connect(@uri.host, @uri.port) if @uri.scheme.casecmp('https').zero?
153+
tcp_socket = TCPSocket.new(@uri.host, @uri.port)
154+
if @uri.scheme.casecmp('https').zero?
155+
begin
156+
ssl_context = OpenSSL::SSL::SSLContext.new
157+
ssl_socket = OpenSSL::SSL::SSLSocket.new(tcp_socket, ssl_context)
158+
ssl_socket.hostname = @uri.host
159+
ssl_socket.connect
160+
return ssl_socket.connect
161+
rescue Exception => e
162+
@config.logger.error("socket connect error: #{e.inspect}")
163+
puts e.inspect
164+
return nil
165+
end
166+
end
142167

143-
Socketry::TCP::Socket.connect(@uri.host, @uri.port)
168+
tcp_socket
144169
end
145170

146171
def process_data(partial_data)

lib/splitclient-rb/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module SplitIoClient
2-
VERSION = '8.9.0'
2+
VERSION = '8.10.0-rc1'
33
end

splitclient-rb.gemspec

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,5 @@ Gem::Specification.new do |spec|
5959
spec.add_runtime_dependency 'lru_redux', '~> 1.1'
6060
spec.add_runtime_dependency 'net-http-persistent', '>= 2.9', '< 5.0'
6161
spec.add_runtime_dependency 'redis', '>= 4.0.0', '< 6.0'
62-
spec.add_runtime_dependency 'socketry', '>= 0.4', '< 1.0'
6362
spec.add_runtime_dependency 'thread_safe', '~> 0.3'
6463
end

0 commit comments

Comments
 (0)