Class: Redis::Connection::Synchrony

Inherits:
Object
  • Object
show all
Includes:
CommandHelper
Defined in:
lib/redis/connection/synchrony.rb

Constant Summary

Constants included from CommandHelper

CommandHelper::COMMAND_DELIMITER

Instance Method Summary collapse

Methods included from CommandHelper

#build_command

Constructor Details

#initializeSynchrony

Returns a new instance of Synchrony.



54
55
56
57
58
# File 'lib/redis/connection/synchrony.rb', line 54

def initialize
  @timeout = 5_000_000
  @state = :disconnected
  @connection = nil
end

Instance Method Details

#connect(host, port, timeout) ⇒ Object



68
69
70
71
72
73
74
# File 'lib/redis/connection/synchrony.rb', line 68

def connect(host, port, timeout)
  conn = EventMachine.connect(host, port, RedisClient) do |c|
    c.pending_connect_timeout = [Float(timeout / 1_000_000), 0.1].max
  end

  setup_connect_callbacks(conn, Fiber.current)
end

#connect_unix(path, timeout) ⇒ Object



76
77
78
79
# File 'lib/redis/connection/synchrony.rb', line 76

def connect_unix(path, timeout)
  conn = EventMachine.connect_unix_domain(path, RedisClient)
  setup_connect_callbacks(conn, Fiber.current)
end

#connected?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/redis/connection/synchrony.rb', line 60

def connected?
  @state == :connected
end

#disconnectObject



81
82
83
84
85
# File 'lib/redis/connection/synchrony.rb', line 81

def disconnect
  @state = :disconnected
  @connection.close_connection
  @connection = nil
end

#readObject



91
92
93
94
95
96
97
98
99
100
101
# File 'lib/redis/connection/synchrony.rb', line 91

def read
  type, payload = @connection.read

  if type == :reply
    payload
  elsif type == :error
    raise payload
  else
    raise "Unknown type #{type.inspect}"
  end
end

#timeout=(usecs) ⇒ Object



64
65
66
# File 'lib/redis/connection/synchrony.rb', line 64

def timeout=(usecs)
  @timeout = usecs
end

#write(command) ⇒ Object



87
88
89
# File 'lib/redis/connection/synchrony.rb', line 87

def write(command)
  @connection.send(build_command(*command).join(COMMAND_DELIMITER))
end