Class: Redis2::Connection::Synchrony

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

Constant Summary

Constants included from CommandHelper

CommandHelper::COMMAND_DELIMITER

Class Method Summary collapse

Instance Method Summary collapse

Methods included from CommandHelper

#build_command

Constructor Details

#initialize(connection) ⇒ Synchrony

Returns a new instance of Synchrony.



88
89
90
# File 'lib/redis2/connection/synchrony.rb', line 88

def initialize(connection)
  @connection = connection
end

Class Method Details

.connect(config) ⇒ Object

Raises:

  • (Errno::ECONNREFUSED)


68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/redis2/connection/synchrony.rb', line 68

def self.connect(config)
  if config[:scheme] == "unix"
    conn = EventMachine.connect_unix_domain(config[:path], Redis2Client)
  else
    conn = EventMachine.connect(config[:host], config[:port], Redis2Client) do |c|
      c.pending_connect_timeout = [config[:timeout], 0.1].max
    end
  end

  fiber = Fiber.current
  conn.callback { fiber.resume }
  conn.errback { fiber.resume :refused }

  raise Errno::ECONNREFUSED if Fiber.yield == :refused

  instance = new(conn)
  instance.timeout = config[:timeout]
  instance
end

Instance Method Details

#connected?Boolean

Returns:

  • (Boolean)


92
93
94
# File 'lib/redis2/connection/synchrony.rb', line 92

def connected?
  @connection && @connection.connected?
end

#disconnectObject



100
101
102
103
# File 'lib/redis2/connection/synchrony.rb', line 100

def disconnect
  @connection.close_connection
  @connection = nil
end

#readObject



109
110
111
112
113
114
115
116
117
118
119
# File 'lib/redis2/connection/synchrony.rb', line 109

def read
  type, payload = @connection.read

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

#timeout=(timeout) ⇒ Object



96
97
98
# File 'lib/redis2/connection/synchrony.rb', line 96

def timeout=(timeout)
  @timeout = timeout
end

#write(command) ⇒ Object



105
106
107
# File 'lib/redis2/connection/synchrony.rb', line 105

def write(command)
  @connection.send(build_command(command))
end