redis-client 0.1.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.rubocop.yml +1 -0
- data/CHANGELOG.md +19 -0
- data/Gemfile.lock +2 -2
- data/README.md +89 -3
- data/Rakefile +15 -6
- data/ext/redis_client/hiredis/export.clang +2 -0
- data/ext/redis_client/hiredis/export.gcc +7 -0
- data/ext/redis_client/hiredis/extconf.rb +24 -9
- data/ext/redis_client/hiredis/hiredis_connection.c +13 -1
- data/lib/redis_client/command_builder.rb +83 -0
- data/lib/redis_client/config.rb +9 -48
- data/lib/redis_client/connection_mixin.rb +38 -0
- data/lib/redis_client/decorator.rb +84 -0
- data/lib/redis_client/hiredis_connection.rb +16 -1
- data/lib/redis_client/middlewares.rb +12 -0
- data/lib/redis_client/pooled.rb +55 -35
- data/lib/redis_client/ruby_connection/buffered_io.rb +153 -0
- data/lib/redis_client/{resp3.rb → ruby_connection/resp3.rb} +1 -27
- data/lib/redis_client/{connection.rb → ruby_connection.rb} +51 -5
- data/lib/redis_client/version.rb +1 -1
- data/lib/redis_client.rb +239 -106
- metadata +11 -5
- data/lib/redis_client/buffered_io.rb +0 -149
@@ -1,149 +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
|
-
def initialize(io, read_timeout:, write_timeout:, chunk_size: 4096)
|
11
|
-
@io = io
|
12
|
-
@buffer = "".b
|
13
|
-
@offset = 0
|
14
|
-
@chunk_size = chunk_size
|
15
|
-
@read_timeout = read_timeout
|
16
|
-
@write_timeout = write_timeout
|
17
|
-
@blocking_reads = false
|
18
|
-
end
|
19
|
-
|
20
|
-
def close
|
21
|
-
@io.to_io.close
|
22
|
-
end
|
23
|
-
|
24
|
-
def closed?
|
25
|
-
@io.to_io.closed?
|
26
|
-
end
|
27
|
-
|
28
|
-
def eof?
|
29
|
-
@offset >= @buffer.bytesize && @io.eof?
|
30
|
-
end
|
31
|
-
|
32
|
-
def with_timeout(new_timeout)
|
33
|
-
new_timeout = false if new_timeout == 0
|
34
|
-
|
35
|
-
previous_read_timeout = @read_timeout
|
36
|
-
previous_blocking_reads = @blocking_reads
|
37
|
-
|
38
|
-
if new_timeout
|
39
|
-
@read_timeout = new_timeout
|
40
|
-
else
|
41
|
-
@blocking_reads = true
|
42
|
-
end
|
43
|
-
|
44
|
-
begin
|
45
|
-
yield
|
46
|
-
ensure
|
47
|
-
@read_timeout = previous_read_timeout
|
48
|
-
@blocking_reads = previous_blocking_reads
|
49
|
-
end
|
50
|
-
end
|
51
|
-
|
52
|
-
def skip(offset)
|
53
|
-
ensure_remaining(offset)
|
54
|
-
@offset += offset
|
55
|
-
nil
|
56
|
-
end
|
57
|
-
|
58
|
-
def write(string)
|
59
|
-
total = remaining = string.bytesize
|
60
|
-
loop do
|
61
|
-
case bytes_written = @io.write_nonblock(string, exception: false)
|
62
|
-
when Integer
|
63
|
-
remaining -= bytes_written
|
64
|
-
if remaining > 0
|
65
|
-
string = string.byteslice(bytes_written..-1)
|
66
|
-
else
|
67
|
-
return total
|
68
|
-
end
|
69
|
-
when :wait_readable
|
70
|
-
@io.to_io.wait_readable(@read_timeout) or raise ReadTimeoutError
|
71
|
-
when :wait_writable
|
72
|
-
@io.to_io.wait_writable(@write_timeout) or raise WriteTimeoutError
|
73
|
-
when nil
|
74
|
-
raise Errno::ECONNRESET
|
75
|
-
else
|
76
|
-
raise "Unexpected `write_nonblock` return: #{bytes.inspect}"
|
77
|
-
end
|
78
|
-
end
|
79
|
-
end
|
80
|
-
|
81
|
-
def getbyte
|
82
|
-
ensure_remaining(1)
|
83
|
-
byte = @buffer.getbyte(@offset)
|
84
|
-
@offset += 1
|
85
|
-
byte
|
86
|
-
end
|
87
|
-
|
88
|
-
def gets_chomp
|
89
|
-
fill_buffer(false) if @offset >= @buffer.bytesize
|
90
|
-
until eol_index = @buffer.index(EOL, @offset)
|
91
|
-
fill_buffer(false)
|
92
|
-
end
|
93
|
-
|
94
|
-
line = @buffer.byteslice(@offset, eol_index - @offset)
|
95
|
-
@offset = eol_index + EOL_SIZE
|
96
|
-
line
|
97
|
-
end
|
98
|
-
|
99
|
-
def read_chomp(bytes)
|
100
|
-
ensure_remaining(bytes + EOL_SIZE)
|
101
|
-
str = @buffer.byteslice(@offset, bytes)
|
102
|
-
@offset += bytes + EOL_SIZE
|
103
|
-
str
|
104
|
-
end
|
105
|
-
|
106
|
-
private
|
107
|
-
|
108
|
-
def ensure_remaining(bytes)
|
109
|
-
needed = bytes - (@buffer.bytesize - @offset)
|
110
|
-
if needed > 0
|
111
|
-
fill_buffer(true, needed)
|
112
|
-
end
|
113
|
-
end
|
114
|
-
|
115
|
-
def fill_buffer(strict, size = @chunk_size)
|
116
|
-
remaining = size
|
117
|
-
empty_buffer = @offset >= @buffer.bytesize
|
118
|
-
|
119
|
-
loop do
|
120
|
-
bytes = if empty_buffer
|
121
|
-
@io.read_nonblock([remaining, @chunk_size].max, @buffer, exception: false)
|
122
|
-
else
|
123
|
-
@io.read_nonblock([remaining, @chunk_size].max, exception: false)
|
124
|
-
end
|
125
|
-
case bytes
|
126
|
-
when String
|
127
|
-
if empty_buffer
|
128
|
-
@offset = 0
|
129
|
-
empty_buffer = false
|
130
|
-
else
|
131
|
-
@buffer << bytes
|
132
|
-
end
|
133
|
-
remaining -= bytes.bytesize
|
134
|
-
return if !strict || remaining <= 0
|
135
|
-
when :wait_readable
|
136
|
-
unless @io.to_io.wait_readable(@read_timeout)
|
137
|
-
raise ReadTimeoutError unless @blocking_reads
|
138
|
-
end
|
139
|
-
when :wait_writable
|
140
|
-
@io.to_io.wait_writable(@write_timeout) or raise WriteTimeoutError
|
141
|
-
when nil
|
142
|
-
raise Errno::ECONNRESET
|
143
|
-
else
|
144
|
-
raise "Unexpected `read_nonblock` return: #{bytes.inspect}"
|
145
|
-
end
|
146
|
-
end
|
147
|
-
end
|
148
|
-
end
|
149
|
-
end
|