Class: Redis::Client
- Inherits:
-
Object
- Object
- Redis::Client
- Defined in:
- lib/redis/client.rb
Defined Under Namespace
Classes: Connector
Constant Summary collapse
- DEFAULTS =
{ url: -> { ENV["REDIS_URL"] }, scheme: "redis", host: "127.0.0.1", port: 6379, path: nil, timeout: 5.0, password: nil, db: 0, driver: nil, id: nil, tcp_keepalive: 0, reconnect_attempts: 1, reconnect_delay: 0, reconnect_delay_max: 0.5, inherit_socket: false, sentinels: nil, role: nil }.freeze
Instance Attribute Summary collapse
-
#command_map ⇒ Object
readonly
Returns the value of attribute command_map.
-
#connection ⇒ Object
readonly
Returns the value of attribute connection.
-
#logger ⇒ Object
Returns the value of attribute logger.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
- #call(command) ⇒ Object
- #call_loop(command, timeout = 0) ⇒ Object
- #call_pipeline(pipeline) ⇒ Object
- #call_pipelined(pipeline) ⇒ Object
- #call_with_timeout(command, timeout, &blk) ⇒ Object
- #call_without_timeout(command, &blk) ⇒ Object
- #connect ⇒ Object
- #connect_timeout ⇒ Object
- #connected? ⇒ Boolean
- #db ⇒ Object
- #db=(db) ⇒ Object
- #disconnect ⇒ Object (also: #close)
- #driver ⇒ Object
- #host ⇒ Object
- #id ⇒ Object
- #inherit_socket? ⇒ Boolean
-
#initialize(options = {}) ⇒ Client
constructor
A new instance of Client.
- #io ⇒ Object
- #location ⇒ Object
- #password ⇒ Object
- #path ⇒ Object
- #port ⇒ Object
- #process(commands) ⇒ Object
- #read ⇒ Object
- #read_timeout ⇒ Object
- #reconnect ⇒ Object
- #scheme ⇒ Object
- #timeout ⇒ Object
- #with_reconnect(val = true) ⇒ Object
- #with_socket_timeout(timeout) ⇒ Object
- #without_reconnect(&blk) ⇒ Object
- #without_socket_timeout(&blk) ⇒ Object
- #write(command) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Client
Returns a new instance of Client.
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/redis/client.rb', line 83 def initialize( = {}) = () @reconnect = true @logger = [:logger] @connection = nil @command_map = {} @pending_reads = 0 @connector = if ![:sentinels].nil? Connector::Sentinel.new() elsif .include?(:connector) && [:connector].respond_to?(:new) .delete(:connector).new() else Connector.new() end end |
Instance Attribute Details
#command_map ⇒ Object (readonly)
Returns the value of attribute command_map.
81 82 83 |
# File 'lib/redis/client.rb', line 81 def command_map @command_map end |
#connection ⇒ Object (readonly)
Returns the value of attribute connection.
80 81 82 |
# File 'lib/redis/client.rb', line 80 def connection @connection end |
#logger ⇒ Object
Returns the value of attribute logger.
79 80 81 |
# File 'lib/redis/client.rb', line 79 def logger @logger end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
29 30 31 |
# File 'lib/redis/client.rb', line 29 def end |
Instance Method Details
#call(command) ⇒ Object
125 126 127 128 129 130 131 132 133 134 |
# File 'lib/redis/client.rb', line 125 def call(command) reply = process([command]) { read } raise reply if reply.is_a?(CommandError) if block_given? yield reply else reply end end |
#call_loop(command, timeout = 0) ⇒ Object
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/redis/client.rb', line 136 def call_loop(command, timeout = 0) error = nil result = with_socket_timeout(timeout) do process([command]) do loop do reply = read if reply.is_a?(CommandError) error = reply break else yield reply end end end end # Raise error when previous block broke out of the loop. raise error if error # Result is set to the value that the provided block used to break. result end |
#call_pipeline(pipeline) ⇒ Object
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/redis/client.rb', line 160 def call_pipeline(pipeline) return [] if pipeline.futures.empty? with_reconnect pipeline.with_reconnect? do begin pipeline.finish(call_pipelined(pipeline)).tap do self.db = pipeline.db if pipeline.db end rescue ConnectionError => e return nil if pipeline.shutdown? # Assume the pipeline was sent in one piece, but execution of # SHUTDOWN caused none of the replies for commands that were executed # prior to it from coming back around. raise e end end end |
#call_pipelined(pipeline) ⇒ Object
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 |
# File 'lib/redis/client.rb', line 179 def call_pipelined(pipeline) return [] if pipeline.futures.empty? # The method #ensure_connected (called from #process) reconnects once on # I/O errors. To make an effort in making sure that commands are not # executed more than once, only allow reconnection before the first reply # has been read. When an error occurs after the first reply has been # read, retrying would re-execute the entire pipeline, thus re-issuing # already successfully executed commands. To circumvent this, don't retry # after the first reply has been read successfully. commands = pipeline.commands result = Array.new(commands.size) reconnect = @reconnect begin exception = nil process(commands) do pipeline.timeouts.each_with_index do |timeout, i| reply = if timeout with_socket_timeout(timeout) { read } else read end result[i] = reply @reconnect = false exception = reply if exception.nil? && reply.is_a?(CommandError) end end raise exception if exception ensure @reconnect = reconnect end result end |
#call_with_timeout(command, timeout, &blk) ⇒ Object
219 220 221 222 223 224 225 |
# File 'lib/redis/client.rb', line 219 def call_with_timeout(command, timeout, &blk) with_socket_timeout(timeout) do call(command, &blk) end rescue ConnectionError retry end |
#call_without_timeout(command, &blk) ⇒ Object
227 228 229 |
# File 'lib/redis/client.rb', line 227 def call_without_timeout(command, &blk) call_with_timeout(command, 0, &blk) end |
#connect ⇒ Object
102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/redis/client.rb', line 102 def connect @pid = Process.pid # Don't try to reconnect when the connection is fresh with_reconnect(false) do establish_connection call [:auth, password] if password call [:select, db] if db != 0 call [:client, :setname, [:id]] if [:id] @connector.check(self) end self end |
#connect_timeout ⇒ Object
51 52 53 |
# File 'lib/redis/client.rb', line 51 def connect_timeout [:connect_timeout] end |
#connected? ⇒ Boolean
248 249 250 |
# File 'lib/redis/client.rb', line 248 def connected? !!(connection && connection.connected?) end |
#db ⇒ Object
63 64 65 |
# File 'lib/redis/client.rb', line 63 def db [:db] end |
#db=(db) ⇒ Object
67 68 69 |
# File 'lib/redis/client.rb', line 67 def db=(db) [:db] = db.to_i end |
#disconnect ⇒ Object Also known as: close
252 253 254 |
# File 'lib/redis/client.rb', line 252 def disconnect connection.disconnect if connected? end |
#driver ⇒ Object
71 72 73 |
# File 'lib/redis/client.rb', line 71 def driver [:driver] end |
#host ⇒ Object
35 36 37 |
# File 'lib/redis/client.rb', line 35 def host [:host] end |
#id ⇒ Object
117 118 119 |
# File 'lib/redis/client.rb', line 117 def id [:id] || "redis://#{location}/#{db}" end |
#inherit_socket? ⇒ Boolean
75 76 77 |
# File 'lib/redis/client.rb', line 75 def inherit_socket? [:inherit_socket] end |
#io ⇒ Object
262 263 264 265 266 267 268 269 270 271 |
# File 'lib/redis/client.rb', line 262 def io yield rescue TimeoutError => e1 # Add a message to the exception without destroying the original stack e2 = TimeoutError.new("Connection timed out") e2.set_backtrace(e1.backtrace) raise e2 rescue Errno::ECONNRESET, Errno::EPIPE, Errno::ECONNABORTED, Errno::EBADF, Errno::EINVAL => e raise ConnectionError, "Connection lost (%s)" % [e.class.name.split("::").last] end |
#location ⇒ Object
121 122 123 |
# File 'lib/redis/client.rb', line 121 def location path || "#{host}:#{port}" end |
#password ⇒ Object
59 60 61 |
# File 'lib/redis/client.rb', line 59 def password [:password] end |
#path ⇒ Object
43 44 45 |
# File 'lib/redis/client.rb', line 43 def path [:path] end |
#port ⇒ Object
39 40 41 |
# File 'lib/redis/client.rb', line 39 def port [:port] end |
#process(commands) ⇒ Object
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 |
# File 'lib/redis/client.rb', line 231 def process(commands) logging(commands) do ensure_connected do commands.each do |command| if command_map[command.first] command = command.dup command[0] = command_map[command.first] end write(command) end yield if block_given? end end end |
#read ⇒ Object
273 274 275 276 277 278 279 |
# File 'lib/redis/client.rb', line 273 def read io do value = connection.read @pending_reads -= 1 value end end |
#read_timeout ⇒ Object
47 48 49 |
# File 'lib/redis/client.rb', line 47 def read_timeout [:read_timeout] end |
#reconnect ⇒ Object
257 258 259 260 |
# File 'lib/redis/client.rb', line 257 def reconnect disconnect connect end |
#scheme ⇒ Object
31 32 33 |
# File 'lib/redis/client.rb', line 31 def scheme [:scheme] end |
#timeout ⇒ Object
55 56 57 |
# File 'lib/redis/client.rb', line 55 def timeout [:read_timeout] end |
#with_reconnect(val = true) ⇒ Object
306 307 308 309 310 311 |
# File 'lib/redis/client.rb', line 306 def with_reconnect(val = true) original, @reconnect = @reconnect, val yield ensure @reconnect = original end |
#with_socket_timeout(timeout) ⇒ Object
288 289 290 291 292 293 294 295 296 297 298 299 300 |
# File 'lib/redis/client.rb', line 288 def with_socket_timeout(timeout) connect unless connected? original = [:read_timeout] begin connection.timeout = timeout [:read_timeout] = timeout # for reconnection yield ensure connection.timeout = self.timeout if connected? [:read_timeout] = original end end |
#without_reconnect(&blk) ⇒ Object
313 314 315 |
# File 'lib/redis/client.rb', line 313 def without_reconnect(&blk) with_reconnect(false, &blk) end |
#without_socket_timeout(&blk) ⇒ Object
302 303 304 |
# File 'lib/redis/client.rb', line 302 def without_socket_timeout(&blk) with_socket_timeout(0, &blk) end |
#write(command) ⇒ Object
281 282 283 284 285 286 |
# File 'lib/redis/client.rb', line 281 def write(command) io do @pending_reads += 1 connection.write(command) end end |