Class: Hiredis::Ruby::Connection
- Inherits:
-
Object
- Object
- Hiredis::Ruby::Connection
- Defined in:
- lib/hiredis/ruby/connection.rb
Constant Summary collapse
- COMMAND_DELIMITER =
"\r\n".freeze
Instance Attribute Summary collapse
-
#sock ⇒ Object
readonly
Returns the value of attribute sock.
Class Method Summary collapse
Instance Method Summary collapse
- #_connect(host, port, timeout) ⇒ Object
- #_connect_unix(path, timeout) ⇒ Object
- #_write(sock, data, timeout) ⇒ Object
- #connect(host, port, usecs = nil) ⇒ Object
- #connect_unix(path, usecs = 0) ⇒ Object
- #connected? ⇒ Boolean
- #disconnect ⇒ Object
- #fileno ⇒ Object
-
#flush ⇒ Object
No-op for now..
-
#initialize ⇒ Connection
constructor
A new instance of Connection.
- #read ⇒ Object
- #timeout=(usecs) ⇒ Object
- #write(args) ⇒ Object
Constructor Details
#initialize ⇒ Connection
Returns a new instance of Connection.
176 177 178 179 |
# File 'lib/hiredis/ruby/connection.rb', line 176 def initialize @sock = nil @timeout = nil end |
Instance Attribute Details
#sock ⇒ Object (readonly)
Returns the value of attribute sock.
174 175 176 |
# File 'lib/hiredis/ruby/connection.rb', line 174 def sock @sock end |
Class Method Details
.errno_to_class ⇒ Object
11 12 13 |
# File 'lib/hiredis/ruby/connection.rb', line 11 def self.errno_to_class Errno::Mapping end |
Instance Method Details
#_connect(host, port, timeout) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/hiredis/ruby/connection.rb', line 30 def _connect(host, port, timeout) sock = nil begin Timeout.timeout(timeout) do sock = TCPSocket.new(host, port) end rescue SocketError => se raise se. rescue Timeout::Error raise Errno::ETIMEDOUT end sock end |
#_connect_unix(path, timeout) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/hiredis/ruby/connection.rb', line 46 def _connect_unix(path, timeout) sock = nil begin Timeout.timeout(timeout) do sock = UNIXSocket.new(path) end rescue SocketError => se raise se. rescue Timeout::Error raise Errno::ETIMEDOUT end sock end |
#_write(sock, data, timeout) ⇒ Object
62 63 64 65 66 67 68 69 70 |
# File 'lib/hiredis/ruby/connection.rb', line 62 def _write(sock, data, timeout) begin Timeout.timeout(timeout) do sock.write(data) end rescue Timeout::Error raise Errno::EAGAIN end end |
#connect(host, port, usecs = nil) ⇒ Object
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 |
# File 'lib/hiredis/ruby/connection.rb', line 185 def connect(host, port, usecs = nil) # Temporarily override timeout on #connect timeout = usecs ? (usecs / 1_000_000.0) : @timeout # Optionally disconnect current socket disconnect if connected? sock = _connect(host, port, timeout) sock.setsockopt Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1 @reader = ::Hiredis::Ruby::Reader.new @sock = sock nil end |
#connect_unix(path, usecs = 0) ⇒ Object
201 202 203 204 205 206 207 208 209 210 211 212 213 214 |
# File 'lib/hiredis/ruby/connection.rb', line 201 def connect_unix(path, usecs = 0) # Temporarily override timeout on #connect timeout = usecs ? (usecs / 1_000_000.0) : @timeout # Optionally disconnect current socket disconnect if connected? sock = _connect_unix(path, timeout) @reader = ::Hiredis::Ruby::Reader.new @sock = sock nil end |
#connected? ⇒ Boolean
181 182 183 |
# File 'lib/hiredis/ruby/connection.rb', line 181 def connected? !! @sock end |
#disconnect ⇒ Object
216 217 218 219 220 221 |
# File 'lib/hiredis/ruby/connection.rb', line 216 def disconnect @sock.close rescue ensure @sock = nil end |
#fileno ⇒ Object
235 236 237 238 239 |
# File 'lib/hiredis/ruby/connection.rb', line 235 def fileno raise "not connected" unless connected? @sock.fileno end |
#flush ⇒ Object
No-op for now..
260 261 |
# File 'lib/hiredis/ruby/connection.rb', line 260 def flush end |
#read ⇒ Object
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 |
# File 'lib/hiredis/ruby/connection.rb', line 263 def read raise "not connected" unless connected? while (reply = @reader.gets) == false begin @reader.feed @sock.read_nonblock(1024) rescue Errno::EAGAIN if IO.select([@sock], [], [], @timeout) # Readable, try again retry else # Timed out, raise raise Errno::EAGAIN end end end reply rescue ::EOFError raise Errno::ECONNRESET end |
#timeout=(usecs) ⇒ Object
223 224 225 226 227 228 229 230 231 232 233 |
# File 'lib/hiredis/ruby/connection.rb', line 223 def timeout=(usecs) raise ArgumentError.new("timeout cannot be negative") if usecs < 0 if usecs == 0 @timeout = nil else @timeout = usecs / 1_000_000.0 end nil end |
#write(args) ⇒ Object
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 |
# File 'lib/hiredis/ruby/connection.rb', line 243 def write(args) command = [] command << "*#{args.size}" args.each do |arg| arg = arg.to_s command << "$#{string_size arg}" command << arg end data = command.join(COMMAND_DELIMITER) + COMMAND_DELIMITER _write(@sock, data, @timeout) nil end |