Skip to content

Commit

Permalink
Reconnect if client closes connection
Browse files Browse the repository at this point in the history
  • Loading branch information
bendangelo committed Nov 19, 2023
1 parent 1a24b2b commit 56374c5
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions lib/sonic/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,36 @@ def disconnect
end

def read
data = socket.gets.chomp
data = socket.gets&.chomp

raise ServerError, data if data.nil?
raise ServerError, data if data.start_with?('ENDED ')
raise ServerError, data if data.start_with?('ERR ')

data
end

def write(data)
socket.puts(data)
begin
socket.puts(data)
rescue Errno::EPIPE
reconnect
socket.puts(data)
end
end


private

def socket
@socket ||= TCPSocket.open(@host, @port)
end

def reconnect
@socket.close if @socket
@socket = TCPSocket.open(@host, @port)

connect
end
end
end
Binary file added sonic-ruby-0.2.3.gem
Binary file not shown.

0 comments on commit 56374c5

Please sign in to comment.