Class: RedisClient::RubyConnection::BufferedIO

Inherits:
Object
  • Object
show all
Defined in:
lib/redis_client/ruby_connection/buffered_io.rb

Constant Summary collapse

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io, read_timeout:, write_timeout:, chunk_size: 4096) ⇒ BufferedIO

Returns a new instance of BufferedIO.



13
14
15
16
17
18
19
20
21
# File 'lib/redis_client/ruby_connection/buffered_io.rb', line 13

def initialize(io, read_timeout:, write_timeout:, chunk_size: 4096)
  @io = io
  @buffer = "".b
  @offset = 0
  @chunk_size = chunk_size
  @read_timeout = read_timeout
  @write_timeout = write_timeout
  @blocking_reads = false
end

Instance Attribute Details

#read_timeoutObject

Returns the value of attribute read_timeout.



11
12
13
# File 'lib/redis_client/ruby_connection/buffered_io.rb', line 11

def read_timeout
  @read_timeout
end

#write_timeoutObject

Returns the value of attribute write_timeout.



11
12
13
# File 'lib/redis_client/ruby_connection/buffered_io.rb', line 11

def write_timeout
  @write_timeout
end

Instance Method Details

#closeObject



23
24
25
# File 'lib/redis_client/ruby_connection/buffered_io.rb', line 23

def close
  @io.to_io.close
end

#closed?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/redis_client/ruby_connection/buffered_io.rb', line 27

def closed?
  @io.to_io.closed?
end

#eof?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/redis_client/ruby_connection/buffered_io.rb', line 31

def eof?
  @offset >= @buffer.bytesize && @io.eof?
end

#getbyteObject



84
85
86
87
88
89
# File 'lib/redis_client/ruby_connection/buffered_io.rb', line 84

def getbyte
  ensure_remaining(1)
  byte = @buffer.getbyte(@offset)
  @offset += 1
  byte
end

#gets_chompObject



91
92
93
94
95
96
97
98
99
100
# File 'lib/redis_client/ruby_connection/buffered_io.rb', line 91

def gets_chomp
  fill_buffer(false) if @offset >= @buffer.bytesize
  until eol_index = @buffer.index(EOL, @offset)
    fill_buffer(false)
  end

  line = @buffer.byteslice(@offset, eol_index - @offset)
  @offset = eol_index + EOL_SIZE
  line
end

#read_chomp(bytes) ⇒ Object



102
103
104
105
106
107
# File 'lib/redis_client/ruby_connection/buffered_io.rb', line 102

def read_chomp(bytes)
  ensure_remaining(bytes + EOL_SIZE)
  str = @buffer.byteslice(@offset, bytes)
  @offset += bytes + EOL_SIZE
  str
end

#skip(offset) ⇒ Object



55
56
57
58
59
# File 'lib/redis_client/ruby_connection/buffered_io.rb', line 55

def skip(offset)
  ensure_remaining(offset)
  @offset += offset
  nil
end

#with_timeout(new_timeout) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/redis_client/ruby_connection/buffered_io.rb', line 35

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
    @blocking_reads = true
  end

  begin
    yield
  ensure
    @read_timeout = previous_read_timeout
    @blocking_reads = previous_blocking_reads
  end
end

#write(string) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/redis_client/ruby_connection/buffered_io.rb', line 61

def write(string)
  total = remaining = string.bytesize
  loop do
    case bytes_written = @io.write_nonblock(string, exception: false)
    when Integer
      remaining -= bytes_written
      if remaining > 0
        string = string.byteslice(bytes_written..-1)
      else
        return total
      end
    when :wait_readable
      @io.to_io.wait_readable(@read_timeout) or raise(ReadTimeoutError, "Waited #{@read_timeout} seconds")
    when :wait_writable
      @io.to_io.wait_writable(@write_timeout) or raise(WriteTimeoutError, "Waited #{@write_timeout} seconds")
    when nil
      raise Errno::ECONNRESET
    else
      raise "Unexpected `write_nonblock` return: #{bytes.inspect}"
    end
  end
end