redis-client 0.2.1 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,151 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "io/wait" unless IO.method_defined?(:wait_readable) && IO.method_defined?(:wait_writable)
4
-
5
- class RedisClient
6
- class BufferedIO
7
- EOL = "\r\n".b.freeze
8
- EOL_SIZE = EOL.bytesize
9
-
10
- attr_accessor :read_timeout, :write_timeout
11
-
12
- def initialize(io, read_timeout:, write_timeout:, chunk_size: 4096)
13
- @io = io
14
- @buffer = "".b
15
- @offset = 0
16
- @chunk_size = chunk_size
17
- @read_timeout = read_timeout
18
- @write_timeout = write_timeout
19
- @blocking_reads = false
20
- end
21
-
22
- def close
23
- @io.to_io.close
24
- end
25
-
26
- def closed?
27
- @io.to_io.closed?
28
- end
29
-
30
- def eof?
31
- @offset >= @buffer.bytesize && @io.eof?
32
- end
33
-
34
- def with_timeout(new_timeout)
35
- new_timeout = false if new_timeout == 0
36
-
37
- previous_read_timeout = @read_timeout
38
- previous_blocking_reads = @blocking_reads
39
-
40
- if new_timeout
41
- @read_timeout = new_timeout
42
- else
43
- @blocking_reads = true
44
- end
45
-
46
- begin
47
- yield
48
- ensure
49
- @read_timeout = previous_read_timeout
50
- @blocking_reads = previous_blocking_reads
51
- end
52
- end
53
-
54
- def skip(offset)
55
- ensure_remaining(offset)
56
- @offset += offset
57
- nil
58
- end
59
-
60
- def write(string)
61
- total = remaining = string.bytesize
62
- loop do
63
- case bytes_written = @io.write_nonblock(string, exception: false)
64
- when Integer
65
- remaining -= bytes_written
66
- if remaining > 0
67
- string = string.byteslice(bytes_written..-1)
68
- else
69
- return total
70
- end
71
- when :wait_readable
72
- @io.to_io.wait_readable(@read_timeout) or raise ReadTimeoutError
73
- when :wait_writable
74
- @io.to_io.wait_writable(@write_timeout) or raise WriteTimeoutError
75
- when nil
76
- raise Errno::ECONNRESET
77
- else
78
- raise "Unexpected `write_nonblock` return: #{bytes.inspect}"
79
- end
80
- end
81
- end
82
-
83
- def getbyte
84
- ensure_remaining(1)
85
- byte = @buffer.getbyte(@offset)
86
- @offset += 1
87
- byte
88
- end
89
-
90
- def gets_chomp
91
- fill_buffer(false) if @offset >= @buffer.bytesize
92
- until eol_index = @buffer.index(EOL, @offset)
93
- fill_buffer(false)
94
- end
95
-
96
- line = @buffer.byteslice(@offset, eol_index - @offset)
97
- @offset = eol_index + EOL_SIZE
98
- line
99
- end
100
-
101
- def read_chomp(bytes)
102
- ensure_remaining(bytes + EOL_SIZE)
103
- str = @buffer.byteslice(@offset, bytes)
104
- @offset += bytes + EOL_SIZE
105
- str
106
- end
107
-
108
- private
109
-
110
- def ensure_remaining(bytes)
111
- needed = bytes - (@buffer.bytesize - @offset)
112
- if needed > 0
113
- fill_buffer(true, needed)
114
- end
115
- end
116
-
117
- def fill_buffer(strict, size = @chunk_size)
118
- remaining = size
119
- empty_buffer = @offset >= @buffer.bytesize
120
-
121
- loop do
122
- bytes = if empty_buffer
123
- @io.read_nonblock([remaining, @chunk_size].max, @buffer, exception: false)
124
- else
125
- @io.read_nonblock([remaining, @chunk_size].max, exception: false)
126
- end
127
- case bytes
128
- when String
129
- if empty_buffer
130
- @offset = 0
131
- empty_buffer = false
132
- else
133
- @buffer << bytes
134
- end
135
- remaining -= bytes.bytesize
136
- return if !strict || remaining <= 0
137
- when :wait_readable
138
- unless @io.to_io.wait_readable(@read_timeout)
139
- raise ReadTimeoutError unless @blocking_reads
140
- end
141
- when :wait_writable
142
- @io.to_io.wait_writable(@write_timeout) or raise WriteTimeoutError
143
- when nil
144
- raise Errno::ECONNRESET
145
- else
146
- raise "Unexpected `read_nonblock` return: #{bytes.inspect}"
147
- end
148
- end
149
- end
150
- end
151
- end