Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft: Replace bypass BufferedIO when IO#timeout is available #150

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
4 changes: 3 additions & 1 deletion lib/redis_client/ruby_connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ def read(timeout = nil)
end
rescue RedisClient::RESP3::UnknownType => error
raise RedisClient::ProtocolError, error.message
rescue IO::TimeoutError => error
raise RedisClient::ReadTimeoutError, error.message
rescue SystemCallError, IOError, OpenSSL::SSL::SSLError => error
raise ConnectionError, error.message
end
Expand Down Expand Up @@ -141,7 +143,7 @@ def connect
end
end

@io = BufferedIO.new(
@io = RubyConnection.buffered(
socket,
read_timeout: @read_timeout,
write_timeout: @write_timeout,
Expand Down
92 changes: 92 additions & 0 deletions lib/redis_client/ruby_connection/buffered_io.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,98 @@

class RedisClient
class RubyConnection
class << self
if IO.method_defined?(:timeout)
def buffered(io, **kwargs)
if io.is_a?(OpenSSL::SSL::SSLSocket)
BufferedIO.new(io, **kwargs) # https://github.com/ruby/openssl/pull/693
else
SimpleIO.new(io, **kwargs)
end
end
else
def buffered(io, **kwargs)
BufferedIO.new(io, **kwargs)
end
end
end

class SimpleIO
EOL = "\r\n".b.freeze
EOL_SIZE = EOL.bytesize

attr_accessor :read_timeout, :write_timeout

def initialize(io, read_timeout:, write_timeout:)
@io = io
@io.to_io.timeout = read_timeout
@read_timeout = read_timeout
@write_timeout = write_timeout
@blocking_reads = false
end

def with_timeout(new_timeout)
new_timeout = false if new_timeout == 0

previous_read_timeout = @read_timeout
previous_blocking_reads = @blocking_reads

if new_timeout
@read_timeout = new_timeout
else
@io.timeout = nil
@blocking_reads = true
end

begin
yield
ensure
@io.timeout = @read_timeout = previous_read_timeout
@blocking_reads = previous_blocking_reads
end
end

def write(string)
@io.to_io.timeout = @write_timeout
@io.write(string)
ensure
@io.to_io.timeout = @read_timeout
end

def getbyte
@io.getbyte or raise EOFError
end

def gets_chomp
@io.gets(EOL, chomp: true)
rescue IO::TimeoutError
raise unless @blocking_reads
end

def read_chomp(bytes)
buffer = @io.read(bytes + EOL_SIZE)
buffer.chomp!(EOL)
buffer
rescue IO::TimeoutError
raise unless @blocking_reads
end

def skip(offset)
@io.read(offset)
nil
rescue IO::TimeoutError
raise unless @blocking_reads
end

def closed?
@io.closed?
end

def close
@io.close
end
end

class BufferedIO
EOL = "\r\n".b.freeze
EOL_SIZE = EOL.bytesize
Expand Down
Loading