Skip to content

Commit

Permalink
Fix compatibility with TruffleRuby
Browse files Browse the repository at this point in the history
The behavior of `IO#read_nonblock` differs sligthly.
See: ruby/spec#1145
  • Loading branch information
byroot committed Mar 23, 2024
1 parent 8c1c68b commit a82593c
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions lib/redis_client/ruby_connection/buffered_io.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ class BufferedIO

attr_accessor :read_timeout, :write_timeout

def initialize(io, read_timeout:, write_timeout:, chunk_size: 4096)
def initialize(io, read_timeout:, write_timeout:, chunk_size: 4096, encoding: Encoding.default_external)
@io = io
@buffer = "".dup.force_encoding(Encoding.default_external)
@encoding = encoding
@buffer = "".dup.force_encoding(@encoding)
@offset = 0
@chunk_size = chunk_size
@read_timeout = read_timeout
Expand Down Expand Up @@ -147,6 +148,9 @@ def ensure_remaining(bytes)
end
end

RESET_BUFFER_ENCODING = RUBY_ENGINE == "truffleruby"
private_constant :RESET_BUFFER_ENCODING

def fill_buffer(strict, size = @chunk_size)
remaining = size
start = @offset - @buffer.bytesize
Expand All @@ -171,8 +175,9 @@ def fill_buffer(strict, size = @chunk_size)
if empty_buffer
@offset = start
empty_buffer = false
@buffer.force_encoding(@encoding) if RESET_BUFFER_ENCODING
else
@buffer << bytes
@buffer << bytes.force_encoding(@encoding)
end
remaining -= bytes.bytesize
return if !strict || remaining <= 0
Expand Down

0 comments on commit a82593c

Please sign in to comment.