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

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.



108
109
110
# File 'lib/redis/connection/synchrony.rb', line 108

def initialize(connection)
  @connection = connection
end

Class Method Details

.connect(config) ⇒ Object

Raises:

  • (Errno::ECONNREFUSED)


78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/redis/connection/synchrony.rb', line 78

def self.connect(config)
  if config[:scheme] == "unix"
    begin
      conn = EventMachine.connect_unix_domain(config[:path], RedisClient)
    rescue RuntimeError => e
      if e.message == "no connection"
        raise Errno::ECONNREFUSED
      else
        raise e
      end
    end
  elsif config[:scheme] == "rediss" || config[:ssl]
    raise NotImplementedError, "SSL not supported by synchrony driver"
  else
    conn = EventMachine.connect(config[:host], config[:port], RedisClient) do |c|
      c.pending_connect_timeout = [config[:connect_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[:read_timeout]
  instance
end

Instance Method Details

#connected?Boolean

Returns:

  • (Boolean)


112
113
114
# File 'lib/redis/connection/synchrony.rb', line 112

def connected?
  @connection&.connected?
end

#disconnectObject



120
121
122
123
# File 'lib/redis/connection/synchrony.rb', line 120

def disconnect
  @connection.close_connection
  @connection = nil
end

#readObject



129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/redis/connection/synchrony.rb', line 129

def read
  type, payload = @connection.read

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

#timeout=(timeout) ⇒ Object



116
117
118
# File 'lib/redis/connection/synchrony.rb', line 116

def timeout=(timeout)
  @connection.timeout = timeout
end

#write(command) ⇒ Object



125
126
127
# File 'lib/redis/connection/synchrony.rb', line 125

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